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中任意位置开始插入任意数量行,并填充数据的更多相关文章

  1. 开发宏功能:excel中从sheet批量插入

    源数据如图: 宏操作: 生成数据后: 关键操作:在excel中启用开发工具,添加宏,然后添加模块即可,编辑完代码后,自定义功能按钮即可. Sub MakeDataSource() Dim isExis ...

  2. Excel中通过向导方式插入chart

    1.插入图表则主要是操作ChartObject对象和Chart对象. Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet); Workshe ...

  3. 在Excel中粘贴时怎样跳过隐藏行

    http://www.excel123.cn/Article/exceljichu/201203/932.html 有时在筛选后需要将其他区域中的连续行数据复制粘贴到筛选区域,以替换筛选后的数据.由于 ...

  4. C语言中链表任意位置怎么插入数据?然后写入文件中?

    链表插入示意图:(图是个人所画)因为链表指针指来指去,难以理解,所以辅助画图更加方便. 插入某个学号后面图: 定义的结构体: struct student { ]; //学生学号 ]; //学生姓名 ...

  5. Azure DevOps Server(TFS): 在Excel中解除服务器同步

    通过Azure DevOps Server 提供与Excel集成的功能,用户可以非常便捷地使用Excel,实现工作项数据的同步. 对于需要批量处理数据.离线工作.制作临时报表的用户来说,这个功能必定成 ...

  6. [转] JAVA中读取网络中的图片资源导入到EXCEL中

    需求 导出人员的信息并且加上人员的照片至EXCEL中 完整的代码 //创建一个表格 HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb ...

  7. python接口自动化测试之根据excel中的期望结果是否存在于请求返回的响应值中来判断用例是否执行成功

    1.首先在excel中填写好预期结果的值 这里判断接口成功的依据是预期结果值是否存在于接口的返回数据中. 一般接口的返回值都是json对象,我们需要将json对象转换为json格式的字符串 如下图,进 ...

  8. 数据可视化之powerBI基础(十一)Power BI中的数据如何导出到Excel中?

    https://zhuanlan.zhihu.com/p/64415543 把Excel中数据加载到PowerBI中我们都已经熟悉了,但是怎么把在PowerBI中处理好的数据导出到Excel中呢?毕竟 ...

  9. 使用Npoi向Excel中插入图片

    先把数据库中的数据都导入到Excel表格中,把图片地址的路径全部转成绝对路径. 使用Npoi读取刚导出的Excle表格,把图片那个单元格的图片路径读出来,然后用文件流读取图片,然后通过Npoi把图片放 ...

随机推荐

  1. linux里面源码安装imagemagick库

    在搞树莓派的时候想搞一下树莓派中摄像头获取图像之后传给安卓,安卓进行展示. 恰好用到了imagemagick这个库,我就像正常一样进行安装,sudo apt-get install Imagick 但 ...

  2. Oracle 的 oracle 数据库分类

    一.数据库分类 1.小型数据库:access.foxbase 2.中型数据库:informix.sql server.mysql 3.大型数据库:sybase.db2.oracle 二.项目中如何合理 ...

  3. 12 Flutter仿京东商城项目 商品列表页面请求数据、封装Loading Widget、上拉分页加载更多

    ProductList.dart import 'package:flutter/material.dart'; import '../services/ScreenAdaper.dart'; imp ...

  4. 启动Spring boot报错:nested exception is java.sql.SQLException: Field 'id' doesn't have a default value

    先看具体日志: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with n ...

  5. 实现在线阅读WORD,PDF等文件,JAVA,PHP都可以

    1 <?php 2 //header("Content-type:text/html;charset=utf-8"); 3 //word转html 展示 4 $lj=$_GE ...

  6. GCN入门理解

    图是信息的最佳表示方式.在一个图中,有通过边(谓之“关系”)连接起来的节点(谓之“实体”).想一想,你的Facebook社交网络是个什么样子的:以你为中心连接上你的朋友们,他们又以不同的方式相互联系. ...

  7. WordPress自定义循环

    我们在学WordPress的时候,最常用到的就是循环了.写模板的时候,多数的时间都是和循环打交道的.如果你不能很详细的了解WordPress的循环,是很难写出模板来的. 而WordPress自定义循环 ...

  8. XSS 攻击的预防

    XSS 攻击有两大要素: 1.攻击者提交恶意代码. 2.浏览器执行恶意代码. 针对第一个要素:我们是否能够在用户输入的过程,过滤掉用户输入的恶意代码呢? 输入过滤 在用户提交时,由前端过滤输入,然后提 ...

  9. pytest.mark.parametrize()参数化的应用一

    from page.LoginPage import Loginpage import os, sys, pytest base_dir = os.path.dirname(os.path.dirna ...

  10. 【记录】编译安装 YAML 扩展

    转自:https://learnku.com/articles/30985 Yaml,专门用来写配置文件的语言   依赖安装 $ sudo apt-get install libyaml-dev   ...