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. 20170218 OO-ALV标准工具栏按钮

    原文地址:OO ALV 工具栏对于的功能码   图标与对应的 功能码 明细 &DETAIL 检查 &CHECK 刷新 &REFRESH 剪切 &LOCAL&CU ...

  2. mysql 数据库导入错误:40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET

    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;/*!40101 SET @OLD_CHARACTER_SET_RES ...

  3. linux下如何用php读取word

    在实际的工作中遇到到要导入word格式的文件,经过努力,终于成功了. 在linux上用PHP读取WORD文档,其实是使用了 antiword程序把word文档转化为txt文档. 再使用php执行系统命 ...

  4. bzoj4169: Lmc的游戏

    终于有道我会的了... int f[2][maxn],g[2][maxn],tot[maxn];//构造叶子编号时希望最大/小result 先手取子树最小/大的编号的排名 tot是子树中叶子个数 如果 ...

  5. loadrunner性能测试步骤

    性能测试过程分为4个阶段:设计.构建.执行.分析/诊断/调节具体的工作流程如下图 设计 > 构建 > 执行 > 分析/诊断/调节 收集要求 设置测试环境 基准测试 诊断瓶颈 设计测试 ...

  6. I.MX6 wpa_supplicant_8 编译问题

    /************************************************************************ * I.MX6 wpa_supplicant_8 编 ...

  7. sublime text 3 安装vue 语法插件

    1.下载https://github.com/vuejs/vue-syntax-highlight,点击这里也可以下载压缩包 2.解压到C:\Users\***\AppData\Roaming\Sub ...

  8. 【TJOI2013】 单词

    [题目链接] 点击打开链接 [算法] AC自动机+递推 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 200 ...

  9. Watir: 当出现错误提示AutoItX3.dll 没有注册的时候,该怎么处理?

    对于Ruby 1.8版本,以管理员身份运行命令行窗口,输入Regsvr32 AutoItX3.dll路径即可.对于1.9 版本,路径与1.8版本是不同的,我们可以进入Ruby安装目录下,搜索AutoI ...

  10. 你真的会使用assert吗?

    写这篇博客源于在阅读lighttpd源代码是遇到的一个关于assert应用的疑问. 在阅读lighttpd源代码时,发现比比皆是的对malloc的调用结果进行assert检查,比如:Buffer.c: ...