当前位置:网站首页>【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;
}
边栏推荐
- 【推荐系统学习】推荐系统架构
- Wechat app mall, review products, upload commodity pictures, and score Commodity Services
- 背包问题求方案数
- Over the weekend: 20000 words! Summary of JVM core knowledge, 18 serial cannons as a gift
- sparksql如何通过日期返回具体周几-dayofweek函数
- Here comes the hero League full skin Downloader
- Leetcode topic [array] -283- move zero
- Comp281 explanation
- Sandboxed container: container or virtual machine
- vue--vuerouter缓存路由组件
猜你喜欢

7 views on NFT market prospect

Byte interview: two array interview questions, please accept

She said she was tired! So tired! I want to change my career

Distributed Architecture Overview

对NFT市场前景的7个看法

Today, I met a "migrant worker" who took out 38K from Tencent, which let me see the ceiling of the foundation

The difference between round and truncate in SQL (round or truncate)

Kubernetes essential tools: 2021

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

20: Chapter 3: develop the pass service: 3: get through the redis server in the program; (it only connects with the redis server and does not involve specific business development)
随机推荐
Platform management background and merchant menu resource management: merchant registration management design
关于FlowUs这一款国民好笔记
#25class的类继承
Viteconfigure project path alias
Redis OM . Net redis object mapping framework
[dynamic planning] Jianzhi offer II 091 Paint the house
[C language] static modifies local variables
Use middleware to record slow laravel requests
What does the equals method compare? Who told you
Cache breakdown! Don't even know how to write code???
Synchronized description of concurrency
Sandboxed container: container or virtual machine
在国金证券开户怎么样?保障安全吗?
20:第三章:开发通行证服务:3:在程序中,打通redis服务器;(仅仅是打通redis服务器,不涉及具体的业务开发)
Programmer interview guide - self introduction
The king of Internet of things protocol: mqtt
Turtle cartography
Microservice architecture practice: business management background and SSO design: SSO design
【推荐系统学习】推荐系统的技术栈
Leetcode daily [2022 - 02 - 16]