|
|
@@ -118,65 +118,64 @@ namespace CallCenter.Utility
|
|
118
|
118
|
{
|
|
119
|
119
|
try
|
|
120
|
120
|
{
|
|
121
|
|
- if (dt.Rows.Count > 0)
|
|
|
121
|
+ HSSFWorkbook workbook = new HSSFWorkbook();
|
|
|
122
|
+ ISheet sheet = workbook.CreateSheet("Sheet1");
|
|
|
123
|
+
|
|
|
124
|
+ ICellStyle HeadercellStyle = workbook.CreateCellStyle();
|
|
|
125
|
+ HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
126
|
+ HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
127
|
+ HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
128
|
+ HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
129
|
+ HeadercellStyle.Alignment = HorizontalAlignment.Center;
|
|
|
130
|
+ HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
|
|
|
131
|
+ HeadercellStyle.FillPattern = FillPattern.SolidForeground;
|
|
|
132
|
+ HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
|
|
|
133
|
+
|
|
|
134
|
+ //字体
|
|
|
135
|
+ NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
|
|
|
136
|
+ headerfont.Boldweight = (short)FontBoldWeight.Bold;
|
|
|
137
|
+ headerfont.FontHeightInPoints = 12;
|
|
|
138
|
+ HeadercellStyle.SetFont(headerfont);
|
|
|
139
|
+
|
|
|
140
|
+
|
|
|
141
|
+ //用column name 作为列名
|
|
|
142
|
+ int icolIndex = 0;
|
|
|
143
|
+ IRow headerRow = sheet.CreateRow(0);
|
|
|
144
|
+ if (cols == null || (cols != null && cols.Length == 0))
|
|
122
|
145
|
{
|
|
123
|
|
- HSSFWorkbook workbook = new HSSFWorkbook();
|
|
124
|
|
- ISheet sheet = workbook.CreateSheet("Sheet1");
|
|
125
|
|
-
|
|
126
|
|
- ICellStyle HeadercellStyle = workbook.CreateCellStyle();
|
|
127
|
|
- HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
128
|
|
- HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
129
|
|
- HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
130
|
|
- HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
131
|
|
- HeadercellStyle.Alignment = HorizontalAlignment.Center;
|
|
132
|
|
- HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
|
|
133
|
|
- HeadercellStyle.FillPattern = FillPattern.SolidForeground;
|
|
134
|
|
- HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
|
|
135
|
|
-
|
|
136
|
|
- //字体
|
|
137
|
|
- NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
|
|
138
|
|
- headerfont.Boldweight = (short)FontBoldWeight.Bold;
|
|
139
|
|
- headerfont.FontHeightInPoints = 12;
|
|
140
|
|
- HeadercellStyle.SetFont(headerfont);
|
|
141
|
|
-
|
|
142
|
|
-
|
|
143
|
|
- //用column name 作为列名
|
|
144
|
|
- int icolIndex = 0;
|
|
145
|
|
- IRow headerRow = sheet.CreateRow(0);
|
|
146
|
|
- if (cols == null || (cols != null && cols.Length == 0))
|
|
|
146
|
+ foreach (DataColumn dc in dt.Columns)
|
|
147
|
147
|
{
|
|
148
|
|
- foreach (DataColumn dc in dt.Columns)
|
|
149
|
|
- {
|
|
150
|
|
- ICell cell = headerRow.CreateCell(icolIndex);
|
|
151
|
|
- cell.SetCellValue(dc.ColumnName);
|
|
152
|
|
- cell.CellStyle = HeadercellStyle;
|
|
153
|
|
- icolIndex++;
|
|
154
|
|
- }
|
|
|
148
|
+ ICell cell = headerRow.CreateCell(icolIndex);
|
|
|
149
|
+ cell.SetCellValue(dc.ColumnName);
|
|
|
150
|
+ cell.CellStyle = HeadercellStyle;
|
|
|
151
|
+ icolIndex++;
|
|
155
|
152
|
}
|
|
156
|
|
- else
|
|
|
153
|
+ }
|
|
|
154
|
+ else
|
|
|
155
|
+ {
|
|
|
156
|
+ foreach (string dc in cols)
|
|
157
|
157
|
{
|
|
158
|
|
- foreach (string dc in cols)
|
|
159
|
|
- {
|
|
160
|
|
- ICell cell = headerRow.CreateCell(icolIndex);
|
|
161
|
|
- cell.SetCellValue(dc);
|
|
162
|
|
- cell.CellStyle = HeadercellStyle;
|
|
163
|
|
- icolIndex++;
|
|
164
|
|
- }
|
|
|
158
|
+ ICell cell = headerRow.CreateCell(icolIndex);
|
|
|
159
|
+ cell.SetCellValue(dc);
|
|
|
160
|
+ cell.CellStyle = HeadercellStyle;
|
|
|
161
|
+ icolIndex++;
|
|
165
|
162
|
}
|
|
166
|
|
- ICellStyle cellStyle = workbook.CreateCellStyle();
|
|
167
|
|
-
|
|
168
|
|
- //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
|
|
169
|
|
- cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
|
|
170
|
|
- cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
171
|
|
- cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
172
|
|
- cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
173
|
|
- cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
163
|
+ }
|
|
|
164
|
+ ICellStyle cellStyle = workbook.CreateCellStyle();
|
|
174
|
165
|
|
|
|
166
|
+ //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
|
|
|
167
|
+ cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
|
|
|
168
|
+ cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
169
|
+ cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
170
|
+ cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
171
|
+ cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
175
|
172
|
|
|
176
|
|
- NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
|
|
177
|
|
- cellfont.Boldweight = (short)FontBoldWeight.Normal;
|
|
178
|
|
- cellStyle.SetFont(cellfont);
|
|
179
|
173
|
|
|
|
174
|
+ NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
|
|
|
175
|
+ cellfont.Boldweight = (short)FontBoldWeight.Normal;
|
|
|
176
|
+ cellStyle.SetFont(cellfont);
|
|
|
177
|
+ if (dt.Rows.Count > 0)
|
|
|
178
|
+ {
|
|
180
|
179
|
//建立内容行
|
|
181
|
180
|
int iRowIndex = 0;
|
|
182
|
181
|
foreach (DataRow dr in dt.Rows)
|
|
|
@@ -203,30 +202,30 @@ namespace CallCenter.Utility
|
|
203
|
202
|
{
|
|
204
|
203
|
sheet.AutoSizeColumn(i);
|
|
205
|
204
|
}
|
|
|
205
|
+ }
|
|
|
206
|
+ using (MemoryStream ms = new MemoryStream())
|
|
|
207
|
+ {
|
|
|
208
|
+ workbook.Write(ms);
|
|
206
|
209
|
|
|
207
|
|
- using (MemoryStream ms = new MemoryStream())
|
|
208
|
|
- {
|
|
209
|
|
- workbook.Write(ms);
|
|
210
|
|
-
|
|
211
|
|
- HttpContext curContext = HttpContext.Current;
|
|
|
210
|
+ HttpContext curContext = HttpContext.Current;
|
|
212
|
211
|
|
|
213
|
212
|
|
|
214
|
|
- // 设置编码和附件格式
|
|
215
|
|
- curContext.Response.ContentType = "application/vnd.ms-excel";
|
|
216
|
|
- curContext.Response.ContentEncoding = Encoding.UTF8;
|
|
217
|
|
- curContext.Response.Charset = "";
|
|
218
|
|
- curContext.Response.AppendHeader("Content-Disposition",
|
|
219
|
|
- "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
|
|
|
213
|
+ // 设置编码和附件格式
|
|
|
214
|
+ curContext.Response.ContentType = "application/vnd.ms-excel";
|
|
|
215
|
+ curContext.Response.ContentEncoding = Encoding.UTF8;
|
|
|
216
|
+ curContext.Response.Charset = "";
|
|
|
217
|
+ curContext.Response.AppendHeader("Content-Disposition",
|
|
|
218
|
+ "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
|
|
220
|
219
|
|
|
221
|
|
- curContext.Response.BinaryWrite(ms.GetBuffer());
|
|
|
220
|
+ curContext.Response.BinaryWrite(ms.GetBuffer());
|
|
222
|
221
|
|
|
223
|
|
- workbook = null;
|
|
224
|
|
- ms.Close();
|
|
225
|
|
- ms.Dispose();
|
|
|
222
|
+ workbook = null;
|
|
|
223
|
+ ms.Close();
|
|
|
224
|
+ ms.Dispose();
|
|
226
|
225
|
|
|
227
|
|
- curContext.Response.End();
|
|
228
|
|
- }
|
|
|
226
|
+ curContext.Response.End();
|
|
229
|
227
|
}
|
|
|
228
|
+
|
|
230
|
229
|
return "";
|
|
231
|
230
|
}
|
|
232
|
231
|
catch
|