NPOI的excel导出1

Posted .net&new

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NPOI的excel导出1相关的知识,希望对你有一定的参考价值。

   #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的主要内容,如果未能解决你的问题,请参考以下文章

●导出excel(NPOI)

NPOI导出Excel

NPOI Excel导出数据信息

NPOI 导出Excel

NPOI 导出 excel 性能测试

.Net Core+NPOI快速导入导出Excel