咳咳
这个示例有点乱,都写了注释,大家作为参考把
DataGridView转Excel
///
/// 将表格控件保存至Excel文件(新建/替换)
///
/// 要保存的目标Excel文件路径名
/// 要保存至Excel的表格控件
///
public static bool SaveToExcel(string filePath, DataGridView dataGridView)
{
bool result = true;//判断保存状态
FileStream fs = null;//创建一个新的文件流
XSSFWorkbook workbook = null;//创建一个新的Excel文件
ISheet sheet = null;//为Excel创建一张工作表
//定义行数、列数、与当前Excel已有行数
int rowCount = dataGridView.RowCount;//记录表格中的行数
int colCount = dataGridView.ColumnCount;//记录表格中的列数
//这两行是用来获取文件名,用来当标题的
int index = filePath.LastIndexOf("\\") + 1;
string title = filePath.Substring(index, filePath.Length - index - 5);
//创建工作表
try
{
//创建文件流
fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
//创建一个xlsx格式的工作簿
workbook = new XSSFWorkbook();
sheet = workbook.CreateSheet("Sheet1");//创建一个表
//调用方法设置表的表标题格式
SheetRowTitle(workbook, sheet, title);
IRow row = sheet.CreateRow(1);//在表中创建一行
ICellStyle cellStyle = InfoCellStyle(workbook);//设置表中单元格样式
int colCountC = 0;//统计DataGridView有数据的列的数量
//列循环,获取dataGridView中的每一列,将其使用的列写入到excel中
for (int j = 0; j < colCount; j++)
{
//判断dataGridView的列的状态和标题不为空时
if (dataGridView.Columns[j].Visible && dataGridView.Rows[0].Cells[j].Value != null)
{
ICell cell = row.CreateCell(j);//创建列
cell.SetCellValue(dataGridView.Columns[j].HeaderText.ToString());//更改单元格值
sheet.SetColumnWidth(j, 12 * 256);//设置列宽
cell.CellStyle = cellStyle;//设置每一个单元格的格式
colCountC++;
}
}
sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, colCountC - 1));//合并单元,将表中的第一行的存在列全部合并成一个单元格
//填充数据,将dataGridView中的数据写入到excel中
for (int i = 0; i < rowCount; i++) //行循环
{
//防止行数超过Excel限制
if (i >= 65536)
{
result = false;
break;
}
IRow rows = sheet.CreateRow(2 + i); //创建行
for (int j = 0; j < colCountC; j++) //列循环
{
if (dataGridView.Columns[j].Visible && dataGridView.Rows[i].Cells[j].Value != null)
{
ICell cell = rows.CreateCell(j);//创建单元格
cell.CellStyle = cellStyle;//设置单元格样式
cell.SetCellValue(dataGridView.Rows[i].Cells[j].Value.ToString());//更改单元格值
}
}
}
workbook.Write(fs);//将流中的数据通过npoi写入到excel中
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
result = false;
return result;
}
finally
{
if (fs != null)
{
fs.Close();
fs.Dispose();
}
workbook = null;
}
return result;
}
设置单元格格式
///
/// 设置excel内容单元格格式
///
/// excel工作簿对象
/// 返回单元格格式
private static ICellStyle InfoCellStyle(XSSFWorkbook workbook)
{
ICellStyle style = workbook.CreateCellStyle();//创建单元格样式对象
style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CenterSelection;//设置单元格的样式:水平对齐居中
style.VerticalAlignment = VerticalAlignment.Center; //垂直居中
IFont font = workbook.CreateFont();//字体样式对象
font.FontName = "黑体";//设置字体样式
font.FontHeightInPoints = 16;//字体大小
style.SetFont(font);//将字体样式添加到ICellStyle样式对象中
//设置单元格四周的边框
style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
return style;
}
///
/// 设置excel的标题
///
/// excel工作簿对象
/// excel工作表对象
private static void SheetRowTitle(XSSFWorkbook workbook, ISheet sheet, string title)
{
IRow rowTitle = sheet.CreateRow(0);
rowTitle.Height = 35 * 20;
ICell cellTitle = rowTitle.CreateCell(0);
cellTitle.SetCellValue(title);
ICellStyle style = workbook.CreateCellStyle();
style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CenterSelection;//设置单元格的样式:水平对齐居中
style.VerticalAlignment = VerticalAlignment.Center; //垂直居中
IFont font = workbook.CreateFont();
font.IsBold = true;
font.FontName = "黑体";
font.FontHeightInPoints = 24; //设置字体大小
style.SetFont(font);
style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
cellTitle.CellStyle = style;
}