如何利用npoi 将gridview里面的数据导出
但是 首先 是 我已将 模板导入之后 如何将girdview里面的数据一次的导入到这个模板中 并且数据一次的对应的?哪位高手指点 指点
------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Management;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.SS.UserModel;
namespace CorlunBCL
{
public static class ExcelClass
{
public static void ExportToExcel(DataGridView DG, string ExcelFileName)
{
//导出的数据不能超过 256 列、65535 行
//创建数组,保存网格的数据,只有网格中的可见列的内容保存到数组,计算可见的列数
int ColCount = 0;
for (int i = 0; i < DG.ColumnCount; i++)
if (DG.Columns[i].Visible)
ColCount++;
//开始保存数据到 Excel
HSSFWorkbook hssfworkbook;
hssfworkbook = new HSSFWorkbook();
DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
dsi.Company = "";
hssfworkbook.DocumentSummaryInformation = dsi;
SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
si.Subject = "";
hssfworkbook.SummaryInformation = si;
try
{
ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
// 定义格式
ICellStyle cellstyle1 = hssfworkbook.CreateCellStyle();
IFont font = hssfworkbook.CreateFont();
font.FontName = DG.Font.Name;
font.FontHeightInPoints = (short)(Math.Round(DG.Font.SizeInPoints, 0));
cellstyle1.SetFont(font);
// 生成首行(标题行)
string ColumnName = "";
IRow newRow = null;
ICell newCell = null;
newRow = sheet1.CreateRow(0);
for (int i = 0; i < DG.ColumnCount; i++)
{
ColumnName = GetColumnNameByDisplayIndex(DG, i);
if (!DG.Columns[ColumnName].Visible)
continue;
newCell = newRow.CreateCell(i);
newCell.SetCellValue(DG.Columns[ColumnName].HeaderText);
// 设置格式
newCell.CellStyle = cellstyle1;
}
// 填充数据行
for (int i = 0; i < DG.Rows.Count; i++)
{
newRow = sheet1.CreateRow(i + 1);
for (int j = 0; j < DG.ColumnCount; j++)
{
ColumnName = GetColumnNameByDisplayIndex(DG, j);
if (!DG.Columns[ColumnName].Visible)
continue;
newCell = newRow.CreateCell(j);
if (DG.Columns[ColumnName] is DataGridViewCheckBoxColumn)
{
//复选框列类型
if (DG[ColumnName, i].EditedFormattedValue.ToString().ToUpper() == "TRUE")
newCell.SetCellValue("√");
else
newCell.SetCellValue("");
}
else
{
//其他的列类型
object cellObj = DG[ColumnName, i].Value;
Type objType = null;
if (cellObj != null)<