|
|
@@ -8,6 +8,7 @@ using NPOI.HSSF.UserModel;
|
|
8
|
8
|
using NPOI.SS.UserModel;
|
|
9
|
9
|
using NPOI.SS.Util;
|
|
10
|
10
|
using NPOI.XSSF.UserModel;
|
|
|
11
|
+using NPOI.HSSF.Util;
|
|
11
|
12
|
|
|
12
|
13
|
namespace CallCenter.Utility
|
|
13
|
14
|
{
|
|
|
@@ -114,15 +115,16 @@ namespace CallCenter.Utility
|
|
114
|
115
|
/// <param name="Name"></param>
|
|
115
|
116
|
/// <param name="dt"></param>
|
|
116
|
117
|
/// <returns></returns>
|
|
117
|
|
- public string ExportToExcel(string Name, DataTable dt, string[] cols = null)
|
|
|
118
|
+ public string ExportToExcelSplitSheet(string Name, DataTable dt, string[] cols = null)
|
|
118
|
119
|
{
|
|
119
|
|
- try
|
|
120
|
|
- {
|
|
|
120
|
+ //try
|
|
|
121
|
+ //{
|
|
121
|
122
|
//if (dt.Rows.Count > 0)
|
|
122
|
123
|
//{
|
|
123
|
124
|
HSSFWorkbook workbook = new HSSFWorkbook();//导出xls
|
|
124
|
125
|
//IWorkbook workbook = new XSSFWorkbook();//导出xlsx
|
|
125
|
|
- ISheet sheet = workbook.CreateSheet("Sheet1");
|
|
|
126
|
+ int SheetNum = 1;
|
|
|
127
|
+ ISheet sheet = workbook.CreateSheet("Sheet"+ SheetNum);
|
|
126
|
128
|
|
|
127
|
129
|
ICellStyle HeadercellStyle = workbook.CreateCellStyle();
|
|
128
|
130
|
HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
@@ -180,10 +182,13 @@ namespace CallCenter.Utility
|
|
180
|
182
|
|
|
181
|
183
|
//建立内容行
|
|
182
|
184
|
int iRowIndex = 0;
|
|
183
|
|
- foreach (DataRow dr in dt.Rows)
|
|
|
185
|
+ int iRowIndex2 = 0;
|
|
|
186
|
+ foreach (DataRow dr in dt.Rows)
|
|
|
187
|
+ {
|
|
|
188
|
+ int iCellIndex = 0;
|
|
|
189
|
+ IRow irow = sheet.CreateRow(iRowIndex + 1);
|
|
|
190
|
+ if (iRowIndex < 60000)
|
|
184
|
191
|
{
|
|
185
|
|
- int iCellIndex = 0;
|
|
186
|
|
- IRow irow = sheet.CreateRow(iRowIndex + 1);
|
|
187
|
192
|
for (int i = 0; i < dt.Columns.Count; i++)
|
|
188
|
193
|
{
|
|
189
|
194
|
string strsj = string.Empty;
|
|
|
@@ -198,14 +203,46 @@ namespace CallCenter.Utility
|
|
198
|
203
|
}
|
|
199
|
204
|
iRowIndex++;
|
|
200
|
205
|
}
|
|
201
|
|
-
|
|
202
|
|
- ////自适应列宽度
|
|
203
|
|
- //for (int i = 0; i < icolIndex; i++)
|
|
204
|
|
- //{
|
|
205
|
|
- // sheet.AutoSizeColumn(i);
|
|
206
|
|
- //}
|
|
207
|
|
-
|
|
208
|
|
- using (MemoryStream ms = new MemoryStream())
|
|
|
206
|
+
|
|
|
207
|
+ int iCellIndex2 = 0;
|
|
|
208
|
+ SheetNum = SheetNum + 1;
|
|
|
209
|
+ ISheet sheet2=workbook.CreateSheet("Sheet" + SheetNum );
|
|
|
210
|
+ if (iRowIndex >= 60000)
|
|
|
211
|
+ {
|
|
|
212
|
+
|
|
|
213
|
+ if (iRowIndex == 60000)
|
|
|
214
|
+ {
|
|
|
215
|
+
|
|
|
216
|
+ int icolIndex2 = 0;
|
|
|
217
|
+ foreach (string dc in cols)
|
|
|
218
|
+ {
|
|
|
219
|
+ ICell cell = headerRow.CreateCell(icolIndex2);
|
|
|
220
|
+ cell.SetCellValue(dc);
|
|
|
221
|
+ cell.CellStyle = HeadercellStyle;
|
|
|
222
|
+ icolIndex2++;
|
|
|
223
|
+ }
|
|
|
224
|
+ }
|
|
|
225
|
+
|
|
|
226
|
+ IRow irow2 = sheet2.CreateRow (iRowIndex2 + 1);
|
|
|
227
|
+ for (int i = 0; i < dt.Columns.Count; i++)
|
|
|
228
|
+ {
|
|
|
229
|
+ string strsj = string.Empty;
|
|
|
230
|
+ if (dr[i] != null)
|
|
|
231
|
+ {
|
|
|
232
|
+ strsj = dr[i].ToString();
|
|
|
233
|
+ }
|
|
|
234
|
+ ICell cell = irow.CreateCell(iCellIndex2);
|
|
|
235
|
+ cell.SetCellValue(strsj);
|
|
|
236
|
+ cell.CellStyle = cellStyle;
|
|
|
237
|
+ iCellIndex2++;
|
|
|
238
|
+ }
|
|
|
239
|
+ iRowIndex++;
|
|
|
240
|
+ iRowIndex2++;
|
|
|
241
|
+ }
|
|
|
242
|
+
|
|
|
243
|
+ }
|
|
|
244
|
+
|
|
|
245
|
+ using (MemoryStream ms = new MemoryStream())
|
|
209
|
246
|
{
|
|
210
|
247
|
workbook.Write(ms);
|
|
211
|
248
|
|
|
|
@@ -227,6 +264,130 @@ namespace CallCenter.Utility
|
|
227
|
264
|
|
|
228
|
265
|
curContext.Response.End();
|
|
229
|
266
|
}
|
|
|
267
|
+
|
|
|
268
|
+ return "";
|
|
|
269
|
+ //}
|
|
|
270
|
+ //catch(Exception e)
|
|
|
271
|
+ //{
|
|
|
272
|
+ // string estr = e.Message;
|
|
|
273
|
+ // return "导出失败!";
|
|
|
274
|
+ //}
|
|
|
275
|
+ }
|
|
|
276
|
+
|
|
|
277
|
+
|
|
|
278
|
+ public string ExportToExcel(string Name, DataTable dt, string[] cols = null)
|
|
|
279
|
+ {
|
|
|
280
|
+ try
|
|
|
281
|
+ {
|
|
|
282
|
+ //if (dt.Rows.Count > 0)
|
|
|
283
|
+ //{
|
|
|
284
|
+ HSSFWorkbook workbook = new HSSFWorkbook();//导出xls
|
|
|
285
|
+ //IWorkbook workbook = new XSSFWorkbook();//导出xlsx
|
|
|
286
|
+ ISheet sheet = workbook.CreateSheet("Sheet1");
|
|
|
287
|
+
|
|
|
288
|
+ ICellStyle HeadercellStyle = workbook.CreateCellStyle();
|
|
|
289
|
+ HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
290
|
+ HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
291
|
+ HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
292
|
+ HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
293
|
+ HeadercellStyle.Alignment = HorizontalAlignment.Center;
|
|
|
294
|
+ HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
|
|
|
295
|
+ HeadercellStyle.FillPattern = FillPattern.SolidForeground;
|
|
|
296
|
+ HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
|
|
|
297
|
+
|
|
|
298
|
+ //字体
|
|
|
299
|
+ NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
|
|
|
300
|
+ headerfont.Boldweight = (short)FontBoldWeight.Bold;
|
|
|
301
|
+ headerfont.FontHeightInPoints = 12;
|
|
|
302
|
+ HeadercellStyle.SetFont(headerfont);
|
|
|
303
|
+
|
|
|
304
|
+
|
|
|
305
|
+ //用column name 作为列名
|
|
|
306
|
+ int icolIndex = 0;
|
|
|
307
|
+ IRow headerRow = sheet.CreateRow(0);
|
|
|
308
|
+ if (cols == null || (cols != null && cols.Length == 0))
|
|
|
309
|
+ {
|
|
|
310
|
+ foreach (DataColumn dc in dt.Columns)
|
|
|
311
|
+ {
|
|
|
312
|
+ ICell cell = headerRow.CreateCell(icolIndex);
|
|
|
313
|
+ cell.SetCellValue(dc.ColumnName);
|
|
|
314
|
+ cell.CellStyle = HeadercellStyle;
|
|
|
315
|
+ icolIndex++;
|
|
|
316
|
+ }
|
|
|
317
|
+ }
|
|
|
318
|
+ else
|
|
|
319
|
+ {
|
|
|
320
|
+ foreach (string dc in cols)
|
|
|
321
|
+ {
|
|
|
322
|
+ ICell cell = headerRow.CreateCell(icolIndex);
|
|
|
323
|
+ cell.SetCellValue(dc);
|
|
|
324
|
+ cell.CellStyle = HeadercellStyle;
|
|
|
325
|
+ icolIndex++;
|
|
|
326
|
+ }
|
|
|
327
|
+ }
|
|
|
328
|
+ ICellStyle cellStyle = workbook.CreateCellStyle();
|
|
|
329
|
+
|
|
|
330
|
+ //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
|
|
|
331
|
+ cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
|
|
|
332
|
+ cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
333
|
+ cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
334
|
+ cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
335
|
+ cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
336
|
+
|
|
|
337
|
+
|
|
|
338
|
+ NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
|
|
|
339
|
+ cellfont.Boldweight = (short)FontBoldWeight.Normal;
|
|
|
340
|
+ cellStyle.SetFont(cellfont);
|
|
|
341
|
+
|
|
|
342
|
+ //建立内容行
|
|
|
343
|
+ int iRowIndex = 0;
|
|
|
344
|
+ foreach (DataRow dr in dt.Rows)
|
|
|
345
|
+ {
|
|
|
346
|
+ int iCellIndex = 0;
|
|
|
347
|
+ IRow irow = sheet.CreateRow(iRowIndex + 1);
|
|
|
348
|
+ for (int i = 0; i < dt.Columns.Count; i++)
|
|
|
349
|
+ {
|
|
|
350
|
+ string strsj = string.Empty;
|
|
|
351
|
+ if (dr[i] != null)
|
|
|
352
|
+ {
|
|
|
353
|
+ strsj = dr[i].ToString();
|
|
|
354
|
+ }
|
|
|
355
|
+ ICell cell = irow.CreateCell(iCellIndex);
|
|
|
356
|
+ cell.SetCellValue(strsj);
|
|
|
357
|
+ cell.CellStyle = cellStyle;
|
|
|
358
|
+ iCellIndex++;
|
|
|
359
|
+ }
|
|
|
360
|
+ iRowIndex++;
|
|
|
361
|
+ }
|
|
|
362
|
+
|
|
|
363
|
+ ////自适应列宽度
|
|
|
364
|
+ //for (int i = 0; i < icolIndex; i++)
|
|
|
365
|
+ //{
|
|
|
366
|
+ // sheet.AutoSizeColumn(i);
|
|
|
367
|
+ //}
|
|
|
368
|
+
|
|
|
369
|
+ using (MemoryStream ms = new MemoryStream())
|
|
|
370
|
+ {
|
|
|
371
|
+ workbook.Write(ms);
|
|
|
372
|
+
|
|
|
373
|
+ HttpContext curContext = HttpContext.Current;
|
|
|
374
|
+
|
|
|
375
|
+
|
|
|
376
|
+ // 设置编码和附件格式
|
|
|
377
|
+ curContext.Response.ContentType = "application/vnd.ms-excel";
|
|
|
378
|
+ curContext.Response.ContentEncoding = Encoding.UTF8;//Encoding.UTF8;
|
|
|
379
|
+ curContext.Response.Charset = "";
|
|
|
380
|
+ curContext.Response.AppendHeader("Content-Disposition",
|
|
|
381
|
+ "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
|
|
|
382
|
+
|
|
|
383
|
+ curContext.Response.BinaryWrite(ms.GetBuffer());
|
|
|
384
|
+
|
|
|
385
|
+ workbook = null;
|
|
|
386
|
+ ms.Close();
|
|
|
387
|
+ ms.Dispose();
|
|
|
388
|
+
|
|
|
389
|
+ curContext.Response.End();
|
|
|
390
|
+ }
|
|
230
|
391
|
//}
|
|
231
|
392
|
return "";
|
|
232
|
393
|
}
|
|
|
@@ -236,6 +397,7 @@ namespace CallCenter.Utility
|
|
236
|
397
|
}
|
|
237
|
398
|
}
|
|
238
|
399
|
|
|
|
400
|
+
|
|
239
|
401
|
/// <summary>
|
|
240
|
402
|
/// 导入excel转换为datatable
|
|
241
|
403
|
/// </summary>
|
|
|
@@ -510,5 +672,166 @@ namespace CallCenter.Utility
|
|
510
|
672
|
}
|
|
511
|
673
|
}
|
|
512
|
674
|
#endregion
|
|
|
675
|
+
|
|
|
676
|
+ public string ExportToExcel2003(string Name, DataTable dt, string[] cols = null)
|
|
|
677
|
+ {
|
|
|
678
|
+ try
|
|
|
679
|
+ {
|
|
|
680
|
+ HSSFWorkbook workbook = new HSSFWorkbook();
|
|
|
681
|
+
|
|
|
682
|
+ ICellStyle HeadercellStyle = workbook.CreateCellStyle();
|
|
|
683
|
+ HeadercellStyle.BorderBottom = BorderStyle.Thin;
|
|
|
684
|
+ HeadercellStyle.BorderLeft = BorderStyle.Thin;
|
|
|
685
|
+ HeadercellStyle.BorderRight = BorderStyle.Thin;
|
|
|
686
|
+ HeadercellStyle.BorderTop = BorderStyle.Thin;
|
|
|
687
|
+ HeadercellStyle.Alignment = HorizontalAlignment.Center;
|
|
|
688
|
+ HeadercellStyle.FillForegroundColor = HSSFColor.SkyBlue.Index;
|
|
|
689
|
+ HeadercellStyle.FillPattern = FillPattern.SolidForeground;
|
|
|
690
|
+ HeadercellStyle.FillBackgroundColor = HSSFColor.SkyBlue.Index;
|
|
|
691
|
+
|
|
|
692
|
+ //字体
|
|
|
693
|
+ IFont headerfont = workbook.CreateFont();
|
|
|
694
|
+ headerfont.Boldweight = (short)FontBoldWeight.Bold;
|
|
|
695
|
+ headerfont.FontHeightInPoints = 12;
|
|
|
696
|
+ HeadercellStyle.SetFont(headerfont);
|
|
|
697
|
+
|
|
|
698
|
+
|
|
|
699
|
+ //用column name 作为列名
|
|
|
700
|
+ int icolIndex = 0;
|
|
|
701
|
+ //建立内容行
|
|
|
702
|
+ int iRowIndex = 0;
|
|
|
703
|
+
|
|
|
704
|
+ int sheetCount = 0;
|
|
|
705
|
+ int sheetRowCount = 60000;
|
|
|
706
|
+ ISheet sheet = null;
|
|
|
707
|
+ IRow headerRow = null;
|
|
|
708
|
+
|
|
|
709
|
+ if (dt.Rows.Count == 0)
|
|
|
710
|
+ {
|
|
|
711
|
+ sheet = workbook.CreateSheet(Name);
|
|
|
712
|
+ headerRow = sheet.CreateRow(0);
|
|
|
713
|
+
|
|
|
714
|
+ #region 表头
|
|
|
715
|
+ if (cols == null || (cols != null && cols.Length == 0))
|
|
|
716
|
+ {
|
|
|
717
|
+ foreach (DataColumn dc in dt.Columns)
|
|
|
718
|
+ {
|
|
|
719
|
+ ICell cell = headerRow.CreateCell(icolIndex);
|
|
|
720
|
+ cell.SetCellValue(dc.ColumnName);
|
|
|
721
|
+ cell.CellStyle = HeadercellStyle;
|
|
|
722
|
+ icolIndex++;
|
|
|
723
|
+ }
|
|
|
724
|
+ }
|
|
|
725
|
+ else
|
|
|
726
|
+ {
|
|
|
727
|
+ foreach (string dc in cols)
|
|
|
728
|
+ {
|
|
|
729
|
+ ICell cell = headerRow.CreateCell(icolIndex);
|
|
|
730
|
+ cell.SetCellValue(dc);
|
|
|
731
|
+ cell.CellStyle = HeadercellStyle;
|
|
|
732
|
+ icolIndex++;
|
|
|
733
|
+ }
|
|
|
734
|
+ }
|
|
|
735
|
+ #endregion
|
|
|
736
|
+ }
|
|
|
737
|
+ else
|
|
|
738
|
+ {
|
|
|
739
|
+ ICellStyle cellStyle = workbook.CreateCellStyle();
|
|
|
740
|
+
|
|
|
741
|
+ //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
|
|
|
742
|
+ cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
|
|
|
743
|
+ cellStyle.BorderBottom = BorderStyle.Thin;
|
|
|
744
|
+ cellStyle.BorderLeft = BorderStyle.Thin;
|
|
|
745
|
+ cellStyle.BorderRight = BorderStyle.Thin;
|
|
|
746
|
+ cellStyle.BorderTop = BorderStyle.Thin;
|
|
|
747
|
+ IFont cellfont = workbook.CreateFont();
|
|
|
748
|
+ cellfont.Boldweight = (short)FontBoldWeight.Normal;
|
|
|
749
|
+ cellStyle.SetFont(cellfont);
|
|
|
750
|
+ for (int i = 0; i < dt.Rows.Count; i++)
|
|
|
751
|
+ {
|
|
|
752
|
+ if (i % sheetRowCount == 0)
|
|
|
753
|
+ {
|
|
|
754
|
+ if (i > 0)
|
|
|
755
|
+ {
|
|
|
756
|
+ //自适应列宽度
|
|
|
757
|
+ for (int j = 0; j < icolIndex; j++)
|
|
|
758
|
+ {
|
|
|
759
|
+ sheet.AutoSizeColumn(j);
|
|
|
760
|
+ }
|
|
|
761
|
+ }
|
|
|
762
|
+
|
|
|
763
|
+ sheetCount++;
|
|
|
764
|
+ iRowIndex = 0;
|
|
|
765
|
+ icolIndex = 0;
|
|
|
766
|
+
|
|
|
767
|
+ sheet = workbook.CreateSheet(Name + sheetCount.ToString());
|
|
|
768
|
+ headerRow = sheet.CreateRow(0);
|
|
|
769
|
+
|
|
|
770
|
+ #region 表头
|
|
|
771
|
+ if (cols == null || (cols != null && cols.Length == 0))
|
|
|
772
|
+ {
|
|
|
773
|
+ foreach (DataColumn dc in dt.Columns)
|
|
|
774
|
+ {
|
|
|
775
|
+ ICell cell = headerRow.CreateCell(icolIndex);
|
|
|
776
|
+ cell.SetCellValue(dc.ColumnName);
|
|
|
777
|
+ cell.CellStyle = HeadercellStyle;
|
|
|
778
|
+ icolIndex++;
|
|
|
779
|
+ }
|
|
|
780
|
+ }
|
|
|
781
|
+ else
|
|
|
782
|
+ {
|
|
|
783
|
+ foreach (string dc in cols)
|
|
|
784
|
+ {
|
|
|
785
|
+ ICell cell = headerRow.CreateCell(icolIndex);
|
|
|
786
|
+ cell.SetCellValue(dc);
|
|
|
787
|
+ cell.CellStyle = HeadercellStyle;
|
|
|
788
|
+ icolIndex++;
|
|
|
789
|
+ }
|
|
|
790
|
+ }
|
|
|
791
|
+ #endregion
|
|
|
792
|
+ }
|
|
|
793
|
+
|
|
|
794
|
+ IRow irow = sheet.CreateRow(iRowIndex + 1);
|
|
|
795
|
+ foreach (DataColumn dc in dt.Columns)
|
|
|
796
|
+ {
|
|
|
797
|
+ string strsj = dt.Rows[i][dc]?.ToString() ?? "";
|
|
|
798
|
+ ICell cell = irow.CreateCell(dc.Ordinal);
|
|
|
799
|
+ cell.SetCellValue(strsj);
|
|
|
800
|
+ cell.CellStyle = cellStyle;
|
|
|
801
|
+ }
|
|
|
802
|
+ iRowIndex++;
|
|
|
803
|
+ }
|
|
|
804
|
+ }
|
|
|
805
|
+
|
|
|
806
|
+ using (MemoryStream ms = new MemoryStream())
|
|
|
807
|
+ {
|
|
|
808
|
+ workbook.Write(ms);
|
|
|
809
|
+ HttpContext curContext = HttpContext.Current;
|
|
|
810
|
+ // 设置编码和附件格式
|
|
|
811
|
+ curContext.Response.ContentType = "application/vnd.ms-excel";
|
|
|
812
|
+ curContext.Response.ContentEncoding = Encoding.UTF8;
|
|
|
813
|
+ curContext.Response.Charset = "";
|
|
|
814
|
+ curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
|
|
|
815
|
+ curContext.Response.AppendHeader("Content-Disposition",
|
|
|
816
|
+ "attachment;filename=" + HttpUtility.UrlEncode(Name + "_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
|
|
|
817
|
+ curContext.Response.BinaryWrite(ms.GetBuffer());
|
|
|
818
|
+ workbook = null;
|
|
|
819
|
+ ms.Close();
|
|
|
820
|
+ ms.Dispose();
|
|
|
821
|
+ curContext.Response.End();
|
|
|
822
|
+ }
|
|
|
823
|
+
|
|
|
824
|
+ return "";
|
|
|
825
|
+ }
|
|
|
826
|
+ catch (Exception ex)
|
|
|
827
|
+ {
|
|
|
828
|
+ // LogHelper.Error("导出2003失败!", ex);
|
|
|
829
|
+ return "导出失败!";
|
|
|
830
|
+ }
|
|
|
831
|
+ }
|
|
|
832
|
+
|
|
|
833
|
+
|
|
|
834
|
+
|
|
|
835
|
+
|
|
513
|
836
|
}
|
|
514
|
837
|
}
|