方法一:导出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, DictionarycellHeaderList, 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: DictionarycellHeaderList = 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: ListrowList = 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, "学校列表");