当前位置:网站首页>【NPOI】C#跨工作薄复制Sheet模板导出Excel
【NPOI】C#跨工作薄复制Sheet模板导出Excel
2022-06-26 17:18:00 【Guistar~~】
文章介绍
- 只能读xlsx后缀的文件,xls需要自己加
- 因为我公司电脑环境新建一个excel会自动加密,所以我只能先读取一个未加密excel模板(需要找领导申请,改了也会自动加密),再创建一个excel,并在过程中把读到的模板写进去
参考
使用
读取模板
/// <summary>
/// 读取模板
/// </summary>
/// <param name="sheetname">传入一个表名</param>
/// <returns></returns>
public static XSSFWorkbook SheetCopytest(string sheetname)
{
// 模板地址
string templetfilepath = @"C:\Users\10774114\Desktop\SHEET\Ulami科室日报模板.xlsx";//模版Excel
// 生成文件地址
string tpath = @"C:\Users\10774114\Desktop\SHEET\11.xlsx";//中介Excel,以它为中介来导出,避免直接使用模块Excel而改变模块的格式
FileInfo ff = new FileInfo(tpath);
if (ff.Exists)
{
ff.Delete();
}
// 读取模板
FileStream fileRead = new FileStream(templetfilepath, FileMode.Open, FileAccess.Read);
XSSFWorkbook XSSFWorkbook = new XSSFWorkbook(fileRead);
XSSFWorkbook book2 = new XSSFWorkbook();
XSSFSheet CPS = (XSSFSheet)XSSFWorkbook.GetSheetAt(1); // 获得模板sheet
CPS.CopyTo(book2, sheetname, true, true); // 将模板sheet复制到目标工作薄并取名字
return book2;
}
生成Excel
public static string DataTableToExcel()
{
string sheetname = "20220601";
XSSFWorkbook book2 = SheetCopytest(sheetname);
using (FileStream file = new FileStream(@"C:\Users\10774114\Desktop\SHEET\11.xlsx", FileMode.Create, FileAccess.Write))
{
book2.Write(file);
file.Close();
}
return sheetname;
}
边栏推荐
- 类型多样的石膏PBR多通道贴图素材,速来收藏!
- Environment setup mongodb
- Army chat -- registration of Registration Center
- Platform management background and merchant menu resource management: merchant registration management design
- Daily record 2
- LeetCode——226. Flip binary tree (BFS)
- ACL 2022 | 基于神经标签搜索的零样本多语言抽取式文本摘要
- Uncover the secret of Agora lipsync Technology: driving portraits to simulate human speech through real-time voice
- Programmer interview guide - self introduction
- 用redis做用户访问数据统计HyperLogLog及Bitmap高级数据类型
猜你喜欢

分布式架构概述

Discover K8E: minimalist kubernetes distribution

halcon之区域:多种区域(Region)特征(5)

Inspirational. In one year, from Xiaobai to entering the core Department of Alibaba, his counter attack

Ndroid development from introduction to mastery Chapter 2: view and ViewGroup

Basic requirements: 7 problems in singleton mode

Redis and database data consistency

丰富专业化产品线, 江铃福特领睿·极境版上市

Which low code platform is more friendly to Xiaobai? Here comes the professional evaluation!

Web3去中心化存储生态图景
随机推荐
背包问题求方案数
Quantitative contract system development analysis case - detailed explanation of contract quantitative system development scheme
Prometeus 2.34.0 new features
vue--vuerouter缓存路由组件
The king of Internet of things protocol: mqtt
Synchronized description of concurrency
Redis' 43 serial cannons, try how many you can carry
Comp281 explanation
Teach you to learn dapr - 1 The era of net developers
Some explanations for latex CJK
Don't believe it, 98% of programmers are like this
【Unity】在Unity中使用C#执行外部文件,如.exe或者.bat
Daily record 2
[suggested collection] 11 online communities suitable for programmers
Rich professional product lines, and Jiangling Ford Lingrui · Jijing version is listed
一起备战蓝桥杯与CCF-CSP之大模拟炉石传说
分布式架构概述
Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)
The function keeps the value of variable H to two decimal places and rounds the third digit
Leetcode 1169. Query invalid transactions (if the amount of data is small, this problem still needs to be solved by violent enumeration)