ASP.NET 导出到Excel时保留换行的代码
程序员文章站
2023-11-13 08:28:28
完整代码: 复制代码 代码如下:<%@ page language...
<br style='mso-data-placement:same-cell;'/>
完整代码:
<%@ page language="c#" trace="false" 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">
protected void button1_click(object sender, eventargs e)
{
system.web.httpcontext curcontext = system.web.httpcontext.current;
// io用于导出并返回excel文件
system.io.stringwriter strwriter = null;
system.web.ui.htmltextwriter htmlwriter = null;
// 设置编码和附件格式
httpcontext.current.response.clear();
httpcontext.current.response.buffer = true;
httpcontext.current.response.addheader("content-disposition", string.format("attachment; filename={0}", "aaa.xls"));
curcontext.response.contenttype = "application/vnd.ms-excel";
curcontext.response.contentencoding = system.text.encoding.getencoding("gb18030");
curcontext.response.charset = "";
// 导出excel文件
strwriter = new system.io.stringwriter();
htmlwriter = new system.web.ui.htmltextwriter(strwriter);
htmlwriter.writeline("标题");
// 返回客户端
gridview1.rendercontrol(htmlwriter);
curcontext.response.write(strwriter.tostring().replace("<br/>", "<br style='mso-data-placement:same-cell;'/> "));
curcontext.response.end();
}
protected void page_load(object sender, eventargs e)
{
if (!page.ispostback)
{
gridview1.datasource = createdatasourcebyxianhuimeng();
gridview1.databind();
}
}
system.data.dataview createdatasourcebyxianhuimeng()
{
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)));
for (int i = 0; i < 8; i++)
{
system.random rd = new system.random(environment.tickcount * i); ;
dr = dt.newrow();
dr[0] = "班级" + i.tostring();
dr[1] = "学生姓名:孟子e章" + i.tostring() + "<br/>所在班级:" + "班级" + i.tostring();
dr[2] = system.math.round(rd.nextdouble() * 100, 2);
dr[3] = system.math.round(rd.nextdouble() * 100, 2);
dr[4] = system.math.round(rd.nextdouble() * 100, 2);
dr[5] = system.math.round(rd.nextdouble() * 100, 2);
dt.rows.add(dr);
}
system.data.dataview dv = new system.data.dataview(dt);
return dv;
}
public override void verifyrenderinginserverform(control control)
{ }
</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" autogeneratecolumns="false">
<columns>
<asp:boundfield htmlencode="false" datafield="学生姓名" headertext="测试字段" />
</columns>
</asp:gridview>
<asp:button id="button1" runat="server" onclick="button1_click" text="button" />
</form>
</body>
</html>
完整代码:
复制代码 代码如下:
<%@ page language="c#" trace="false" 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">
protected void button1_click(object sender, eventargs e)
{
system.web.httpcontext curcontext = system.web.httpcontext.current;
// io用于导出并返回excel文件
system.io.stringwriter strwriter = null;
system.web.ui.htmltextwriter htmlwriter = null;
// 设置编码和附件格式
httpcontext.current.response.clear();
httpcontext.current.response.buffer = true;
httpcontext.current.response.addheader("content-disposition", string.format("attachment; filename={0}", "aaa.xls"));
curcontext.response.contenttype = "application/vnd.ms-excel";
curcontext.response.contentencoding = system.text.encoding.getencoding("gb18030");
curcontext.response.charset = "";
// 导出excel文件
strwriter = new system.io.stringwriter();
htmlwriter = new system.web.ui.htmltextwriter(strwriter);
htmlwriter.writeline("标题");
// 返回客户端
gridview1.rendercontrol(htmlwriter);
curcontext.response.write(strwriter.tostring().replace("<br/>", "<br style='mso-data-placement:same-cell;'/> "));
curcontext.response.end();
}
protected void page_load(object sender, eventargs e)
{
if (!page.ispostback)
{
gridview1.datasource = createdatasourcebyxianhuimeng();
gridview1.databind();
}
}
system.data.dataview createdatasourcebyxianhuimeng()
{
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)));
for (int i = 0; i < 8; i++)
{
system.random rd = new system.random(environment.tickcount * i); ;
dr = dt.newrow();
dr[0] = "班级" + i.tostring();
dr[1] = "学生姓名:孟子e章" + i.tostring() + "<br/>所在班级:" + "班级" + i.tostring();
dr[2] = system.math.round(rd.nextdouble() * 100, 2);
dr[3] = system.math.round(rd.nextdouble() * 100, 2);
dr[4] = system.math.round(rd.nextdouble() * 100, 2);
dr[5] = system.math.round(rd.nextdouble() * 100, 2);
dt.rows.add(dr);
}
system.data.dataview dv = new system.data.dataview(dt);
return dv;
}
public override void verifyrenderinginserverform(control control)
{ }
</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" autogeneratecolumns="false">
<columns>
<asp:boundfield htmlencode="false" datafield="学生姓名" headertext="测试字段" />
</columns>
</asp:gridview>
<asp:button id="button1" runat="server" onclick="button1_click" text="button" />
</form>
</body>
</html>
要查阅 excel 中使用的样式规范,请参考:microsoft® office html and xml reference。
下载地址:
上一篇: 智能机器人“上岗”手术无需“换乘”(图)
下一篇: 想抱走:这款可爱的自动驾驶机器人能送外卖