码迷,mamicode.com
首页 > 其他好文 > 详细

NPOI的excel导出1

时间:2018-01-17 12:35:24      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:序号   webconfig   redirect   释放   ret   pat   ++   row   pos   

   #region 导出代码
        /// <summary>
        /// 导出代码
        /// </summary>
        public  void Export()
        {
            NPOI.HSSF.UserModel.HSSFWorkbook workbook = new NPOI.HSSF.UserModel.HSSFWorkbook();
            NPOI.SS.UserModel.ISheet sheet1 = workbook.CreateSheet("BOM详情");
            //给sheet1添加第一行的头部标题
            NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
            row1.CreateCell(0).SetCellValue("序号");
            row1.CreateCell(1).SetCellValue("用户名");
            row1.CreateCell(2).SetCellValue("编号");
           // DataTable dt = ExecuteStoredPro("Store_Query", "teacher", "id,tname,TCode", "id>1");//获取数据表
            DataSet ds = Common.getds("select * from teacher");
            DataTable dt = ds.Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1);
                rowtemp.CreateCell(0).SetCellValue(dt.Rows[i]["id"].ToString());
                rowtemp.CreateCell(1).SetCellValue(dt.Rows[i]["tname"].ToString());
                rowtemp.CreateCell(2).SetCellValue(dt.Rows[i]["TCode"].ToString());
            }
            //写入到客户端
            string path = "/FileRoot/temp/";
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls";
            path += fileName;
            System.IO.FileStream file = new System.IO.FileStream(HttpContext.Current.Server.MapPath(path), System.IO.FileMode.Create);
            workbook.Write(file);
            //关闭文件,释放对象
            file.Close();
            string filePath = Server.MapPath(path);//路径
            Response.Redirect("downExcel.aspx?filePath=" + filePath + "&fileName=" + DateTime.Now.ToString("yyyyMMddHHmmssfff")+".xls");
        }
        #endregion

 

 

  public static string conn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connStr"].ToString();//获取配置数据库字符串
        #region 获取dataset
        /// <summary>
        /// 获取dataset
        /// </summary>
        /// <param name="sqlstr">传入的sql语句</param>
        /// <returns></returns>
        public static DataSet getds(string sqlstr)
        {
            DataSet ds = null;
            using (MySqlConnection conn = new MySqlConnection(Common.conn))
            {
                conn.Open();
                using (MySqlCommand cmd = new MySqlCommand(sqlstr, conn))
                {
                    MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
                    ds = new DataSet();
                    adapter.Fill(ds);

                }
            }
            return ds;
        }
        #endregion

NPOI的excel导出1

标签:序号   webconfig   redirect   释放   ret   pat   ++   row   pos   

原文地址:https://www.cnblogs.com/wugh8726254/p/8302017.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!