NPOI 实现在已存在的Excel中任意位置开始插入任意数量行,并填充数据
1 npoi版本2.1.3.1
2 需要添加的引用:
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.POIFS.FileSystem;
using NPOI;
using NPOI.OpenXml4Net.OPC;
3 调用方式
OperationExcel oe = new OperationExcel(,);第一行开始插入5行,第三个参数是对应要添加到新添加行的每一列的数据
oe.EditorExcel(savePath, readPath,oe);
4 分装好的类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.POIFS.FileSystem;
using NPOI;
using NPOI.OpenXml4Net.OPC; namespace TransactionToString
{
public class OperationExcel
{
private int insertRowIndex;
private int insertRowCount;
private Dictionary<int, string> insertData;
public OperationExcel(int insertRowIndex, int insertRowCount,Dictionary<int,string> insertData=null)
{
if (insertData!=null)
{
this.insertData = insertData;
}
this.insertRowIndex = insertRowIndex;
this.insertRowCount = insertRowCount;
}
private IWorkbook NPOIOpenExcel(string filename)
{
IWorkbook myworkBook;
Stream excelStream = OpenResource(filename);
if (POIFSFileSystem.HasPOIFSHeader(excelStream))
return new HSSFWorkbook(excelStream);
if (POIXMLDocument.HasOOXMLHeader(excelStream))
{
return new XSSFWorkbook(OPCPackage.Open(excelStream));
}
if (filename.EndsWith(".xlsx"))
{
return new XSSFWorkbook(excelStream);
}
if (filename.EndsWith(".xls"))
{
new HSSFWorkbook(excelStream);
}
throw new Exception("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
} private Stream OpenResource(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
return fs;
} //插入
private void InsertRow(ISheet sheet,int insertRowIndex,int insertRowCount,IRow formatRow)
{
sheet.ShiftRows(insertRowIndex, sheet.LastRowNum, insertRowCount, true, false);
for (int i = insertRowIndex; i < insertRowIndex+insertRowCount; i++)
{
IRow targetRow = null;
ICell sourceCell = null;
ICell targetCell = null;
targetRow = sheet.CreateRow(i);
for (int m = formatRow.FirstCellNum; m < formatRow.LastCellNum; m++)
{
sourceCell = formatRow.GetCell(m);
if (sourceCell==null)
{
continue;
}
targetCell = targetRow.CreateCell(m);
targetCell.CellStyle = sourceCell.CellStyle;
targetCell.SetCellType(sourceCell.CellType); }
} for (int i = insertRowIndex; i < insertRowIndex + insertRowCount; i++)
{
IRow firstTargetRow = sheet.GetRow(i);
ICell firstSourceCell = null;
ICell firstTargetCell = null; for (int m = formatRow.FirstCellNum; m < formatRow.LastCellNum; m++)
{
firstSourceCell = formatRow.GetCell(m, MissingCellPolicy.CREATE_NULL_AS_BLANK);
if (firstSourceCell == null)
{
continue;
}
firstTargetCell = firstTargetRow.GetCell(m, MissingCellPolicy.CREATE_NULL_AS_BLANK);
firstTargetCell.CellStyle = firstSourceCell.CellStyle;
firstTargetCell.SetCellType(firstSourceCell.CellType);
if (this.insertData!=null&&this.insertData.Count>)
{
firstTargetCell.SetCellValue(insertData[m]);
}
firstTargetCell.SetCellValue("test");
}
} } public void WriteToFile(IWorkbook workbook,string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
using (FileStream fs=new FileStream(filename,FileMode.OpenOrCreate,FileAccess.Write))
{
workbook.Write(fs);
fs.Close();
}
} public void OpenExcel(string filename)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = filename;
process.StartInfo.ErrorDialog = true;
process.Start();
} public void EditorExcel(string savePath, string readPath, OperationExcel oe)
{
try
{
IWorkbook workbook = oe.NPOIOpenExcel(readPath);
if (workbook == null)
{
return;
}
int sheetNum = workbook.NumberOfSheets;
for (int i = ; i < sheetNum; i++)
{
ISheet mysheet = workbook.GetSheetAt(i);
//获取原格式行
IRow mySourceRow = mysheet.GetRow(insertRowIndex);
oe.InsertRow(mysheet, insertRowIndex, insertRowCount, mySourceRow);
} oe.WriteToFile(workbook, savePath);
oe.OpenExcel(savePath);
}
catch (Exception ex)
{ throw new Exception(ex.Message);
} }
}
}
5 不足之处欢迎留言讨论
NPOI 实现在已存在的Excel中任意位置开始插入任意数量行,并填充数据的更多相关文章
- 开发宏功能:excel中从sheet批量插入
源数据如图: 宏操作: 生成数据后: 关键操作:在excel中启用开发工具,添加宏,然后添加模块即可,编辑完代码后,自定义功能按钮即可. Sub MakeDataSource() Dim isExis ...
- Excel中通过向导方式插入chart
1.插入图表则主要是操作ChartObject对象和Chart对象. Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet); Workshe ...
- 在Excel中粘贴时怎样跳过隐藏行
http://www.excel123.cn/Article/exceljichu/201203/932.html 有时在筛选后需要将其他区域中的连续行数据复制粘贴到筛选区域,以替换筛选后的数据.由于 ...
- C语言中链表任意位置怎么插入数据?然后写入文件中?
链表插入示意图:(图是个人所画)因为链表指针指来指去,难以理解,所以辅助画图更加方便. 插入某个学号后面图: 定义的结构体: struct student { ]; //学生学号 ]; //学生姓名 ...
- Azure DevOps Server(TFS): 在Excel中解除服务器同步
通过Azure DevOps Server 提供与Excel集成的功能,用户可以非常便捷地使用Excel,实现工作项数据的同步. 对于需要批量处理数据.离线工作.制作临时报表的用户来说,这个功能必定成 ...
- [转] JAVA中读取网络中的图片资源导入到EXCEL中
需求 导出人员的信息并且加上人员的照片至EXCEL中 完整的代码 //创建一个表格 HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb ...
- python接口自动化测试之根据excel中的期望结果是否存在于请求返回的响应值中来判断用例是否执行成功
1.首先在excel中填写好预期结果的值 这里判断接口成功的依据是预期结果值是否存在于接口的返回数据中. 一般接口的返回值都是json对象,我们需要将json对象转换为json格式的字符串 如下图,进 ...
- 数据可视化之powerBI基础(十一)Power BI中的数据如何导出到Excel中?
https://zhuanlan.zhihu.com/p/64415543 把Excel中数据加载到PowerBI中我们都已经熟悉了,但是怎么把在PowerBI中处理好的数据导出到Excel中呢?毕竟 ...
- 使用Npoi向Excel中插入图片
先把数据库中的数据都导入到Excel表格中,把图片地址的路径全部转成绝对路径. 使用Npoi读取刚导出的Excle表格,把图片那个单元格的图片路径读出来,然后用文件流读取图片,然后通过Npoi把图片放 ...
随机推荐
- Java同步数据结构之PriorityBlockingQueue
前言 接下来继续BlockingQueue的另一个实现,优先级阻塞队列PriorityBlockingQueue.PriorityBlockingQueue是一个无限容量的阻塞队列,由于容量是无限的所 ...
- leetcode 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- oracle-sql脚本
select * from dba_users; create tablespace kyc_coo; create user kyc_coo identified by "123456&q ...
- 009-DNS域名解析系统
一.概述 DNS是域名系统(DomainNameSystem)的缩写,该系统用于命名组织到域层次结构中的计算机和网络服务.域名是由圆点分开一串单词或缩写组成的,每一个域名都对应一个惟一的IP地址,在I ...
- 获取当前运行的exe路径
void GetAppPath(CString& path) { TCHAR str[] = {}; GetModuleFileName(NULL,str,); wchar_t *pszPos ...
- python基础----redis模块
数据库 关系型数据 例如mysql,有表还有约束条件等 非关系型 k-v形式 memcache 存在内存中 redis 存在内存 mongodb 数据存在磁盘 import redis #string ...
- 深度优先dfs与广度bfs优先搜索总结+例题
DFS(Deep First Search)深度优先搜索 深度优先遍历(dfs)是对一个连通图进行遍历的算法.它的思想是从一个顶点开始,沿着一条路一直走到底,如果发现不能到达目标解,那就返回到上一个节 ...
- Arduino基础入门—1.Hello World
1.首先是下载.安装IDE. https://www.arduino.cc/en/Main/Software 2.连接开发板到电脑. 将UNO R3开发板通过数据线连接到电脑.打开设备管理器查看驱动是 ...
- kali PIN码破解
airmon-ng start wlan0 //开启网卡airodump-ng wlan0mon //监听模式,查找开启wps的apreaver -i wlan0mon -b [ap’s m ...
- 包银消费CTO汤向军:消费金融大数据风控架构与实践
1 业务架构 风控平台是相对独立的系统,信审的案件可以从借款端平台推过来,也可以从第三方平台推过来.信审案件到达风控平台后,自动创建工作流,根据风控流程处理各流程环节任务. •自动决策 风控流程自动处 ...