asp.net gridview导出excel 添加行

tony312ws 2012-09-08 03:49:19
好比gridview中显示3行3列,我想要的是导出到excel后显示成5行3列,并且设置打印的默认纸张为A3
...全文
114 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2012-09-08
  • 打赏
  • 举报
回复
例子,直接拷贝测试
<%@ 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'/>
<Border ss:Position='Right' ss:LineStyle='Continuous' ss:Weight='1'/>
<Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='1'/></Borders></Style>");

Response.Write(@"<Style ss:ID=""s23""><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><Alignment ss:Horizontal=""Right"" ss:Vertical=""Center""/></Style>");
Response.Write(@"<Style ss:ID='HeaderRight'><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'/><Alignment ss:Horizontal=""Center"" ss:Vertical=""Center""/></Style>");

Response.Write("</Styles>");

Response.Write("<Worksheet ss:Name='Sheet1'>");
Response.Write("<Table x:FullColumns='1' x:FullRows='1'>");
//输出标题
Response.Write("<Row><Cell ss:MergeAcross='2' ss:StyleID='HeaderRight'><Data ss:Type='String'>标题</Data></Cell></Row>");
Response.Write("<Row ss:AutoFitHeight='1'>");

Response.Write("<Cell ss:StyleID='Header'><Data ss:Type='String'>列1</Data></Cell>");
Response.Write("<Cell ss:StyleID='Header'><Data ss:Type='String'>列2</Data></Cell>");
Response.Write("<Cell ss:StyleID='Header'><Data ss:Type='String'>列3</Data></Cell>");

Response.Write("\r\n</Row>");

System.Data.DataTable dt = this.GetData();
for (int j = 0; j < dt.Rows.Count; j++)
{
Response.Write("<Row>");
for (int c = 0; c < 3; c++)
{
//对于数字,采用Number数字类型
if (c > 1)
{
Response.Write("<Cell ss:StyleID='border'><Data ss:Type='Number'>" + dt.Rows[j][c].ToString() + "</Data></Cell>");
}
else
{
Response.Write("<Cell ss:StyleID='border'><Data ss:Type='String'>" + dt.Rows[j][c].ToString() + "</Data></Cell>");
}
}
Response.Write("</Row>");
}
Response.Write("<Row><Cell ss:MergeAcross='2' ss:StyleID='s23'><Data ss:Type='String'>2010年9月8日</Data></Cell></Row>");
Response.Write("</Table>");
Response.Write("</Worksheet>");
Response.Flush();

Response.Write("</Workbook>");
Response.End();
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出" />
</form>
</body>
</html>
tony312ws 2012-09-08
  • 打赏
  • 举报
回复
孟子E章 2012-09-08
  • 打赏
  • 举报
回复
不要再这里显示你计算机上的图片,要先上传到你空间的相册中
tony312ws 2012-09-08
  • 打赏
  • 举报
回复

就像这样子的
孟子E章 2012-09-08
  • 打赏
  • 举报
回复
显示成5行3列,把数据添加到数据源中就可以了,
打印的默认纸张为A3
这个是不能设置的

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧