• 导出excel


       protected void Button1_Click(object sender, EventArgs e)
            {
                string fileName = "WLSearch_Favorites.xls";
                string sendMethdo = this.DropDownList1.Text;
                object[] obj = { sendMethdo };
                DataTable dt = CM.DataAccess.dataDrive.WelanMain.connProcedureExec("CustomerAddress", obj).Tables[0];
                ExportEasy(dt, fileName);


                fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);//解决导出EXCEL时乱码的问题

                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "GB2312"; //设置了类型为中文防止乱码的出现
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName); //定义输出文件和文件名
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
                Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
                this.EnableViewState = false;
                System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

                GridView gv = new GridView();

                gv.DataSource = dt;
                gv.DataBind();
                gv.RenderControl(oHtmlTextWriter);


                Response.Write(oStringWriter.ToString());
                Response.End();
                Response.Write("<script>window.close();</script>");
            }
            public static void ExportEasy(DataTable dtSource, string strFileName)
            {
                HSSFWorkbook workbook = new HSSFWorkbook();
                HSSFSheet sheet = workbook.CreateSheet();

                //填充表头
                HSSFRow dataRow = sheet.CreateRow(0);
                foreach (DataColumn column in dtSource.Columns)
                {
                    dataRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
                }
                //填充内容
                for (int i = 0; i < dtSource.Rows.Count; i++)
                {
                    dataRow = sheet.CreateRow(i + 1);
                    for (int j = 0; j < dtSource.Columns.Count; j++)
                    {
                        dataRow.CreateCell(j).SetCellValue(dtSource.Rows[i][j].ToString());
                    }
                }
                //保存
                using (MemoryStream ms = new MemoryStream())
                {
                    using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write))
                    {
                        workbook.Write(fs);
                    }
                }
            }

    =====================================================================

  • 相关阅读:
    python加载csv数据
    Android项目依赖库管理方式简介
    Android PhotoView基本功能实现
    Android ListView的header footer设置visibility gone不起作用
    [干货][EMIT]千行代码实现代理式AOP+属性的自动装配
    Emit实现DataRow转化成强类型的T
    有关docker新版的icc、iptables的一个巨坑
    Mac神器Iterm2的Shell Integration的用法和注意事项
    生成ssl证书文件
    python3 module中__init__.py的需要注意的地方
  • 原文地址:https://www.cnblogs.com/happygx/p/1957910.html
Copyright © 2020-2023  润新知