日期:2014-05-18  浏览次数:20467 次

求个能导出Excel2007的通用类
网上找了一堆 都是只支持2003的, 导出的Excel我在用Excel2007 打开的时候总是提示与指定的扩展名 不一致,Excel也能打开。
有没能支持07的 这个 通用 方法 ,类似只需要传 Datetable,表头,表头字段 进去. 直接能通过respose输出的
100求.


------解决方案--------------------
其实,存储成xml格式的就可以了,你可以直接拷贝下面的代码执行下看
C# code
<%@ Page Language="C#" EnableViewState="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      System.Data.DataTable dt = new System.Data.DataTable();
      System.Data.DataRow dr;
      dt.Columns.Add(new System.Data.DataColumn("学生班级", typeof(System.String)));
      dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String)));
      dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal)));
      dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal)));
      dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal)));
      dt.Columns.Add(new System.Data.DataColumn("计算机", typeof(System.Decimal)));
      System.Random rd = new System.Random();
      for (int i = 0; i < 88; i++)
      {
        dr = dt.NewRow();
        dr[0] = "班级" + i.ToString();
        dr[1] = "【孟子E章】" + i.ToString();
        dr[2] = System.Math.Round(rd.NextDouble() * 100, 0);
        dr[3] = System.Math.Round(rd.NextDouble() * 100, 0);
        dr[4] = System.Math.Round(rd.NextDouble() * 100, 0);
        dr[5] = System.Math.Round(rd.NextDouble() * 100, 0);
        dt.Rows.Add(dr);
      }
      GridView1.DataSource = dt;
      GridView1.DataBind();
    }
  }

  protected void Button1_Click(object sender, EventArgs e)
  {
    //假如每10条数据放在一个 Sheet 里面,先计算需要多少个 Sheet
    int ItenCountPerSheet = 10;
    int SheetCount = Convert.ToInt32(Math.Ceiling((double)GridView1.Rows.Count / ItenCountPerSheet));

    String ExportFileName = "孟宪会Excel表格测试";
    if (Request.Browser.Browser.IndexOf("MSIE") > -1)
    {
      ExportFileName = Server.UrlEncode(ExportFileName);
    }
    Response.ClearContent();
    Response.BufferOutput = true;
    Response.Charset = "utf-8";
    Response.ContentType = "text/xml";
    Response.ContentEncoding = System.Text.Encoding.UTF8;
    //Response.AppendHeader("Content-Disposition", "attachment;filename=" + ExportFileName + ".xlsx");
    // 采用下面的格式,将兼容 Excel 2003,Excel 2007, Excel 2010。
    Response.AppendHeader("Content-Disposition", "attachment;filename="+Server.UrlEncode("孟宪会Excel表格测试")+".xml");
    Response.Write("<?xml version='1.0'?><?mso-application progid='Excel.Sheet'?>");
    Response.Write(@"\r\n<Workbook xmlns='urn:schemas-microsoft-com:office:spreadsheet'
      xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel'
      xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet' xmlns:html='http://www.w3.org/TR/REC-html40'>");
    Response.Write(@"\r\n<DocumentProperties xmlns='urn:schemas-microsoft-com:office:office'>");
    Response.Write(@"\r\n<Author>孟宪会</Author><LastAuthor>孟子E章</LastAuthor>
          <Created>2010-09-08T14:07:11Z</Created><Company>mxh</Company><Version>1990</Version>");
    Response.Write("\r\n</DocumentProperties>");
    Response.Write(@"\r\n<Styles><Style ss:ID='Default' ss:Name='Normal'><Alignment ss:Vertical='Center'/>
      <Borders/><Font ss:FontName='宋体' x:CharSet='134' ss:Size='12'/><Interior/><NumberFormat/><Protection/></Style>"