Asp.net导出Excel方法

11 篇文章 0 订阅


方法一:

直接导出控件的数据源

 

 /// <summary>
        /// 从控件导出
        /// </summary>
        /// <param name="title">标题</param>
        public void outputExcle1(string title)
        {
          
            string fileName = string.Empty;
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ClearHeaders();
           
            //HttpContext.Current.Response.Charset = "utf-8";
            //HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
            HttpContext.Current.Response.Charset = "GB2312"; //设置了类型为中文防止乱码的出现 

            fileName = string.Format("Export-File {0:yyyy-MM-dd_HH_mm}.xls", DateTime.Now);
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文

            HttpContext.Current.Response.AppendHeader("Content-Type", "text/html; charset=gb2312");
            HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
            
            StringWriter tw = new System.IO.StringWriter();
            HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

            this.Repeater1.RenderControl(hw); //将服务器控件中的内容输出到hw中 有RenderControl方法的控件应该都支持
          
            if (!string.IsNullOrEmpty(title))
            {
                HttpContext.Current.Response.Write("<b><center><font size=3 face=Verdana color=#0000FF>" + title + "</font></center></b>");
            }

           
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.Close();
            HttpContext.Current.Response.End();

        }

 

方法二,直接将DataTable导出

 

     public void outputExcle2(System.Data.DataTable dt)
        {

            StringWriter stringwriter = new StringWriter();
            HtmlTextWriter htmlwriter = new HtmlTextWriter(stringwriter);
            DataGrid excel = new DataGrid();
            System.Web.UI.WebControls.TableItemStyle alternatingstyle = new TableItemStyle();
            System.Web.UI.WebControls.TableItemStyle headerstyle = new TableItemStyle();
            System.Web.UI.WebControls.TableItemStyle itemstyle = new TableItemStyle();
            alternatingstyle.BackColor = System.Drawing.Color.LightGray;
            headerstyle.BackColor = System.Drawing.Color.White;
            headerstyle.Font.Bold = true;
            headerstyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
            itemstyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center; ;
            excel.AlternatingItemStyle.MergeWith(alternatingstyle);
            excel.HeaderStyle.MergeWith(headerstyle);
            excel.ItemStyle.MergeWith(itemstyle);
            excel.GridLines = GridLines.Both;
            excel.HeaderStyle.Font.Bold = true;
            excel.DataSource = dt;   //输出datatable的内容
            excel.DataBind();
            excel.RenderControl(htmlwriter);
            string filepath = "aaa.xls";
            string filestr = Server.MapPath(filepath);  //filepath是文件的路径
            int pos = filestr.LastIndexOf("\\");
            string file = filestr.Substring(0, pos);
            if (!Directory.Exists(file))
            {
                Directory.CreateDirectory(file);
            }
            System.IO.StreamWriter sw = new StreamWriter(filestr);
            sw.Write(stringwriter.ToString());
            sw.Close();
             
        }

 

 

方法三:

直接导出DataTable 要引用:Microsoft.Office.Interop.Excel.dll 

   public void outputExcle2(System.Data.DataTable dt)
        {
            Application xlApp = new Application();

            Workbooks w = xlApp.Workbooks;

            Workbook workbook = w.Add(XlWBATemplate.xlWBATWorksheet);

            Worksheet worksheet = (Worksheet)workbook.Worksheets[1];

            //写入字段

            for (int i = 0; i < dt.Columns.Count; i++)
            {

                worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;

            }

            //写入数值

            for (int r = 0; r < dt.Rows.Count; r++)
            {

                for (int i = 0; i < dt.Columns.Count; i++)
                {

                    worksheet.Cells[r + 2, i + 1] = dt.Rows[r][i];

                }

            }

            worksheet.Columns.EntireColumn.AutoFit();//列宽自适应。

            workbook.Saved = true;
            workbook.SaveCopyAs(Server.MapPath("test.xls"));
            

            xlApp.Quit();

            GC.Collect();//强行销毁
            Response.Redirect("test.xls");
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值