• 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

  • 相关阅读:
    js 将内容复制到剪切板上
    javascript刷新父页面的各种方法汇总
    jQuery 使得文本框获得焦点
    layui switch 开关监听 弹出确定状态转换
    layui 图片上传+表单提交+ Spring MVC
    python爬虫实例--网易云音乐排行榜爬虫
    Python爬虫html解析工具beautifulSoup在pycharm中安装及失败的解决办法
    python爬虫实例--博客园首页Java目录博文爬虫
    让js中的函数只有一次有效调用的三种常用方法
    spring项目获取ServletContext
  • 原文地址:https://www.cnblogs.com/wugh8726254/p/8302017.html
Copyright © 2020-2023  润新知