如何利用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)
objType = cellObj.GetType();
if (cellObj == null)
newCell.SetCellValue("");
else if (objType == typeof(DBNull))
newCell.SetCellValue("");
else if (objType == typeof(short))
newCell.SetCellValue((short)cellObj);
else if (objType == typeof(int))
newCell.SetCellValue((int)cellObj);
else if (objType == typeof(long))
newCell.SetCellValue((long)cellObj);
else if (objType == typeof(float))
newCell.SetCellValue((float)cellObj);
else if (objType == typeof(double))
newCell.SetCellValue((double)cellObj);
else if (objType == typeof(decimal))
newCell.SetCellValue((double)((decimal)cellObj));
else
newCell.SetCellValue(cellObj.ToString());
}
// 设置格式
newCell.CellStyle = cellstyle1;
}
}
// 自动列宽
for (int i = 0; i < ColCount; i++)
sheet1.AutoSizeColumn(i);
FileStream fs = new FileStream(ExcelFileName, FileMode.Create);
hssfworkbook.Write(fs);
fs.Close();
}
catch (Exception Err)
{
//出错处理...
}
}
public static string GetColumnNameByDisplayIndex(DataGridView DG, int DisplayIndex)
{
for (int i = 0; i < DG.ColumnCount; i++)
{
if (DG.Columns[i].DisplayIndex == DisplayIndex)
return DG.Columns[i].Name;
}
throw new Exception("列的索引参数无效,超过了列的总数。");
}
}
}
[解决办法]
看着二楼的没问题阿,一看是答案。。。。。