首页 技术 正文
技术 2022年11月20日
0 收藏 869 点赞 4,304 浏览 5511 个字

下面介绍一种导出Excel的方法:

此方法不需要在服务器上安装Excel,采用生成xml以excel方式输出到客户端,可能需要客户机安装excel,所以也不会有乱七八糟的权限设定,和莫名其妙的版本问题。而且此种方法比第二种更快试了下10W条数据(20列)不到10秒(小白pc)。

protected void Btn_Up(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/vnd.ms-excel";
this.EnableViewState = false; Hashtable ht = new Hashtable();
try
{
ht["PayAcc"] = payeeaccno.Value;
ht["BgDate"] = rzbDate.Value.Replace("-", "");
ht["EdDate"] = rzEDate.Value.Replace("-", "");
ht["BizBrid"] = bizBrandid;
}
catch (Exception)
{
throw;
} WithholdingQueryBll wiBill = new WithholdingQueryBll();//数据操作类
int count = ;
DataTable qdp = wiBill.QuerDeductPayee(ht, true, out count);//查询数据,返回DataTable
CreateExcel(qdp, "", "xls");
}public void CreateExcel(System.Data.DataTable dt, String TableTitle, string fileName)
{
//1) 文本:vnd.ms-excel.numberformat:@
//2) 日期:vnd.ms-excel.numberformat:yyyy/mm/dd
//3) 数字:vnd.ms-excel.numberformat:#,##0.00
//4) 货币:vnd.ms-excel.numberformat:¥#,##0.00
//5) 百分比:vnd.ms-excel.numberformat: #0.00%
StringBuilder strb = new StringBuilder();
DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
strb.Append(" <html xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
strb.Append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
strb.Append("xmlns=\" strb.Append(\"<head> <meta http-equiv='Content-Type' content='text/html; charset=gb2312'>");
strb.Append(" <style>");
strb.Append("body");
strb.Append(" {mso-style-parent:style0;");
strb.Append(" font-family:\"Times New Roman\", serif;");
strb.Append(" mso-font-charset:0;");
strb.Append(" mso-number-format:\"@\";}");
strb.Append("table");
strb.Append(" {border-collapse:collapse;margin:1em 0;line-height:20px;font-size:12px;color:#222; margin:0px;}");
strb.Append("thead tr td");
strb.Append(" {background-color:#e3e6ea;color:#6e6e6e;text-align:center;font-size:14px;}");
strb.Append("tbody tr td");
strb.Append(" {font-size:12px;color:#666;}");
strb.Append(" </style>");
strb.Append(" <xml>");
strb.Append(" <x:ExcelWorkbook>");
strb.Append(" <x:ExcelWorksheets>");
strb.Append(" <x:ExcelWorksheet>");
strb.Append(" <x:Name>Sheet1 </x:Name>");
strb.Append(" <x:WorksheetOptions>");
strb.Append(" <x:DefaultRowHeight>285 </x:DefaultRowHeight>");
strb.Append(" <x:Selected/>");
strb.Append(" <x:Panes>");
strb.Append(" <x:Pane>");
strb.Append(" <x:Number>3 </x:Number>");
strb.Append(" <x:ActiveCol>1 </x:ActiveCol>");
strb.Append(" </x:Pane>");
strb.Append(" </x:Panes>");
strb.Append(" <x:ProtectContents>False </x:ProtectContents>");
strb.Append(" <x:ProtectObjects>False </x:ProtectObjects>");
strb.Append(" <x:ProtectScenarios>False </x:ProtectScenarios>");
strb.Append(" </x:WorksheetOptions>");
strb.Append(" </x:ExcelWorksheet>");
strb.Append(" <x:WindowHeight>6750 </x:WindowHeight>");
strb.Append(" <x:WindowWidth>10620 </x:WindowWidth>");
strb.Append(" <x:WindowTopX>480 </x:WindowTopX>");
strb.Append(" <x:WindowTopY>75 </x:WindowTopY>");
strb.Append(" <x:ProtectStructure>False </x:ProtectStructure>");
strb.Append(" <x:ProtectWindows>False </x:ProtectWindows>");
strb.Append(" </x:ExcelWorkbook>");
strb.Append(" </xml>");
strb.Append("");
strb.Append(" </head> <body> ");
//strb.Append(" <table> <thead><tr>");
//strb.Append(" <td colspan=\"" + ds.Columns.Count + "\" >");
//strb.Append(TableTitle);
//strb.Append(" </td> ");
//strb.Append(" </tr>");
//strb.Append(" </thead><tbody><tr>");
strb.Append(" <table> <tbody><tr>");
//写列标题
strb.Append(" <td> <b>" + "交易日期" + " </b> </td>");
strb.Append(" <td> <b>" + "交易时间" + " </b> </td>");
strb.Append(" <td> <b>" + "银行流水号" + " </b> </td>");
strb.Append(" <td> <b>" + "发起方(第三方)流水号" + " </b> </td>");
strb.Append(" <td> <b>" + "收款人账号" + " </b> </td>");
strb.Append(" <td> <b>" + "收款人名称" + " </b> </td>");
strb.Append(" <td> <b>" + "付款人账号" + " </b> </td>");
strb.Append(" <td> <b>" + "付款人名称" + " </b> </td>");
strb.Append(" <td> <b>" + "付款人开户行" + " </b> </td>");
strb.Append(" <td> <b>" + "交易金额" + " </b> </td>");
strb.Append(" <td> <b>" + "交易标志" + " </b> </td>");
strb.Append(" <td> <b>" + "结果信息" + " </b> </td>");
//strb.Append(" <td> <b>" + "付款人户名" + " </b> </td>");
//strb.Append(" <td> <b>" + "成功/失败原因" + " </b> </td>");
strb.Append(" </tr>");
//根据查询出来的字段遍历数据
foreach (DataRow row in myRow)
{
strb.Append(" <tr>");
strb.Append(" <td>" + row["TRANDATE"].ToString() + " </td>");
strb.Append(" <td>" + row["TRANTIME"].ToString() + " </td>");
strb.Append(" <td style=\"vnd.ms-excel.numberformat:@\">" + row["TRANSEQNO"].ToString() + " </td>");
strb.Append(" <td style=\"vnd.ms-excel.numberformat:@\">" + row["THSEQNO"].ToString() + " </td>");
strb.Append(" <td style=\"vnd.ms-excel.numberformat:@\">" + row["PAYEENO"].ToString() + " </td>");
strb.Append(" <td>" + row["PAYEENAME"].ToString() + " </td>");
strb.Append(" <td style=\"vnd.ms-excel.numberformat:@\">" + row["PAYERNO"].ToString() + " </td>");
strb.Append(" <td>" + row["PAYERNAME"].ToString() + " </td>");
strb.Append(" <td style=\"vnd.ms-excel.numberformat:@\">" + row["PAYERBANK"].ToString() + " </td>");
strb.Append(" <td style=\"vnd.ms-excel.numberformat:¥#,##0.00\">" + row["TRANAMT"].ToString() + " </td>");
strb.Append(" <td>" + row["TRANSTAT"].ToString() + " </td>");
strb.Append(" <td>" + row["REMSG"].ToString() + " </td>");
//strb.Append(" <td>" + row["PAYER_NAME"].ToString() + " </td>");
//strb.Append(" <td>" + row["RET_MSG"].ToString() + " </td>");
strb.Append(" </tr>");
} strb.Append(" </tbody> </table>");
strb.Append(" </body> </html>");
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
//Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//设置输出流为简体中文
Response.ContentType = "application/ms-excel";
//设置输出文件类型为excel文件。
this.EnableViewState = false;
Response.Write(strb);
Response.End();
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,904
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,430
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,727