NPOI功能强大,不用装Excel,就可以操作表格中数据----Excel.Sheet------>DataTable

 private IWorkbook workbook = null;
private ISheet sheet = null;
private string fileName = "";//文档路径
private FileStream fs = null; public ExcelHelper()
{
}
//构造函数
public ExcelHelper(string file)
{
this.fileName = file;
}
/// <summary>
/// 用NPOI从Excel到DatTable
/// </summary>
/// <returns></returns>
public DataTable ExcelToDataTable()
{
DataTable dt = new DataTable();
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
if (System.IO.Path.GetExtension(fileName) == ".xls")
{
workbook = new HSSFWorkbook(fs);
}
else if (System.IO.Path.GetExtension(fileName) == ".xlsx")
{
workbook = new XSSFWorkbook(fs);
}
else
{
throw new Exception("文件类型错误");
}
sheet = workbook.GetSheetAt();
if (sheet != null)
{
IRow firstRow = sheet.GetRow(); //表头
for (int i = firstRow.FirstCellNum; i < firstRow.LastCellNum; i++)
{
ICell cell = firstRow.GetCell(i);
if (cell != null)
{
DataColumn column = new DataColumn(cell.StringCellValue);
dt.Columns.Add(column);
}
}
//表数据 Access数据库时,加“等号”-j <= sheet.LastRowNum
for (int j = sheet.FirstRowNum + ; j <= sheet.LastRowNum; j++)
{
IRow row = sheet.GetRow(j);
if (row == null)
{
continue;
}
DataRow dataRow = dt.NewRow();
for (int i = row.FirstCellNum; i < firstRow.LastCellNum; i++)
{
ICell cell = row.GetCell(i);
//加日期处理
if (cell.CellType == CellType.Numeric && DateUtil.IsCellDateFormatted(cell))
{
dataRow[i] = cell.DateCellValue.ToString();
}
else
{
dataRow[i] = cell.ToString();
}
}
dt.Rows.Add(dataRow);
}
}
return dt;
}

用NPOI从Excel到DataTable的更多相关文章

  1. 使用NPOI读取Excel到DataTable

    一.NPOI介绍: 使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 WORD/EXCEL 文档进行读写.NPOI是构建在POI 3.x版本之上的,它可以在没有安装Office ...

  2. NPOI操作Excel导入DataTable中

    using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System.Data; using System.IO; using NPOI.X ...

  3. NPOI 读取excel到DataTable 读取隐藏列 读取公式列

    处理思路: 1.打开excel 用NPOI进行读取: 2.读取第一个Sheet: 读取过程中: a.先设置相应列 不隐藏 b.读取Cell时 先判断是否的包含公式 相应代码如下: public sta ...

  4. NPOI导入excel为datatable (xls xlsx xlsm)

    使用NPOI导入导出Excel(xls/xlsx)数据到DataTable中 http://www.cnblogs.com/songrun/p/3547738.html NPOI 2.0教程 – 自动 ...

  5. NPOI 将excel转换为datatable或者将datatable转换为excel

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. NPOI对Excel的操作(Sheet转DataTable、List<T>)

    通过NPOI对Excel进行操作,这里主要是读取的操作.封装到ExcelHelper操作类中. 1 using System.Collections.Generic; 2 using NPOI.HSS ...

  7. [转].net 使用NPOI或MyXls把DataTable导出到Excel

    本文转自:http://www.cnblogs.com/yongfa365/archive/2010/05/10/NPOI-MyXls-DataTable-To-Excel-From-Excel.ht ...

  8. Excel操作--使用NPOI导入导出Excel为DataTable

    1.ExcelHelper封装 namespace NPOI操作Excel { public class ExcelHelper { /// <summary> /// DataTable ...

  9. 利用npoi把多个DataTable导入Excel多个sheet中

    { 题外拓展:把datatable插入dataset DataTable fuben = new DataTable();//定义的datatablefuben = table.Tables[0].C ...

随机推荐

  1. 使用libcurl的包装库cpr发起http请求

    cpr GitHub地址https://github.com/whoshuu/cpr 简单示例:cpr_http_request.cpp #include <iostream> #incl ...

  2. Hadoop一些要注意的点

    1.大多小文件的劣处: a. 生成更多的map任务,额外的开销: b. 每个文件都需要守址时间: c. HDFS上namenode需要占用内存空间:

  3. HDU2089 不要62 —— 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 不要62 Time Limit: 1000/1000 MS (Java/Others)    M ...

  4. 织梦CMS首页、列表页文章如何调出该文章TAG标签?

    1.如果是dedecms v5.7版本直接使用标签 [field:id function=GetTags(@me)/] 就可以调用出来了.只不过不带连接的. 2.如果需要连接请注释掉include/h ...

  5. 时间:NSTimer,代码时运行时间段,

    一:NSTimer 当时间间隔>1s是用NSTimer; 方法: [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selecto ...

  6. 【AC自动机&&Trie图】积累

    以前KMP和后缀系列(主要是后缀数组,后缀自动机),都刷了一定数量的题,但是对于AC自动机,却有些冷落,罪过. 但是我感觉,在蓝桥杯比赛中AC自动机出现的概率比后缀系列大,简单的会考匹配,稍难一点会考 ...

  7. Oracle常用数据库表操作

    配置数据库: user:orcl.passward:71911.Hao全局数据库名:orcl..解锁数据库用户名,SCOTT,SYSTEM,SYS,   PWD:71911.Hao输入sqlplus, ...

  8. AJAX如何传递json对象给后端

    如果页面上一直报错,根本没有触发异步请求的话,首先就要检查接口或者路径是否写对或者写全,在去考虑是否跨境的问题. 如果想要给后端传递一个json对象,需要在路径上一句添加content:applica ...

  9. [TJOI2012]防御

    https://www.zybuluo.com/ysner/note/1332539 题面 戳我 解析 一道挺棒棒的线段树. 显然一次伤害到来时我们要先看看区间内哪些点的护甲没了. 这个可以通过维护区 ...

  10. 【旧文章搬运】对抗RKU的StealthCode检测

    原文发表于百度空间,2009-07-02========================================================================== 快一个月没 ...