#region 导出
Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("题目", Encoding.UTF8) + ".xls");
Response.ContentEncoding = System.Text.Encoding.Default;
Response.ContentType = "application/vnd.ms-excel";//"application/vnd.ms-word";
Response.Charset = "";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
int CommissionNum = 0;
var list = ...获取数据对象;
if (list.Count > 0)
{
sb.Append("类型\t"); sb.Append("知识类型\t"); sb.Append("会员类型\t"); sb.Append("题目\t");
sb.Append("版块\t"); sb.Append("学习内容\t");
sb.Append(Environment.NewLine);
foreach (var Info in list)
{
sb.Append(Info.ProblemsFormat + "\t");
sb.Append(Info.StudyTypeName + "\t");
sb.Append(Info.MemberTypeFormat + "\t");
//避免错位
sb.Append(Info.QuestionsContent.Replace("\t","").Replace("\r","").Replace(" ","").Replace("\n", "") + "\t");
sb.Append(Info.TypeName + "\t");
sb.Append("\t");
sb.Append(Environment.NewLine);
}
Response.Write(sb.ToString());
Response.End();
}
#endregion
简单的导出是可以的,而且体量很小,但是出现一些特别的,如银行卡号,流水号,一些数字编号等,导出就会出现科学计数法了,这就很尴尬了。
最后改成table输出
sb.Append("<style> td { border:.5pt solid black; }</style>");
sb.Append("<table class='.border'><tr>"); //style = 'border:1px solid #000000'
sb.Append("<td>收入公司</td>"); sb.Append("<td>收入门店</td>"); sb.Append("<td>收入部门</td>"); sb.Append("<td>卡号</td>");
sb.Append("<td>支出公司</td>"); sb.Append("<td>支出门店</td>"); sb.Append("<td>支出部门</td>"); sb.Append("<td>卡号</td>");
sb.Append("<td>产生源</td>"); sb.Append("<td>合同编号</td>"); sb.Append("<td>金额(元)</td>"); sb.Append("<td>时间</td>");
sb.Append("<td>审核状态</td>"); sb.Append("<td>支付状态</td>"); sb.Append("<td>流水号</td>"); sb.Append("<td>备注</td>");
sb.Append("</tr>");
foreach (var Info in list)
{
sb.Append("<tr>");
sb.Append("<td>" + Info.IncomeCompanyName + "</td>");
sb.Append("<td>" + Info.IncomeBranchName + "</td>");
sb.Append("<td>" + Info.IncomeDepartmentName + "</td>");
sb.Append("<td style='vnd.ms-excel.numberformat:@'>" + Info.IncomeCard + "</td></td>");
sb.Append("<td>" + Info.PaymentCompanyName + "</td>");
sb.Append("<td>" + Info.PaymentBranchName + "</td>");
sb.Append("<td>" + Info.PaymentDepartmentName + "</td>");
sb.Append("<td style='vnd.ms-excel.numberformat:@'>" + Info.PaymentCard + "</td></td>");
sb.Append("<td>" + Info.ProduceSourceFormart + "</td>");
sb.Append("<td style='vnd.ms-excel.numberformat:@'>" + Info.ContractNo + " </td>");
sb.Append("<td>" + Info.Amount + "</td>");
sb.Append("<td>" + Info.CreateTimeFormart + "</td>");
sb.Append("<td>" + Info.AuditStatusFormart + "</td>");
sb.Append("<td>" + Info.PaymentStatusFormart + "</td>");
sb.Append("<td style='vnd.ms-excel.numberformat:@'>" + Info.PaymentSerialNumber + "</td>");
sb.Append("<td>" + Info.Remark.Replace("\r", "").Replace("\n", "") + "</td>");
sb.Append("</tr>");
}
sb.Append("</table>");
Response.Write(sb.ToString());
Response.End();
可以像HTML一样加样式。td { border:.5pt solid black; }所以td就有样式了
单元格格式化:
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%
解决了科学计数法问题
图片的插入:
在td中插入img标签,需要设置图片大小和路径,但是不能加单位px
否则导出图片会没有大小,是原始尺寸。
输出到服务器中:
StringBuilder sb = new StringBuilder();
sb.Append("<style> td { border:.5pt solid black; }</style>");
sb.Append("<table class='.border'><tr>"); //style = 'border:1px solid #000000'
sb.Append("<td>收入公司</td>"); sb.Append("<td>收入门店</td>"); sb.Append("<td>收入部门</td>"); sb.Append("<td>卡号</td>");
sb.Append("<td>支出公司</td>"); sb.Append("<td>支出门店</td>"); sb.Append("<td>支出部门</td>"); sb.Append("<td>卡号</td>");
sb.Append("<td>产生源</td>"); sb.Append("<td>合同编号</td>"); sb.Append("<td>金额(元)</td>"); sb.Append("<td>时间</td>");
sb.Append("<td>审核状态</td>"); sb.Append("<td>支付状态</td>"); sb.Append("<td>流水号</td>"); sb.Append("<td>备注</td>");
sb.Append("</tr>");
sb.Append("<tr>");
sb.Append("<td>Info.IncomeCompanyName</td>");
sb.Append("<td>Info.IncomeBranchName</td>");
sb.Append("<td>Info.IncomeDepartmentName</td>");
sb.Append("<td style='vnd.ms-excel.numberformat:@'>Info.IncomeCard</td></td>");
sb.Append("<td>Info.PaymentCompanyName</td>");
sb.Append("<td>Info.PaymentBranchName</td>");
sb.Append("<td>Info.PaymentDepartmentName</td>");
sb.Append("<td style='vnd.ms-excel.numberformat:@'>Info.PaymentCard</td></td>");
sb.Append("<td>Info.ProduceSourceFormart</td>");
sb.Append("<td style='vnd.ms-excel.numberformat:@'>Info.ContractNo + (Info.IsBack ? \"[退]\" : \"\")</td>");
sb.Append("<td>Info.Amount</td>");
sb.Append("<td>Info.CreateTimeFormart</td>");
sb.Append("<td>Info.AuditStatusFormart</td>");
sb.Append("<td>Info.PaymentStatusFormart</td>");
sb.Append("<td style='vnd.ms-excel.numberformat:@'>Info.PaymentSerialNumber</td>");
sb.Append("<td>Info.Remark</td>");
sb.Append("</tr>");
sb.Append("</table>");
for (int i = 0; i < 20; i++)
{
//string File = @"D:\Upfiles\" + i + ".xls";
string File = Server.MapPath("/Upload/") + i + ".xls"; ;
//设置新建文件路径及名称
string savePath = File;
//创建文件
FileStream file = new FileStream(savePath, FileMode.CreateNew, FileAccess.Write);
//以指定的字符编码向指定的流写入字符
StreamWriter sw = new StreamWriter(file, Encoding.GetEncoding("UTF-8"));//GetEncoding("GB2312")
加入换行字符串
//strbu.Append(Environment.NewLine);
sw.Write(sb.ToString());
sw.Flush();
file.Flush();
sw.Close();
sw.Dispose();
file.Close();
file.Dispose();
}