|
|
@@ -114,7 +114,7 @@ namespace CallCenter.Utility
|
|
114
|
114
|
/// <param name="Name"></param>
|
|
115
|
115
|
/// <param name="dt"></param>
|
|
116
|
116
|
/// <returns></returns>
|
|
117
|
|
- public string ExportToExcel(string Name, DataTable dt, string[] cols = null)
|
|
|
117
|
+ public string ExportToExcel_old(string Name, DataTable dt, string[] cols = null)
|
|
118
|
118
|
{
|
|
119
|
119
|
try
|
|
120
|
120
|
{
|
|
|
@@ -234,6 +234,193 @@ namespace CallCenter.Utility
|
|
234
|
234
|
}
|
|
235
|
235
|
}
|
|
236
|
236
|
|
|
|
237
|
+
|
|
|
238
|
+ /// <summary>
|
|
|
239
|
+ /// 弹出下载框导出excel => 创建多个Sheet:例如sheet1,sheet2...
|
|
|
240
|
+ /// </summary>
|
|
|
241
|
+ /// <param name="Name"></param>
|
|
|
242
|
+ /// <param name="dt"></param>
|
|
|
243
|
+ /// <returns></returns>
|
|
|
244
|
+ public string ExportToExcel(string Name, DataTable dt, string[] cols = null)
|
|
|
245
|
+ {
|
|
|
246
|
+ try
|
|
|
247
|
+ {
|
|
|
248
|
+ HSSFWorkbook workbook = new HSSFWorkbook();
|
|
|
249
|
+ //ISheet sheet = workbook.CreateSheet("Sheet1");
|
|
|
250
|
+
|
|
|
251
|
+ //数据总数
|
|
|
252
|
+ int count = dt.Rows.Count;
|
|
|
253
|
+ //注意:1、Excel2003及以前版本65536行,256列; 2、Excel2007、2010版本1048576行,16384列
|
|
|
254
|
+ int sheetpage = 50000; //这里设置5w条
|
|
|
255
|
+ //向上取整
|
|
|
256
|
+ int sheetindexcount = int.Parse(Math.Ceiling(Convert.ToDecimal((double)count / sheetpage)).ToString());
|
|
|
257
|
+ if(sheetindexcount == 0)
|
|
|
258
|
+ sheetindexcount = 1;
|
|
|
259
|
+
|
|
|
260
|
+ for (int n = 1; n <= sheetindexcount; n++)
|
|
|
261
|
+ {
|
|
|
262
|
+ ISheet sheet = workbook.CreateSheet("Sheet" + n);
|
|
|
263
|
+ ICellStyle HeadercellStyle = workbook.CreateCellStyle();
|
|
|
264
|
+ HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
265
|
+ HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
266
|
+ HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
267
|
+ HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
268
|
+ HeadercellStyle.Alignment = HorizontalAlignment.Center;
|
|
|
269
|
+ HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
|
|
|
270
|
+ HeadercellStyle.FillPattern = FillPattern.SolidForeground;
|
|
|
271
|
+ HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
|
|
|
272
|
+
|
|
|
273
|
+ //字体
|
|
|
274
|
+ NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
|
|
|
275
|
+ headerfont.Boldweight = (short)FontBoldWeight.Bold;
|
|
|
276
|
+ headerfont.FontHeightInPoints = 12;
|
|
|
277
|
+ HeadercellStyle.SetFont(headerfont);
|
|
|
278
|
+
|
|
|
279
|
+
|
|
|
280
|
+ //用column name 作为列名
|
|
|
281
|
+ int icolIndex = 0;
|
|
|
282
|
+ IRow headerRow = sheet.CreateRow(0);
|
|
|
283
|
+ if (cols == null || (cols != null && cols.Length == 0))
|
|
|
284
|
+ {
|
|
|
285
|
+ foreach (DataColumn dc in dt.Columns)
|
|
|
286
|
+ {
|
|
|
287
|
+ ICell cell = headerRow.CreateCell(icolIndex);
|
|
|
288
|
+ cell.SetCellValue(dc.ColumnName);
|
|
|
289
|
+ cell.CellStyle = HeadercellStyle;
|
|
|
290
|
+ icolIndex++;
|
|
|
291
|
+ }
|
|
|
292
|
+ }
|
|
|
293
|
+ else
|
|
|
294
|
+ {
|
|
|
295
|
+ foreach (string dc in cols)
|
|
|
296
|
+ {
|
|
|
297
|
+ ICell cell = headerRow.CreateCell(icolIndex);
|
|
|
298
|
+ cell.SetCellValue(dc);
|
|
|
299
|
+ cell.CellStyle = HeadercellStyle;
|
|
|
300
|
+ icolIndex++;
|
|
|
301
|
+ }
|
|
|
302
|
+ }
|
|
|
303
|
+ ICellStyle cellStyle = workbook.CreateCellStyle();
|
|
|
304
|
+
|
|
|
305
|
+ //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
|
|
|
306
|
+ cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
|
|
|
307
|
+ cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
308
|
+ cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
309
|
+ cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
310
|
+ cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
311
|
+
|
|
|
312
|
+
|
|
|
313
|
+ NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
|
|
|
314
|
+ cellfont.Boldweight = (short)FontBoldWeight.Normal;
|
|
|
315
|
+ cellStyle.SetFont(cellfont);
|
|
|
316
|
+ if (dt.Rows.Count > 0)
|
|
|
317
|
+ {
|
|
|
318
|
+ //建立内容行
|
|
|
319
|
+ int iRowIndex = 0;
|
|
|
320
|
+ #region 修改不用
|
|
|
321
|
+ //foreach (DataRow dr in dt.Rows)
|
|
|
322
|
+ //{
|
|
|
323
|
+ // int iCellIndex = 0;
|
|
|
324
|
+ // IRow irow = sheet.CreateRow(iRowIndex + 1);
|
|
|
325
|
+ // for (int i = 0; i < dt.Columns.Count; i++)
|
|
|
326
|
+ // {
|
|
|
327
|
+ // string strsj = string.Empty;
|
|
|
328
|
+ // if (dr[i] != null)
|
|
|
329
|
+ // {
|
|
|
330
|
+ // strsj = dr[i].ToString();
|
|
|
331
|
+ // }
|
|
|
332
|
+ // ICell cell = irow.CreateCell(iCellIndex);
|
|
|
333
|
+ // cell.SetCellValue(strsj);
|
|
|
334
|
+ // cell.CellStyle = cellStyle;
|
|
|
335
|
+ // iCellIndex++;
|
|
|
336
|
+ // }
|
|
|
337
|
+ // iRowIndex++;
|
|
|
338
|
+ //}
|
|
|
339
|
+ #endregion
|
|
|
340
|
+
|
|
|
341
|
+ int startcount = (n - 1) * sheetpage;
|
|
|
342
|
+ int endcount = n * sheetpage;
|
|
|
343
|
+ if (sheetindexcount == n)
|
|
|
344
|
+ endcount = count;
|
|
|
345
|
+
|
|
|
346
|
+ //for (int x = 0; x < dt.Rows.Count; x++)
|
|
|
347
|
+ for (int x = startcount; x < endcount; x++)
|
|
|
348
|
+ {
|
|
|
349
|
+ int iCellIndex = 0;
|
|
|
350
|
+ IRow irow = sheet.CreateRow(iRowIndex + 1);
|
|
|
351
|
+ for (int i = 0; i < dt.Columns.Count; i++)
|
|
|
352
|
+ {
|
|
|
353
|
+ string strsj = string.Empty;
|
|
|
354
|
+ if (dt.Rows[x][i] != null)
|
|
|
355
|
+ {
|
|
|
356
|
+ strsj = dt.Rows[x][i].ToString();
|
|
|
357
|
+ }
|
|
|
358
|
+ ICell cell = irow.CreateCell(iCellIndex);
|
|
|
359
|
+ cell.SetCellValue(strsj);
|
|
|
360
|
+ cell.CellStyle = cellStyle;
|
|
|
361
|
+ iCellIndex++;
|
|
|
362
|
+ }
|
|
|
363
|
+ iRowIndex++;
|
|
|
364
|
+ }
|
|
|
365
|
+
|
|
|
366
|
+ //自适应列宽度
|
|
|
367
|
+ for (int i = 0; i < icolIndex; i++)
|
|
|
368
|
+ {
|
|
|
369
|
+ sheet.AutoSizeColumn(i);
|
|
|
370
|
+ }
|
|
|
371
|
+ }
|
|
|
372
|
+ }
|
|
|
373
|
+ using (MemoryStream ms = new MemoryStream())
|
|
|
374
|
+ {
|
|
|
375
|
+ workbook.Write(ms);
|
|
|
376
|
+ HttpContext curContext = HttpContext.Current;
|
|
|
377
|
+ // 设置编码和附件格式
|
|
|
378
|
+ curContext.Response.ContentType = "application/vnd.ms-excel";
|
|
|
379
|
+ curContext.Response.ContentEncoding = Encoding.UTF8;
|
|
|
380
|
+ curContext.Response.Charset = "";
|
|
|
381
|
+ curContext.Response.AppendHeader("Content-Disposition",
|
|
|
382
|
+ "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
|
|
|
383
|
+ curContext.Response.BinaryWrite(ms.GetBuffer());
|
|
|
384
|
+ workbook = null;
|
|
|
385
|
+ ms.Close();
|
|
|
386
|
+ ms.Dispose();
|
|
|
387
|
+ curContext.Response.End();
|
|
|
388
|
+ }
|
|
|
389
|
+ return "";
|
|
|
390
|
+ }
|
|
|
391
|
+ catch(Exception ex)
|
|
|
392
|
+ {
|
|
|
393
|
+ return "导出失败!" + ex.Message;
|
|
|
394
|
+ }
|
|
|
395
|
+ }
|
|
|
396
|
+ /// <summary>
|
|
|
397
|
+ /// 创建Sheet
|
|
|
398
|
+ /// </summary>
|
|
|
399
|
+ /// <param name="workBook"></param>
|
|
|
400
|
+ /// <param name="sheetName"></param>
|
|
|
401
|
+ /// <returns></returns>
|
|
|
402
|
+ public ISheet CreateSheet(HSSFWorkbook workBook, string sheetName)
|
|
|
403
|
+ {
|
|
|
404
|
+ ISheet sheet = workBook.CreateSheet(sheetName);
|
|
|
405
|
+ IRow RowHead = sheet.CreateRow(0);
|
|
|
406
|
+
|
|
|
407
|
+ for (int iColumnIndex = 0; iColumnIndex < 10; iColumnIndex++)
|
|
|
408
|
+ {
|
|
|
409
|
+ RowHead.CreateCell(iColumnIndex).SetCellValue(Guid.NewGuid().ToString());
|
|
|
410
|
+ }
|
|
|
411
|
+
|
|
|
412
|
+ for (int iRowIndex = 0; iRowIndex < 20; iRowIndex++)
|
|
|
413
|
+ {
|
|
|
414
|
+ IRow RowBody = sheet.CreateRow(iRowIndex + 1);
|
|
|
415
|
+ for (int iColumnIndex = 0; iColumnIndex < 10; iColumnIndex++)
|
|
|
416
|
+ {
|
|
|
417
|
+ RowBody.CreateCell(iColumnIndex).SetCellValue(DateTime.Now.Millisecond);
|
|
|
418
|
+ sheet.AutoSizeColumn(iColumnIndex);
|
|
|
419
|
+ }
|
|
|
420
|
+ }
|
|
|
421
|
+ return sheet;
|
|
|
422
|
+ }
|
|
|
423
|
+
|
|
237
|
424
|
/// <summary>
|
|
238
|
425
|
/// 导入excel转换为datatable
|
|
239
|
426
|
/// </summary>
|