99
This commit is contained in:
60
Reportapi/MyCode.Project.Infrastructure/Common/NPOIHelper.cs
Normal file
60
Reportapi/MyCode.Project.Infrastructure/Common/NPOIHelper.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using NPOI.HPSF;
|
||||
using NPOI.HSSF.UserModel;
|
||||
using NPOI.SS.Formula.Eval;
|
||||
using NPOI.SS.UserModel;
|
||||
using NPOI.SS.Util;
|
||||
using NPOI.XSSF.UserModel;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
|
||||
namespace MyCode.Project.Infrastructure.Common
|
||||
{
|
||||
public class NPOIHelper
|
||||
{
|
||||
|
||||
public static MemoryStream ExcelExport(DataTable table)
|
||||
{
|
||||
|
||||
|
||||
MemoryStream ms = new MemoryStream();
|
||||
using (table)
|
||||
{
|
||||
IWorkbook workbook = new HSSFWorkbook();
|
||||
|
||||
ISheet sheet = workbook.CreateSheet();
|
||||
|
||||
IRow headerRow = sheet.CreateRow(0);
|
||||
// handling header.
|
||||
foreach (DataColumn column in table.Columns)
|
||||
headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);//If Caption not set, returns the ColumnName value
|
||||
// handling value.
|
||||
int rowIndex = 1;
|
||||
foreach (DataRow row in table.Rows)
|
||||
{
|
||||
IRow dataRow = sheet.CreateRow(rowIndex);
|
||||
foreach (DataColumn column in table.Columns)
|
||||
{
|
||||
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
|
||||
}
|
||||
rowIndex++;
|
||||
}
|
||||
workbook.Write(ms);
|
||||
ms.Flush();
|
||||
ms.Position = 0;
|
||||
}
|
||||
|
||||
|
||||
return ms;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user