NOPI Excel 读取公式生成后的数据
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web; namespace PortalPBIRS.Models
{
class ExcelHelper : IDisposable
{
private string fileName = null; //文件名
private IWorkbook workbook = null;
private FileStream fs = null;
private bool disposed;
readonly int EXCEL03_MaxRow = ;
public ExcelHelper(string fileName)
{
this.fileName = fileName;
disposed = false;
} /// <summary>
/// 将excel中的数据导入到DataTable中
/// </summary>
/// <param name="sheetName">excel工作薄sheet的名称</param>
/// <param name="isFirstRowColumn">第一行是否是DataTable的列名</param>
/// <returns>返回的DataTable</returns>
public DataTable ExcelToDataTable(string sheetName, bool isFirstRowColumn)
{
ISheet sheet = null;
DataTable data = new DataTable();
int startRow = ;
try
{
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);//读取文件
if (fileName.IndexOf(".xlsx") > ) // 2007版本
workbook = new XSSFWorkbook(fs);
else if (fileName.IndexOf(".xls") > ) // 2003版本
workbook = new HSSFWorkbook(fs); if (sheetName != null)
{
sheet = workbook.GetSheet(sheetName);//获取文件的分页
if (sheet == null) //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet
{
sheet = workbook.GetSheetAt();
}
}
else
{
sheet = workbook.GetSheetAt();
}
if (sheet != null)
{
int k = , cellCount = ;
IRow firstRow = sheet.GetRow();//获取第一行
while (firstRow == null)
{
firstRow = sheet.GetRow(++k);//排除空行,找到第一个有字符的行
} if (isFirstRowColumn)//第一行是列名
{
cellCount = firstRow.LastCellNum; //一行最后一个cell的编号 即总的列数 for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
{
ICell cell = firstRow.GetCell(i); if (cell != null)
{
string cellValue = "";
if (cell.CellType == CellType.Formula)
{
cellValue = cell.RichStringCellValue.String;
}
else
{
cellValue = cell.StringCellValue;
} if (cellValue != null)
{
DataColumn column = new DataColumn(cellValue.Replace("\n",""));
data.Columns.Add(column);
}
}
}
startRow = sheet.FirstRowNum + ;
}
else// 第一行不是列名
{
int max = ;
for (int i = sheet.FirstRowNum; i <= sheet.LastRowNum; i++)// 找到所有行中最多字符
{
if (sheet.GetRow(i) == null)
continue; max = (sheet.GetRow(i).LastCellNum > max) ? sheet.GetRow(i).LastCellNum : max;
}
cellCount = max;
for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
{
DataColumn column = new DataColumn();
data.Columns.Add(column);
}
startRow = sheet.FirstRowNum;
} //最后一列的标号
int rowCount = sheet.LastRowNum; for (int i = startRow; i <= rowCount; ++i)//将从文件中获取的数据存储到DataTable中
{
IRow row = sheet.GetRow(i); if (row == null)
{
continue; //没有数据的行默认是null
} DataRow dataRow = data.NewRow();
for (int j = row.FirstCellNum; j < cellCount; ++j)
{
//同理,没有数据的单元格都默认是null
if (row.GetCell(j) != null)
{
if (row.GetCell(j).CellType == CellType.Formula)
{
row.GetCell(j).SetCellType(CellType.String); dataRow[j]= row.GetCell(j).RichStringCellValue.String;
}
else
{
dataRow[j] = row.GetCell(j).ToString();
}
}
} data.Rows.Add(dataRow);
}
} return data;
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
return null;
}
} /// <summary>
/// 将DataTable转换为excel2003格式。
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public byte[] DataTableToExcel(DataTable dt, string sheetName)
{ IWorkbook book = new HSSFWorkbook();
if (dt.Rows.Count < EXCEL03_MaxRow)
DataWrite2Sheet(dt, , dt.Rows.Count - , book, sheetName);
else
{
int page = dt.Rows.Count / EXCEL03_MaxRow;
for (int i = ; i < page; i++)
{
int start = i * EXCEL03_MaxRow;
int end = (i * EXCEL03_MaxRow) + EXCEL03_MaxRow - ;
DataWrite2Sheet(dt, start, end, book, sheetName + i.ToString());
}
int lastPageItemCount = dt.Rows.Count % EXCEL03_MaxRow;
DataWrite2Sheet(dt, dt.Rows.Count - lastPageItemCount, lastPageItemCount, book, sheetName + page.ToString());
}
MemoryStream ms = new MemoryStream();
book.Write(ms);
return ms.ToArray();
}
private void DataWrite2Sheet(DataTable dt, int startRow, int endRow, IWorkbook book, string sheetName)
{
ISheet sheet = book.CreateSheet(sheetName);
IRow header = sheet.CreateRow();
for (int i = ; i < dt.Columns.Count; i++)
{
ICell cell = header.CreateCell(i);
string val = dt.Columns[i].Caption ?? dt.Columns[i].ColumnName;
cell.SetCellValue(val);
}
int rowIndex = ;
for (int i = startRow; i <= endRow; i++)
{
DataRow dtRow = dt.Rows[i];
IRow excelRow = sheet.CreateRow(rowIndex++);
for (int j = ; j < dtRow.ItemArray.Length; j++)
{
excelRow.CreateCell(j).SetCellValue(dtRow[j].ToString());
}
} }
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
} protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
if (fs != null)
fs.Close();
} fs = null;
disposed = true;
}
} }
}
row.GetCell(j).CellType == CellType.Formula
NOPI Excel 读取公式生成后的数据的更多相关文章
- EPPlus实战篇——Excel读取
.net core 项目 可以从excel读取任何类型(T)的数据,只要T中的field的[Display(Name = "1233")]中的name==excel column ...
- ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案 try.dot.net 的正确使用姿势 .Net NPOI 根据excel模板导出excel、直接生成excel .Net NPOI 上传excel文件、提交后台获取excel里的数据
ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案 ASP.NET Core 从2.2版本开始,采用了一个新的名为Endpoint的路由方案,与原来的方案在使用上差别不 ...
- 我写的一个ExcelHelper通用类,可用于读取或生成数据
读取或生成EXCEL数据的方法有很多,一般常见的有: 1.通过OFFICE EXCEL组件,优点:读取与生成EXCEL文件方便,缺点:服务器上必须安装OFFICE软件,且进程无法及时释放 2.通过第三 ...
- NPOI 读取excel到DataTable 读取隐藏列 读取公式列
处理思路: 1.打开excel 用NPOI进行读取: 2.读取第一个Sheet: 读取过程中: a.先设置相应列 不隐藏 b.读取Cell时 先判断是否的包含公式 相应代码如下: public sta ...
- Java读取excel指定sheet中的各行数据,存入二维数组,包括首行,并打印
1. 读取 //读取excel指定sheet中的各行数据,存入二维数组,包括首行 public static String[][] getSheetData(XSSFSheet sheet) thro ...
- POI生成EXCEL,公式不自动执行的有关问题
POI生成EXCEL,公式不自动执行的问题 场景:POI读取Excel模板. 当使用POI操作Excel时,发现由POI生成的公式能够在打开Excel是被执行, 而事先手工写入Excel模板文件的公式 ...
- 工作总结 1 sql写法 insert into select from 2 vs中 obj文件和bin文件 3 npoi 模板copy CopySheet 最好先全部Copy完后 再根据生成sheet写数据 4 sheet.CopyRow(rowsindex, rowsindex + x); 5 npoi 复制模板如果出现单元格显示问题
我们可以从一个表中复制所有的列插入到另一个已存在的表中: INSERT INTO table2SELECT * FROM table1; 或者我们可以只复制希望的列插入到另一个已存在的表中: INSE ...
- Excel输入公式后只显示公式却不计算如何解决?
在使用Excel函数公式的时候,您是否碰到过输入公式,按下Enter键之后,单元格仍只显示公式,而不显示计算结果. 工具/原料 Excel 教程以Excel2013为例 方法/步骤 教 ...
- php使用PHPexcel类读取excel文件(循环读取每个单元格的数据)
error_reporting(E_ALL); date_default_timezone_set('Asia/ShangHai'); include_once('Classes/PHPExcel/I ...
随机推荐
- MySQL文本处理函数2_20160921
需求:从目前的 test_a03order 表里面提取出来产品规格,押金的数据 一.首先添加表字段我们在表里面添加这两个字段 命名为product_size,deposit 后期进行更新这两个字段内 ...
- 如何解决outlook2013邮件规则for other machine的失效问题
如何解决outlook2013邮件规则for other machine的失效问题 问题描述:因为重装系统,outlook2013进去后->Rules and Alerts->发现所有原来 ...
- 主备角色switch
理论知识:Switchover 切换允许primary 和一个备库进行切换,并且这种切换没有数据丢失. 前提条件: 1) 主备库相关参数 fal_client.fal_server .standby_ ...
- poj1742硬币——多重背包可行性
题目:http://poj.org/problem?id=1742 贪心地想,1.如果一种面值已经可以被组成,则不再对它更新: 2.对于同一种面值的硬币,尽量用较少硬币(一个)更新,使后面可以用更多此 ...
- DB2 Error Messages (Sorted by SQLCODE)
DB2 Error Messages (Sorted by SQLCODE) DB2 Error Messages (Sorted by SQLCODE) SQLCODE SQLSTATE Descr ...
- linux 中spfvim安装
1. 安装 git 1.1 安装依赖的包: curl curl-devel zlib-devel openssl-devel perl c ...
- linux下libphenom的测试代码
使用说明:测试使用libphenom库的字符串追加函数,效率是strcat的60多倍.所以在进行大量的字符串累加的时候可以考虑使用libphenom库 依赖库: ck-.tar.gz cmake-. ...
- hostent结构体和wsadata结构体
一.hostent结构体 使用这个东西,首先要包含2个头文件:#include <netdb.h>#include <sys/socket.h> struct hostent ...
- 利用C++创建DLL并C#调用
日期:2018年11月26日 环境:window 10,VS2015 community 一.利用C++创建DLL 1.新建项目: 2.打开CreateDLL.cpp文件,并输入测试代码 #inclu ...
- Ajax.BeginForm 的使用
@using (Ajax.BeginForm("AddHomeRule", "MyHome", new AjaxOptions { HttpMethod = & ...