Asp.net 将DataGrid分多个Sheet导出Excel

13 篇文章 0 订阅
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

/// <summary>
///ExcelUtil 的摘要说明
/// </summary>
public class ExcelUtil
{
    public ExcelUtil()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
    string _Author;
    string _Company;

    StringBuilder sbBody = new StringBuilder();
    StringBuilder sbSheet = new StringBuilder();
    public ExcelUtil(string Author, string Company)
    {
        _Author = Author;
        _Company = Company;
        sbBody.AppendFormat(
            "MIME-Version: 1.0\r\n" +
            "X-Document-Type: Workbook\r\n" +
            "Content-Type: multipart/related; boundary=\"-=BOUNDARY_EXCEL\"\r\n\r\n" +
            "---=BOUNDARY_EXCEL\r\n" +
            "Content-Type: text/html; charset=\"big5\"\r\n\r\n" +
            "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\n" +
            "xmlns:x=\"urn:schemas-microsoft-com:office:excel\">\r\n\r\n" +
            "<head>\r\n" +
            "<xml>\r\n" +
            "<o:DocumentProperties>\r\n" +
            "<o:Author>...{0}</o:Author>\r\n" +
            "<o:LastAuthor>...{0}</o:LastAuthor>\r\n" +
            "<o:Created>...{1}</o:Created>\r\n" +
            "<o:LastSaved>...{1}</o:LastSaved>\r\n" +
            "<o:Company>...{2}</o:Company>\r\n" +
            "<o:Version>11.5606</o:Version>\r\n" +
            "</o:DocumentProperties>\r\n" +
            "</xml>\r\n" +
            "<xml>\r\n" +
            "<x:ExcelWorkbook>\r\n" +
            "<x:ExcelWorksheets>\r\n"
            , _Author
            , DateTime.Now.ToString()
            , _Company);
    }

    private string ExportExcel()
    {
        StringBuilder sb = new StringBuilder(sbBody.ToString());

        sb.Append("</x:ExcelWorksheets>\r\n" +
            "</x:ExcelWorkbook>\r\n" +
           "</xml>\r\n" +
            "</head>\r\n" +
            "</html>\r\n\r\n");

        sb.Append(sbSheet.ToString());

        sb.Append("---=BOUNDARY_EXCEL--");

        return sb.ToString();
    }

   public void AddGrid(DataGrid grid, string sheetName)
    {
        string gid = Guid.NewGuid().ToString();
        sbBody.AppendFormat("<x:ExcelWorksheet>\r\n" +
            "<x:Name>...{0}</x:Name>\r\n" +
            "<x:WorksheetSource HRef=\"cid:...{1}\"/>\r\n" +
            "</x:ExcelWorksheet>\r\n"
            , sheetName.Replace(":", "").Replace("\\", "").Replace("/", "").Replace

     ("?", "").Replace("*", "").Replace("[", "").Replace("]", "").Trim()
            , gid);

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        grid.RenderControl(htw);

        sbSheet.AppendFormat("---=BOUNDARY_EXCEL\r\n" +
         "Content-ID: ...{0}\r\n" +
         "Content-Type: text/html; charset=\"big5\"\r\n\r\n" +
         "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\n" +
         "xmlns:x=\"urn:schemas-microsoft-com:office:excel\">\r\n\r\n" +
         "<head>\r\n" +
         "<xml>\r\n" +
         "<x:WorksheetOptions>\r\n" +
         "<x:ProtectContents>False</x:ProtectContents>\r\n" +
         "<x:ProtectObjects>False</x:ProtectObjects>\r\n" +
         "<x:ProtectScenarios>False</x:ProtectScenarios>\r\n" +
         "</x:WorksheetOptions>\r\n" +
         "</xml>\r\n" +
         "</head>\r\n" +
         "<body>\r\n"
         , gid);
        sbSheet.Append(sw.ToString());
        sbSheet.Append("</body>\r\n" +
            "</html>\r\n\r\n");
        sw.Close();
        htw.Close();
    }

    public void Export(Page page, string FileName)
    {
        page.Response.Clear();
        page.Response.Buffer = true;
        page.Response.Charset = "";
        page.Response.AddHeader("content-disposition", string.Format

     ("attachment;filename=...{0}.xls", FileName));
        page.Response.ContentEncoding = Encoding.GetEncoding("big5");
        page.Response.ContentType = "application/vnd.ms-excel";
        page.Response.Write(ExportExcel());
        page.Response.End();
    }

    public void Clear()
    {
        sbBody.Remove(0, sbBody.Length);
        sbSheet.Remove(0, sbBody.Length);
    }
}


 调用方法:

           ExcelUtil eu=new ExcelUtil("Jeff_Yeh","隨手記");
           eu.AddGrid(givw, "TestA");
           eu.AddGrid(givw, "TestB");
           eu.Export(this,"Jeff");

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值