首页 技术 正文
技术 2022年11月12日
0 收藏 934 点赞 4,871 浏览 2765 个字

使用NPOI导入导出Excel(xls/xlsx)数据到DataTable中

http://www.cnblogs.com/songrun/p/3547738.html

NPOI 2.0教程 – 自动识别Excel 2003或2007格式

http://tonyqus.sinaapp.com/archives/482

NPOI 2.0 教程

http://tonyqus.sinaapp.com/npoi2tutorial

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.POIFS.FileSystem;
using NPOI.OpenXml4Net.OPC;namespace weirManagement.BaseClass
{
public class NPOIHelper : BaseClass
{
//public class WorkbookFactory
//{
// public static IWorkbook Create(POIFSFileSystem fs){...}
// public static IWorkbook Create(OPCPackage pkg){...}
// public static IWorkbook Create(Stream inputStream){...}
// public static IFormulaEvaluator CreateFormulaEvaluator(IWorkbook workbook){...}
//} /// <summary>
/// Excel某sheet中内容导入到DataTable中
/// 区分xsl和xslx分别处理
/// </summary>
/// <param name="filePath">Excel文件路径,含文件全名</param>
/// <param name="sheetName">此Excel中sheet名</param>
/// <returns></returns>
//public DataTable ExcelSheetImportToDataTable(string filePath, string sheetName)
public DataTable ExcelImportToDataTable(string filePath)
{
DataTable dt = new DataTable();
//.xlsx //.xlsm
#region .xlsx文件处理:XSSFWorkbook
try
{
using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
IWorkbook wb = WorkbookFactory.Create(file);
ISheet sheet = wb.GetSheetAt();//.GetSheet(sheetName);
System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
IRow headerRow = sheet.GetRow();
//一行最后一个方格的编号 即总的列数
for (int j = ; j < sheet.GetRow().LastCellNum; j++)
{
//SET EVERY COLUMN NAME
ICell cell = headerRow.GetCell(j);
if (cell != null && cell.ToString() != "")
{
dt.Columns.Add(cell.ToString());
}
else { continue; } }
int colCount = dt.Columns.Count; while (rows.MoveNext())
{
//IRow row = (XSSFRow)sheet.GetRow(j);
IRow row = (IRow)rows.Current;
DataRow dr = dt.NewRow();
bool addDR = false;
if (row.RowNum == ) continue;//The firt row is title,no need import
for (int i = ; i < colCount; i++)
{
//cell count>column count,then break //每条记录的单元格数量不能大于表格栏位数量 20140213                 
//cell count>column count,then break //每条记录的单元格数量不能大于DataTable的title
if (i >= colCount){ break; } ICell cell = row.GetCell(i); if ((i == ) && (string.IsNullOrEmpty(cell.ToString()) == true))//每行第一个cell为空,break
{
break;
}
if (cell != null)
{
object o = cell;
//读取Excel格式,根据格式读取数据类型
switch (cell.CellType)
{
case CellType.Blank: //空数据类型处理
o = "";
break;
case CellType.String: //字符串类型
o = cell.StringCellValue;
break;
case CellType.Numeric: //数字类型
if (DateUtil.IsCellDateFormatted(cell))
{ o = cell.DateCellValue; }
else
{
o = cell.ToString();
}
break;
case CellType.Formula:
//HSSFFormulaEvaluator e = new HSSFFormulaEvaluator(hssfworkbook);
IFormulaEvaluator e = WorkbookFactory.CreateFormulaEvaluator(wb);
o = e.Evaluate(cell).StringValue;
break;
default:
o = "";
break;
}
dr[i] = Convert.ToString(o);//row.GetCell(j).StringCellValue;
addDR = true;
}
}
if (addDR)
{
dt.Rows.Add(dr);
}
}
}
}
catch (Exception e)
{
throw e;
}
#endregion
return dt;
}
  }
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,024
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,514
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,362
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,776
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,854