您的位置:首页 > 其它

NPOI导入EXCEL数据类

2016-05-06 09:29 281 查看
使用NPOI能够帮助开发者在没有安装微软Office的情况下读写Office 97-2003的文件,支持的文件格式包括xls, doc, ppt等。NPOI是构建在POI 3.x版本之上的,它可以在没有安装Office的情况下对Word/Excel文档进行读写操作。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using NPOI.HSSF.UserModel;

using System.Data;

using System.IO;

using NPOI.SS.UserModel;

using System.Windows.Forms;

using NPOI.XSSF.UserModel;

namespace CavConOpt.Common

{

public class ExcelHelper

{

static IWorkbook hssfworkbook;

#region 导入数据

public static DataTable ImportExcelFile(string filePath)

{

#region//初始化信息

try

{

using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))

{

hssfworkbook = new HSSFWorkbook(file);

}

}

catch (Exception e)

{

throw e;

}

#endregion

ISheet sheet = hssfworkbook.GetSheetAt(0);

System.Collections.IEnumerator rows = sheet.GetRowEnumerator();

DataTable dt = new DataTable();

for (int j = 0; j < (sheet.GetRow(0).LastCellNum); j++)

{

dt.Columns.Add(Convert.ToChar(((int)'A') + j).ToString());

}

while (rows.MoveNext())

{

HSSFRow row = (HSSFRow)rows.Current;

DataRow dr = dt.NewRow();

for (int i = 0; i < row.LastCellNum; i++)

{

ICell cell = row.GetCell(i);

if (cell == null)

{

dr[i] = null;

}

else

{

if (cell.CellType == CellType.Formula)//如果单元格是公式

{

dr[i] = cell.NumericCellValue.ToString();

}

else

dr[i] = cell.ToString();

}

}

dt.Rows.Add(dr);

}

return dt;

}

public static DataSet ImportExcelFiles(string filePath)

{

#region//初始化信息

try

{

using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))

{

if (filePath.IndexOf(".xlsx") > 0) // 2007版本

hssfworkbook = new XSSFWorkbook(file );

else if (filePath.IndexOf(".xls") > 0) // 2003版本

hssfworkbook = new HSSFWorkbook(file );

}

}

catch (Exception e)

{

throw e;

}

#endregion

DataSet ds = new DataSet();

int numberOfSheets = hssfworkbook.NumberOfSheets;//sheets数量

for (int n = 0; n < numberOfSheets; n++)

{

ISheet sheet = hssfworkbook.GetSheetAt(n);

System.Collections.IEnumerator rows = sheet.GetRowEnumerator();

DataTable dt = new DataTable();

for (int j = 0; j < (sheet.GetRow(0).LastCellNum); j++)

{

dt.Columns.Add(Convert.ToChar(((int)'A') + j).ToString());

}

while (rows.MoveNext())

{

HSSFRow row = (HSSFRow)rows.Current;

DataRow dr = dt.NewRow();

for (int i = 0; i < row.LastCellNum; i++)

{

ICell cell = row.GetCell(i);

if (cell == null)

{

dr[i] = null;

}

else

{

if (cell.CellType == CellType.Formula )//如果单元格是公式

{

dr[i] = cell.NumericCellValue.ToString();

}

else

dr[i] = cell.ToString();

}

}

dt.Rows.Add(dr);

}

ds.Tables.Add(dt);

}

return ds;

}

#endregion

#region 导出excel

//Datatable导出Excel

public static void GridToExcel(DataTable dt, string strExcelFileName)

{

HSSFWorkbook workbook = new HSSFWorkbook();

try

{

ISheet sheet = workbook.CreateSheet("Sheet1");

ICellStyle HeadercellStyle = workbook.CreateCellStyle();

HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;

HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;

HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;

HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;

HeadercellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;

//字体

NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();

headerfont.Boldweight = (short)FontBoldWeight.Bold;

HeadercellStyle.SetFont(headerfont);

//用column name 作为列名

int icolIndex = 0;

IRow headerRow = sheet.CreateRow(0);

foreach (DataColumn item in dt.Columns)

{

ICell cell = headerRow.CreateCell(icolIndex);

cell.SetCellValue(item.ColumnName);

cell.CellStyle = HeadercellStyle;

icolIndex++;

}

ICellStyle cellStyle = workbook.CreateCellStyle();

//为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看

cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");

cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;

cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;

cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;

cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;

NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();

cellfont.Boldweight = (short)FontBoldWeight.Normal;

cellStyle.SetFont(cellfont);

//建立内容行

int iRowIndex = 1;

int iCellIndex = 0;

foreach (DataRow Rowitem in dt.Rows)

{

IRow DataRow = sheet.CreateRow(iRowIndex);

foreach (DataColumn Colitem in dt.Columns)

{

ICell cell = DataRow.CreateCell(iCellIndex);

cell.SetCellValue(Rowitem[Colitem].ToString());

cell.CellStyle = cellStyle;

iCellIndex++;

}

iCellIndex = 0;

iRowIndex++;

}

//自适应列宽度

for (int i = 0; i < icolIndex; i++)

{

sheet.AutoSizeColumn(i);

}

//写Excel

FileStream file = new FileStream(strExcelFileName, FileMode.OpenOrCreate);

workbook.Write(file);

file.Flush();

file.Close();

MessageBox.Show("导出成功");

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

finally { workbook = null; }

}

#endregion

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: