日期:2014-05-17  浏览次数:20583 次

asp.net gridview导出excel 添加行
好比gridview中显示3行3列,我想要的是导出到excel后显示成5行3列,并且设置打印的默认纸张为A3

------解决方案--------------------
例子,直接拷贝测试
HTML code
<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
 
  public System.Data.DataTable GetData()
  {
    System.Data.DataTable dataTable1 = new System.Data.DataTable("BlogUser");
    dataTable1.Columns.Add(new System.Data.DataColumn("UserId", typeof(System.Int32)));
    dataTable1.Columns.Add(new System.Data.DataColumn("Title", typeof(System.String)));
    dataTable1.Columns.Add(new System.Data.DataColumn("Test", typeof(System.String)));

    dataTable1.Rows.Add(new Object[] { 11, 22, 33 });
    dataTable1.Rows.Add(new Object[] { 111, 2222, 3333 });
    dataTable1.Rows.Add(new Object[] { 11111, 22222, 33333 });
    return dataTable1;
  }
  protected void Page_Load(object sender, EventArgs e)
  {

    GridView1.DataSource = GetData();
    GridView1.DataBind();
  }

  protected void Button1_Click(object sender, EventArgs e)
  {

    Response.ClearContent();
    Response.BufferOutput = true;
    Response.Charset = "utf-8";
    Response.ContentType = "application/ms-excel";
    Response.AddHeader("Content-Transfer-Encoding", "binary");
    Response.ContentEncoding = System.Text.Encoding.UTF8;

    String FileName = "孟宪会Excel表格测试";
    if (!String.IsNullOrEmpty(Request.UserAgent))
    {
      // firefox 里面文件名无需编码。
      if (!(Request.UserAgent.IndexOf("Firefox") > -1 && Request.UserAgent.IndexOf("Gecko") > -1))
      {
        FileName = Server.UrlEncode(FileName);
      }
    }
    Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName + ".xls");
    Response.Write("<?xml version='1.0'?><?mso-application progid='Excel.Sheet'?>");
    Response.Write(@"<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(@"<DocumentProperties xmlns='urn:schemas-microsoft-com:office:office'>");
    Response.Write(@"<Author>孟宪会</Author><LastAuthor>孟子E章</LastAuthor>
          <Created>2010-09-08T14:07:11Z</Created><Company>mxh</Company><Version>1990</Version>");
    Response.Write("</DocumentProperties>");
    Response.Write(@"<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>");
    //定义标题样式    
    Response.Write(@"<Style ss:ID='Header'><Borders><Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='1'/>
       <Border ss:Position='Left' ss:LineStyle='Continuous' ss:Weight='1'/>
       <Border ss:Position='Right' ss:LineStyle='Continuous' ss:Weight='1'/>
       <Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='1'/></Borders>
       <Font ss:FontName='宋体' x:CharSet='134' ss:Size='18' ss:Color='#FF0000' ss:Bold='1'/></Style>");

    //定义边框
    Response.Write(@"<Style ss:ID='border'><NumberFormat ss:Format='@'/><Borders>
      <Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='1'/>
      <Border ss:Position='Left' ss:LineStyle='Continuous' ss:Weight='1'/