C#关于导出excel的方法
标签:style blog http io color ar os 使用 sp
一说到导出excel可能很多人都会觉得说直接利用npoi 导入导出excel格式的文件,那样非常方便,但是可能有些时候有的浏览器不支持,那么该怎么办呢,现在介绍一种纯C#的导出excel的方法,代码如下:
1 public void excel_output1(DataTable dt)
2 {
3 Response.Clear();
4
5 Response.Buffer = true;
6
7 Response.Charset = "utf-8 ";
8 Response.AddHeader("Content-Disposition", "attachment;filename=" +
9 HttpUtility.UrlEncode(DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls", System.Text.Encoding.UTF8));
10 Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
11 Response.ContentType = "application/ms-excel";
12 this.EnableViewState = false;
13 System.IO.StringWriter sw = new System.IO.StringWriter();
14
15 sw.WriteLine("");
16 sw.WriteLine("");
17 sw.WriteLine("");
18 sw.WriteLine(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
19 sw.WriteLine(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
20 sw.WriteLine(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
21 sw.WriteLine(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">");
22 sw.WriteLine(" ");
23 sw.WriteLine(" 1996-12-17T01:32:42Z");
24 sw.WriteLine(" 2011-05-29T05:59:50Z");
25 sw.WriteLine(" 11.6360");
26 sw.WriteLine(" ");
27 sw.WriteLine(" ");
28 sw.WriteLine(" ");
29 sw.WriteLine(" ");
30 sw.WriteLine(" ");
31 sw.WriteLine(" 4530");
32 sw.WriteLine(" 8505");
33 sw.WriteLine(" 480");
34 sw.WriteLine(" 120");
35 sw.WriteLine(" ");
36 sw.WriteLine(" False");
37 sw.WriteLine(" False");
38 sw.WriteLine(" ");
39 sw.WriteLine(" ");
40 sw.WriteLine(" ");
41 sw.WriteLine(" ");
42 sw.WriteLine(" ");
43 sw.WriteLine(" ");
44 sw.WriteLine(" ");
45 sw.WriteLine(" ");
46 sw.WriteLine(" ");
47 sw.WriteLine(" ");
48 sw.WriteLine(" ");
49 sw.WriteLine(" ");
50 sw.WriteLine(" ");
51 sw.WriteLine(" ");
52 sw.WriteLine(" ");
53 sw.WriteLine(" ");
54 sw.WriteLine(" ");
55 sw.WriteLine(" ");
56 sw.WriteLine(" ");
57 sw.WriteLine(" ");
58 sw.WriteLine(" ");
59 sw.WriteLine(" ");
60 sw.WriteLine(" ");
61 sw.WriteLine(" ");
62 sw.WriteLine(" ");
63 sw.WriteLine(" ");
64 sw.WriteLine(" ");
65 sw.WriteLine(" ");
66 sw.WriteLine(" ");
67 sw.WriteLine(" ");
68 sw.WriteLine(" ");
69 sw.WriteLine(" ");
70 sw.WriteLine(" ");
71 sw.WriteLine(" ");
72 sw.WriteLine(" ");
73 sw.WriteLine(" ");
74 sw.WriteLine(" ");
75 sw.WriteLine(" ");
76 sw.WriteLine(" ");
77 sw.WriteLine(" ");
78 sw.WriteLine(" ");
79 sw.WriteLine(" ");
80 sw.WriteLine(" ");
81 sw.WriteLine(" ");
82 sw.WriteLine(" ");
83 sw.WriteLine(" ");
84 sw.WriteLine(" ");
85 sw.WriteLine(" ");
86 sw.WriteLine(" ");
87 sw.WriteLine(" ");
88 sw.WriteLine(" ");
89 sw.WriteLine(" ");
90 sw.WriteLine(" ");
91 sw.WriteLine(" ");
92 sw.WriteLine(" ");
93 sw.WriteLine(" ");
94 sw.WriteLine(" ");
95 sw.WriteLine(" ");
96 sw.WriteLine(" ");
97 sw.WriteLine(" ");
98 //sw.WriteLine("
99
100 string dt_str = "
";
101 int dt_rCount = dt.Rows.Count + 5;
102 dt_str += dt_rCount.ToString() + "\" x:FullColumns=\"1\"";
103 sw.WriteLine(dt_str);
104
105
106 sw.WriteLine(" x:FullRows=\"1\" ss:DefaultColumnWidth=\"54\" ss:DefaultRowHeight=\"14.25\">");
107
108 sw.WriteLine(" ");
109 sw.WriteLine(" ");
110 sw.WriteLine(" ");
111 sw.WriteLine(" ");
112 sw.WriteLine(" ");
113 sw.WriteLine(" ");
114 sw.WriteLine(" ");
115 sw.WriteLine(" ");
116 sw.WriteLine(" ");
117 sw.WriteLine("
");
118 sw.WriteLine(" ");
119 sw.WriteLine(" ss:Type=\"String\">回访详单 | ");
120 sw.WriteLine(" ");
121 sw.WriteLine("
");
122 sw.WriteLine(" 营业区 | ");
123 sw.WriteLine(" 任务总数 | ");
124 sw.WriteLine(" 明确表示不使用 | ");
125 sw.WriteLine(" 迁移无线 | ");
126 sw.WriteLine(" 改网通 | ");
127 sw.WriteLine(" 改电信 | ");
128 sw.WriteLine(" 联系不上的1个月以上无上网记录 | ");
129 sw.WriteLine(" 联系不上有上网记录 | ");
130 sw.WriteLine(" 对网络不满意 | ");
131 sw.WriteLine(" 对服务不满意 | ");
132 sw.WriteLine(" 考虑中 | ");
133 sw.WriteLine(" 已续费 | ");
134 sw.WriteLine(" ");
135
136 int cs_i = 1;
137 DataRow[] dt_myRow = dt.Select("");
138
139 foreach (DataRow dt_row in dt_myRow)
140 {
141 sw.WriteLine("
");
142
143 sw.WriteLine(" | " + dt_row[0].ToString() + "");
144 sw.WriteLine(" | " + dt_row[1].ToString() + "");
145 sw.WriteLine(" | " + dt_row[2].ToString() + "");
146 sw.WriteLine(" | " + dt_row[2].ToString() + "");
147 sw.WriteLine(" | " + dt_row[4].ToString() + "");
148 sw.WriteLine(" | " + dt_row[5].ToString() + "");
149 sw.WriteLine(" | " + dt_row[6].ToString() + "");
150 sw.WriteLine(" | " + dt_row[7].ToString() + "");
151 sw.WriteLine(" | " + dt_row[8].ToString() + "");
152 sw.WriteLine(" | " + dt_row[9].ToString() + "");
153 sw.WriteLine(" | " + dt_row[10].ToString() + "");
154 sw.WriteLine(" | " + dt_row[11].ToString() + "");
155 //sw.WriteLine(" " + dt_row[12].ToString() + " | ");
156 sw.WriteLine(" ");
157 cs_i++;
158 }
159
160
161
162 //sw.WriteLine(" ");
163 //sw.WriteLine(" 合计 | ");
164 //sw.WriteLine(" 1 | ");
165 //sw.WriteLine(" | ");
166 //sw.WriteLine(" ");
167 sw.WriteLine("
");
168 sw.WriteLine(" ");
169 sw.WriteLine(" ");
170 sw.WriteLine(" ");
171 sw.WriteLine(" 9");
172 sw.WriteLine(" 600");
173 sw.WriteLine(" 600");
174 sw.WriteLine(" ");
175 sw.WriteLine(" ");
176 sw.WriteLine(" ");
177 sw.WriteLine(" ");
178 sw.WriteLine(" 3");
179 sw.WriteLine(" 8");
180 sw.WriteLine(" 12");
181 sw.WriteLine(" ");
182 sw.WriteLine(" ");
183 sw.WriteLine(" False");
184 sw.WriteLine(" False");
185 sw.WriteLine(" ");
186 sw.WriteLine(" ");
187 sw.WriteLine(" ");
188 sw.WriteLine("
");
189 sw.WriteLine(" x:FullRows=\"1\" ss:DefaultColumnWidth=\"54\" ss:DefaultRowHeight=\"14.25\"/>");
190 sw.WriteLine(" ");
191 sw.WriteLine(" False");
192 sw.WriteLine(" False");
193 sw.WriteLine(" ");
194 sw.WriteLine(" ");
195 sw.WriteLine(" ");
196 sw.WriteLine("
");
197 sw.WriteLine(" x:FullRows=\"1\" ss:DefaultColumnWidth=\"54\" ss:DefaultRowHeight=\"14.25\"/>");
198 sw.WriteLine(" ");
199 sw.WriteLine(" False");
200 sw.WriteLine(" False");
201 sw.WriteLine(" ");
202 sw.WriteLine(" ");
203 sw.WriteLine("");
204
205 Response.Write(sw);
206 Response.End();
207
208 }
C#关于导出excel的方法
标签:style blog http io color ar os 使用 sp
原文地址:http://www.cnblogs.com/HoverM/p/4085676.html
评论