首先,通過NuGet添加NPOI. NPOI依賴SharpZipLib,通過NuGet添加SharpZipLib. 然後添加NPOI. 添加後項目的引用列表如下: 把DataTable轉換成Excel文件。 代碼如下: public static MemoryStream RenderDataTab ...
首先,通過NuGet添加NPOI.
NPOI依賴SharpZipLib,通過NuGet添加SharpZipLib.
然後添加NPOI.
添加後項目的引用列表如下:
把DataTable轉換成Excel文件。
代碼如下:
public static MemoryStream RenderDataTableToExcel(DataTable table) { MemoryStream ms = new MemoryStream(); IWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet(table.TableName); for (int rowIndex = 0; rowIndex < table.Rows.Count; rowIndex++) { IRow dataRow = sheet.CreateRow(rowIndex); foreach (DataColumn column in table.Columns) { dataRow.CreateCell(column.Ordinal).SetCellValue(table.Rows[rowIndex][column].ToString()); } } workbook.Write(ms); ms.Close(); return ms; }View Code
轉換Excel文件內容如下:
Excel文件添加表頭
代碼:
public static MemoryStream RenderDataTableToExcelWithHeader(DataTable table) { MemoryStream ms = new MemoryStream(); IWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet(table.TableName); IRow headerRow = sheet.CreateRow(0); foreach (DataColumn column in table.Columns) { headerRow.CreateCell(column.Ordinal).SetCellValue(string.Format(" {0} ", column.Caption)); } for (int rowIndex = 0; rowIndex < table.Rows.Count; rowIndex++) { IRow dataRow = sheet.CreateRow(rowIndex+1); foreach (DataColumn column in table.Columns) { dataRow.CreateCell(column.Ordinal).SetCellValue(table.Rows[rowIndex][column].ToString()); } } workbook.Write(ms); ms.Close(); return ms; }View Code
轉換Excel文件內容如下:
添加Excel文件添加表頭樣式
代碼:
public static MemoryStream RenderDataTableToExcelWithHeaderRowStyle(DataTable table) { MemoryStream ms = new MemoryStream(); IWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet(table.TableName); IRow headerRow = sheet.CreateRow(0); ICellStyle headStyle = workbook.CreateCellStyle(); headStyle.Alignment = HorizontalAlignment.Center; headStyle.FillForegroundColor = HSSFColor.Grey25Percent.Index; headStyle.FillPattern = FillPattern.SolidForeground; IFont font = workbook.CreateFont(); font.FontName = "Microsoft Yahei"; font.FontHeightInPoints = 11; font.IsBold = true; font.Color = HSSFColor.White.Index; headStyle.SetFont(font); headStyle.BorderBottom = BorderStyle.Thin; headStyle.BorderRight = BorderStyle.Thin; headStyle.BorderLeft = BorderStyle.Thin; foreach (DataColumn column in table.Columns) { ICell cell = headerRow.CreateCell(column.Ordinal); cell.SetCellValue(string.Format(" {0} ", column.Caption)); cell.CellStyle = headStyle; } for (int rowIndex = 0; rowIndex < table.Rows.Count; rowIndex++) { IRow dataRow = sheet.CreateRow(rowIndex + 1); foreach (DataColumn column in table.Columns) { dataRow.CreateCell(column.Ordinal).SetCellValue(table.Rows[rowIndex][column].ToString()); } } workbook.Write(ms); ms.Close(); return ms; }View Code
轉換Excel文件內容如下:
添加Excel文件添加數據行樣式
代碼:
public static MemoryStream RenderDataTableToExcelWithDataRowStyle(DataTable table) { MemoryStream ms = new MemoryStream(); IWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet(table.TableName); IRow headerRow = sheet.CreateRow(0); ICellStyle headStyle = workbook.CreateCellStyle(); headStyle.Alignment = HorizontalAlignment.Center; headStyle.FillForegroundColor = HSSFColor.Grey25Percent.Index; headStyle.FillPattern = FillPattern.SolidForeground; IFont font = workbook.CreateFont(); font.FontName = "Microsoft Yahei"; font.FontHeightInPoints = 11; font.IsBold = true; font.Color = HSSFColor.White.Index; headStyle.SetFont(font); headStyle.BorderBottom = BorderStyle.Thin; headStyle.BorderRight = BorderStyle.Thin; headStyle.BorderLeft = BorderStyle.Thin; ICellStyle dataRowEvenStyle = workbook.CreateCellStyle(); dataRowEvenStyle.Alignment = HorizontalAlignment.Center; dataRowEvenStyle.FillForegroundColor = HSSFColor.LightOrange.Index; dataRowEvenStyle.FillPattern = FillPattern.SolidForeground; IFont dataRowEvenFont = workbook.CreateFont(); dataRowEvenFont.FontName = "Microsoft Yahei"; dataRowEvenFont.FontHeightInPoints = 11; dataRowEvenFont.Color = HSSFColor.Blue.Index; dataRowEvenStyle.SetFont(dataRowEvenFont); dataRowEvenStyle.BorderBottom = BorderStyle.Thin; dataRowEvenStyle.BorderRight = BorderStyle.Thin; dataRowEvenStyle.BorderLeft = BorderStyle.Thin; ICellStyle dataRowOddStyle = workbook.CreateCellStyle(); dataRowOddStyle.Alignment = HorizontalAlignment.Center; dataRowOddStyle.FillForegroundColor = HSSFColor.LightGreen.Index; dataRowOddStyle.FillPattern = FillPattern.SolidForeground; IFont dataRowOddFont = workbook.CreateFont(); dataRowOddFont.FontName = "Microsoft Yahei"; dataRowOddFont.FontHeightInPoints = 11; dataRowOddFont.Color = HSSFColor.Black.Index; dataRowOddStyle.SetFont(dataRowOddFont); dataRowOddStyle.BorderBottom = BorderStyle.Thin; dataRowOddStyle.BorderRight = BorderStyle.Thin; dataRowOddStyle.BorderLeft = BorderStyle.Thin; foreach (DataColumn column in table.Columns) { ICell cell = headerRow.CreateCell(column.Ordinal); cell.SetCellValue(string.Format(" {0} ", column.Caption)); cell.CellStyle = headStyle; } for (int rowIndex = 0; rowIndex < table.Rows.Count; rowIndex++) { IRow dataRow = sheet.CreateRow(rowIndex + 1); foreach (DataColumn column in table.Columns) { ICell cell = dataRow.CreateCell(column.Ordinal); cell.SetCellValue(table.Rows[rowIndex][column].ToString()); if (rowIndex % 2 == 0) { cell.CellStyle = dataRowEvenStyle; } else { cell.CellStyle = dataRowOddStyle; } } } workbook.Write(ms); ms.Close(); return ms; }View Code
轉換Excel文件內容如下:
Excel文件合併單元格
代碼:
public static MemoryStream RenderDataTableToExcelMergedRegion(DataTable table) { MemoryStream ms = new MemoryStream(); IWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet(table.TableName); IRow headerRow = sheet.CreateRow(0); ICellStyle headStyle = workbook.CreateCellStyle(); headStyle.Alignment = HorizontalAlignment.Center; headStyle.FillForegroundColor = HSSFColor.Grey25Percent.Index; headStyle.FillPattern = FillPattern.SolidForeground; IFont font = workbook.CreateFont(); font.FontName = "Microsoft Yahei"; font.FontHeightInPoints = 11; font.IsBold = true; font.Color = HSSFColor.White.Index; headStyle.SetFont(font); headStyle.BorderBottom = BorderStyle.Thin; headStyle.BorderRight = BorderStyle.Thin; headStyle.BorderLeft = BorderStyle.Thin; ICellStyle dataRowEvenStyle = workbook.CreateCellStyle(); dataRowEvenStyle.Alignment = HorizontalAlignment.Center; dataRowEvenStyle.FillForegroundColor = HSSFColor.LightOrange.Index; dataRowEvenStyle.FillPattern = FillPattern.SolidForeground; IFont dataRowEvenFont = workbook.CreateFont(); dataRowEvenFont.FontName = "Microsoft Yahei"; dataRowEvenFont.FontHeightInPoints = 11; dataRowEvenFont.Color = HSSFColor.Blue.Index; dataRowEvenStyle.SetFont(dataRowEvenFont); dataRowEvenStyle.BorderBottom = BorderStyle.Thin; dataRowEvenStyle.BorderRight = BorderStyle.Thin; dataRowEvenStyle.BorderLeft = BorderStyle.Thin; ICellStyle dataRowOddStyle = workbook.CreateCellStyle(); dataRowOddStyle.Alignment = HorizontalAlignment.Center; dataRowOddStyle.FillForegroundColor = HSSFColor.LightGreen.Index; dataRowOddStyle.FillPattern = FillPattern.SolidForeground; IFont dataRowOddFont = workbook.CreateFont(); dataRowOddFont.FontName = "Microsoft Yahei"; dataRowOddFont.FontHeightInPoints = 11; dataRowOddFont.Color = HSSFColor.Black.Index; dataRowOddStyle.SetFont(dataRowOddFont); dataRowOddStyle.BorderBottom = BorderStyle.Thin; dataRowOddStyle.BorderRight = BorderStyle.Thin; dataRowOddStyle.BorderLeft = BorderStyle.Thin; foreach (DataColumn column in table.Columns) { ICell cell = headerRow.CreateCell(column.Ordinal); cell.SetCellValue(string.Format(" {0} ", column.Caption)); cell.CellStyle = headStyle; } for (int rowIndex = 0; rowIndex < table.Rows.Count; rowIndex++) { IRow dataRow = sheet.CreateRow(rowIndex + 1); foreach (DataColumn column in table.Columns) { ICell cell = dataRow.CreateCell(column.Ordinal); cell.SetCellValue(table.Rows[rowIndex][column].ToString()); if (rowIndex % 2 == 0) { cell.CellStyle = dataRowEvenStyle; } else { cell.CellStyle = dataRowOddStyle; } } } sheet.AddMergedRegion(new CellRangeAddress(1, 2, 0, 0)); sheet.AddMergedRegion(new CellRangeAddress(3, 4, 0, 0)); sheet.AddMergedRegion(new CellRangeAddress(5, 6, 0, 0)); sheet.AddMergedRegion(new CellRangeAddress(7, 8, 0, 0)); sheet.AddMergedRegion(new CellRangeAddress(9, 10, 0, 0)); workbook.Write(ms); ms.Close(); return ms; }View Code
轉換Excel文件內容如下: