+ -
当前位置:首页 → 问答吧 → asp.net导出EXCEL

asp.net导出EXCEL

时间:2011-12-13

来源:互联网

 
  HttpResponse Response = HttpContext.Current.Response;

  #region Default
  Response.Expires = 0;
  Response.Clear();
  Response.Buffer = true;
  Response.Charset = "gb2312";

  //Response.Charset = "UT-F8";
  Response.ContentEncoding = System.Text.Encoding.Default;
  Response.ContentType = "application/vnd.ms-excel";


  //字码转换
  byte[] ExcelName;
  ExcelName = System.Text.Encoding.Default.GetBytes(fileName);
  fileName = System.Text.Encoding.GetEncoding("gb2312").GetString(ExcelName);
  //fileName = System.Web.HttpUtility.UrlEncode("企业信息报表", System.Text.Encoding.UTF8);


  Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", fileName));
  //引用这三个xmlns  
  Response.Write("<html xmlns:o='urn:schemas-microsoft-com:office:office'");
  Response.Write("xmlns:x='urn:schemas-microsoft-com:office:excel'");
  Response.Write("xmlns='http://www.w3.org/TR/REC-html40'>");
  Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");


  //在head中加入xml定义  
  Response.Write("\n <head>");
  Response.Write("\n <xml>");
  Response.Write("\n <x:ExcelWorkbook>");
  Response.Write("\n <x:ExcelWorksheets>");
  Response.Write("\n <x:ExcelWorksheet>");
  Response.Write(string.Format("\n <x:Name>{0}</x:Name>", ExportTableName)); //表名



  //以下针对此工作表进行属性设定
  Response.Write("\n <x:WorksheetOptions>");
  Response.Write("\n <x:FrozenNoSplit/>");

  //设定冻结行号
  Response.Write("\n <x:SplitHorizontal>4</x:SplitHorizontal>");
  //设定卷轴起始行号
  Response.Write("\n <x:TopRowBottomPane>4</x:TopRowBottomPane>");
  Response.Write("\n <x:ActivePane>2</x:ActivePane>");
  Response.Write("\n </x:WorksheetOptions>");
  Response.Write("\n </xml>");
  Response.Write("\n </head>");
  Response.Write("\n <body>");
<body>
<table width="1000" border="1">
  <tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  </tr>
  <tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  </tr>
  <tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  </tr>
  <tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  </tr>
</table>
</body>
  Response.Write("</html>");

如何把这样的数据导入在我指定的模板里面?不需要指定列

作者: xugan666   发布时间: 2011-12-13

C# code
public void OutPutExcel()
        {
            //定义文档类型、字符编码
            Response.Clear();
          Response.Buffer= true;
          Response.Charset="GB2312";
            //下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开
          //filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc    .xls    .txt   .htm
          Response.AppendHeader("Content-Disposition","attachment;filename=FileFlow.xls");
          Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
          //Response.ContentType指定文件类型 可以为application/ms-excel、application/ms-word、application/ms-txt、application/ms-html 或其他浏览器可直接支持文档
          Response.ContentType = "application/ms-excel";
          this.EnableViewState = false;
            // 定义一个输入流
          System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
          System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

            this.RenderControl(oHtmlTextWriter);
          //this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件
          Response.Write(oStringWriter.ToString());
          Response.End();
        }

作者: fengyarongaa   发布时间: 2011-12-13

引用 1 楼 fengyarongaa 的回复:

C# code
public void OutPutExcel()
{
//定义文档类型、字符编码
Response.Clear();
   Response.Buffer= true;
   Response.Charset="GB2312";
//下面这行很重要, attac……
你这个是指定模板了还是???直接输出了?

作者: xugan666   发布时间: 2011-12-13

Response.Charset = "GB2312";
  Response.ContentEncoding = System.Text.Encoding.UTF8;
  Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
  Response.ContentType = FileType;
  this.EnableViewState = false;
  StringWriter tw = new StringWriter();
  HtmlTextWriter hw = new HtmlTextWriter(tw);
  GridView1.RenderControl(hw);
  Response.Write(tw.ToString());
  Response.End();

作者: caiyang910423   发布时间: 2011-12-13

唉··等下一个方法,我这个方法就木有用到gridview这种控件···

作者: xugan666   发布时间: 2011-12-13

你用2楼的 输出就可以了么- -

作者: a67251026   发布时间: 2011-12-13

哦 是1楼- -~

作者: a67251026   发布时间: 2011-12-13

1231
引用 1 楼 fengyarongaa 的回复:

C# code
public void OutPutExcel()
{
//定义文档类型、字符编码
Response.Clear();
   Response.Buffer= true;
   Response.Charset="GB2312";
//下面这行很重要, attac……

作者: xugan666   发布时间: 2011-12-13

b/s下操作Excel的合并单元格有点难
参考我的百度http://hi.baidu.com/gdlpc88/blog/item/3009acf8c04731949e5146dc.html

作者: net5354   发布时间: 2011-12-13

<table width="1000" border="1">
  <tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  </tr>
  <tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  </tr>
  <tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  </tr>
  <tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  </tr>
</table>
如何把这个代码直接导入在我指定的Excel模板里面?有人实现过没有?

作者: xugan666   发布时间: 2011-12-13

你可以尝试用http://npoi.codeplex.com/ 就不用这么麻烦了

作者: xiahouwen   发布时间: 2011-12-13

我比较建议用excel的api来生成excel文件,先用excel做好模版,在代码里面对模版进行赋值

作者: Choice   发布时间: 2011-12-13

api好一点,或者楼主自己封装函数也行!

作者: souleyes   发布时间: 2011-12-13