private void ExportToExcel(string sheetName, string headerName )
{
if (dt_Grid == null) return;
HSSFWorkbook workBook = new HSSFWorkbook();
ISheet sheet = workBook.CreateSheet(sheetName);
//创建表头
int rowCount = 1;
IRow row01 = sheet.CreateRow(rowCount);
int data_colIndex = 0;
ICellStyle style_cell01 = workBook.CreateCellStyle();
style_cell01.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER; //居中
style_cell01.BorderTop = style_cell01.BorderBottom = style_cell01.BorderLeft = style_cell01.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
style_cell01.LeftBorderColor = style_cell01.RightBorderColor = style_cell01.TopBorderColor = style_cell01.BottomBorderColor
= HSSFColor.BLACK.index;
IFont font_cell01 = workBook.CreateFont();
font_cell01.FontHeightInPoints = 12; //字体大小
font_cell01.Boldweight = 600;
style_cell01.SetFont(font_cell01);
foreach (DataColumn data_col in dt_Grid.Columns)
{
ICell cell_col = row01.CreateCell(data_colIndex);
cell_col.SetCellValue(data_col.ColumnName);
cell_col.CellStyle = style_cell01;
data_colIndex++;
}
#region 设置数据的字体
IFont font_cells = workBook.CreateFont();
font_cells.FontHeightInPoints = 12; //字体大小
#endregion
rowCount = 2;
foreach (DataRow data_row in dt_Grid.Rows)
{
IRow rows = sheet.CreateRow(rowCount);
for (int cols = 0; cols < dt_Grid.Columns.Count; cols++)
{
ICell cell_cols = rows.CreateCell(cols);
cell_cols.SetCellValue(data_row[cols].ToString());
style_cell01.SetFont(font_cells);
cell_cols.CellStyle = style_cell01;
}
rowCount++;
}
//设置列自动,在合并单元格前
for (int col = 0; col < dt_Grid.Columns.Count; col++)
{
sheet.AutoSizeColumn(col);
}
#region
rowCount = 0;
IRow row00 = sheet.CreateRow(rowCount);
ICell cell = row00.CreateCell(0);
//创建-1个单元格(必须创建为了边框设置)
for (int ii = 1; ii <= dt_Grid.Columns.Count - 1; ii++)
{
ICell cell_i = row00.CreateCell(ii);
}
int range_index = sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, dt_Grid.Columns.Count - 1));
cell.SetCellValue(headerName);
CellRangeAddress range = sheet.GetMergedRegion(range_index);
//解决合并丢失样式!!!
for (int j = range.FirstRow; j <= range.LastRow; j++)
{
for (int k = range.FirstColumn; k <= range.LastColumn; k++)
{
ICellStyle cellStyle = workBook.CreateCellStyle();
cellStyle.BorderTop = cellStyle.BorderBottom = cellStyle.BorderLeft = cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
cellStyle.LeftBorderColor = cellStyle.RightBorderColor = cellStyle.TopBorderColor = cellStyle.BottomBorderColor
= HSSFColor.BLACK.index;
cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;
IFont font_cell = workBook.CreateFont();
font_cell.FontHeightInPoints = 20; //字体大小
font_cell.Boldweight = 600;
cellStyle.SetFont(font_cell);
ICell cell_range = sheet.GetRow(j).GetCell(k);
if (cell_range != null)
{
cell_range.CellStyle = cellStyle;
}
}
}
#endregion
WriteToFile(workBook);
}
private void WriteToFile(HSSFWorkbook work)
{
SaveFileDialog sf = new SaveFileDialog();
sf.Filter = "Excel|*.xls";
if (sf.ShowDialog() == DialogResult.OK)
{
FileStream file = new FileStream(sf.FileName, FileMode.Create, FileAccess.ReadWrite);
work.Write(file);
file.Close();
}
}NPOI导出Excel
最新推荐文章于 2025-04-21 14:15:57 发布
2万+

被折叠的 条评论
为什么被折叠?



