博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET导出Excel
阅读量:5036 次
发布时间:2019-06-12

本文共 5915 字,大约阅读时间需要 19 分钟。

方法一:导出CSV格式

优点:各种Excel版本下都不会提示版本兼容问题

缺点:不能设置每列的宽度,另外有中文的情况下会乱码,试了改变输出的编码,还是不管用。

1: public static StringBuilder AppendCSVFields(StringBuilder argSource, string argFields)
2: {
3:     return argSource.Append(argFields.Replace(",", " ").Trim()).Append(",");
4: }
5: 
6: public static void DownloadCSVFile(HttpResponse argResp, StringBuilder argFileStream, string strFileName)
7: {
8:     string strResHeader = "attachment; filename=" + Guid.NewGuid().ToString() + ".csv";
9:     if (!string.IsNullOrEmpty(strFileName))
10:     {
11:         strResHeader = "inline; filename=" + strFileName;
12:     }
13:     argResp.AppendHeader("Content-Disposition", strResHeader);
14:     argResp.ContentType = "application/ms-excel";
15:     argResp.ContentEncoding = Encoding.GetEncoding("GB2312");
16:     argResp.Write(argFileStream);
17:     argResp.End();
18: }

使用示例:

1: StringWriter swCSV = new StringWriter();
2: swCSV.WriteLine("MsgType,MsgNo,ShortText,Status,Priority,Processor,Owner,EnteredDate");
3: foreach (IncidentInfo item in list)
4: {
5:     StringBuilder sbText = new StringBuilder();
6:     sbText = FileHelper.AppendCSVFields(sbText, item.MsgType.ToString());
7:     sbText = FileHelper.AppendCSVFields(sbText, item.MsgNo.ToString());
8:     sbText = FileHelper.AppendCSVFields(sbText, item.ShortText.ToString());
9:     sbText = FileHelper.AppendCSVFields(sbText, item.Status.ToString());
10:     sbText = FileHelper.AppendCSVFields(sbText, item.Priority.ToString());
11:     sbText = FileHelper.AppendCSVFields(sbText, item.Processor.ToString());
12:     sbText = FileHelper.AppendCSVFields(sbText, item.Owner.ToString());
13:     sbText = FileHelper.AppendCSVFields(sbText, item.EnteredDate.ToString(Consts.DATETIMEFORMAT));
14: 
15:     sbText.Remove(sbText.Length - 1, 1);
16: 
17:     swCSV.WriteLine(sbText.ToString());
18: }
19: 
20: FileHelper.DownloadCSVFile(Response, swCSV.GetStringBuilder(), buildName + "_MsgReport.csv");
21: swCSV.Close();

 

方法二:导出xls格式

优点:可以灵活设置各列的宽度

缺点:为了保证客户端只安装了office2003的用户也能打开,设置为xls格式,在高版本的情况下,打开Excel会出现个提示框,降低了用户体验性

1: public static void DownloadXLSFile(HttpResponse argResp, Dictionary
cellHeaderList, List
> cellValueList, string strFileName)
2: {
3:     string _xmlStr = @"
4:   
5:   
6:   xmlns:o='urn:schemas-microsoft-com:office:office'
7:   xmlns:x='urn:schemas-microsoft-com:office:excel'
8:   xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet'
9:   xmlns:html='http://www.w3.org/TR/REC-html40'>
10:   
11:   
aa
12:   
aa
13:   
2007-12-07T06:54:38Z
14:   
WZKJ
15:   
11.5606
16:   
17:   
18:   
5970
19:   
7395
20:   
480
21:   
60
22:   
False
23:   
False
24:   
25:   
26:   
27:   
28:   
29:   
30:   
31:   
32:   
33:   
34:   
35:   
36:   
37:   
38:   
39:   $Table$
40:   
41:   
42:   
43:   
44:   
3
45:   
2
46:   
3
47:   
48:   
49:   
False
50:   
False
51:   
52:   
53:   ";
54: 
55:     StringBuilder sb = new StringBuilder();
56:     int i = 1;
57:     int recordCount = 1;
58:     foreach (var item in cellHeaderList)
59:     {
60:         sb.Append(string.Format("
", i, item.Value));
61:         i++;
62:     }
63: 
64:     sb.Append("
");
65:     foreach (var item in cellHeaderList)
66:     {
67:         sb.Append(string.Format("
{0}
", item.Key));
68:     }
69:     sb.Append("");
70: 
71:     foreach (var item in cellValueList)
72:     {
73:         recordCount = recordCount + 1;
74:         sb.Append("
");
75:         foreach (var cellValue in item)
76:         {
77:             sb.Append(string.Format("
{0}
", cellValue));
78:         }
79:         sb.Append("");
80:     }
81: 
82:     string topString = "
";
83:     string bottomStr = "";
84:     string xxcelXmlStr = _xmlStr.Replace("$Table$", topString + sb.ToString() + bottomStr);
85: 
86:     argResp.Clear();
87:     argResp.Buffer = true;
88:     argResp.AddHeader("Content-Disposition", "attachment;filename=" + strFileName + ".xls");
89:     argResp.ContentEncoding = Encoding.GetEncoding("UTF-8");
90:     argResp.ContentType = "application/ms-excel";
91:     argResp.Output.Write(xxcelXmlStr);
92:     argResp.Flush();
93:     argResp.End();
94: }

使用示例:

1: Dictionary
cellHeaderList = new Dictionary
();
2: cellHeaderList.Add("学校编号", 100);
3: cellHeaderList.Add("学校名称", 200);
4: cellHeaderList.Add("学校类型", 120);
5: cellHeaderList.Add("学校区域", 60);
6: cellHeaderList.Add("学校地址", 200);
7: cellHeaderList.Add("学校邮编", 90);
8: cellHeaderList.Add("征书负责人", 90);
9: cellHeaderList.Add("负责人邮件", 120);
10: cellHeaderList.Add("负责人电话", 120);
11: List
> cellValueList = new List
>();
12: foreach (SchoolEntity item in list)
13: {
14:     StringBuilder sbText = new StringBuilder();
15:     List
rowList = new List
();
16:     rowList.Add(item.Code);
17:     rowList.Add(item.Name);
18:     rowList.Add(item.Type);
19:     rowList.Add(EnumAttributeHelper
.GetDisplayDescriptionByEnum(item.Region));
20:     rowList.Add(item.Address);
21:     rowList.Add(item.PostCode);
22:     rowList.Add(item.Contact);
23:     rowList.Add(item.Email);
24:     rowList.Add(item.Phone);
25:     cellValueList.Add(rowList);
26: }
27: 
28: FileHelper.DownloadXLSFile(Response, cellHeaderList, cellValueList, "学校列表");

转载于:https://www.cnblogs.com/MaoBisheng/archive/2013/04/06/3002587.html

你可能感兴趣的文章
百度编辑器UEditor ASP.NET示例Demo 分类: ASP.NET...
查看>>
JAVA 技术类分享(二)
查看>>
android客户端向服务器发送请求中文乱码的问
查看>>
UOJ#220. 【NOI2016】网格 Tarjan
查看>>
Symfony翻译教程已开课
查看>>
Python模块之pickle(列表,字典等复杂数据类型与二进制文件的转化)
查看>>
通过数据库表反向生成pojo类
查看>>
css_去掉默认样式
查看>>
TensorFlow2.0矩阵与向量的加减乘
查看>>
NOIP 2010题解
查看>>
javascript中的each遍历
查看>>
String中各方法多数情况下返回新的String对象
查看>>
浅谈tcp粘包问题
查看>>
UVA11524构造系数数组+高斯消元解异或方程组
查看>>
排序系列之——冒泡排序、插入排序、选择排序
查看>>
爬虫基础
查看>>
jquery.lazyload延迟加载图片第一屏问题
查看>>
HDU 1011 Starship Troopers (树形DP)
查看>>
手把手教你写DI_1_DI框架有什么?
查看>>
.net常见的一些面试题
查看>>