惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
雷峰网
雷峰网
S
Securelist
V
Vulnerabilities – Threatpost
S
SegmentFault 最新的问题
T
The Exploit Database - CXSecurity.com
A
About on SuperTechFans
T
Threat Research - Cisco Blogs
Attack and Defense Labs
Attack and Defense Labs
L
LangChain Blog
Cyberwarzone
Cyberwarzone
Engineering at Meta
Engineering at Meta
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
V
V2EX
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
L
Lohrmann on Cybersecurity
S
Security Affairs
S
Secure Thoughts
P
Privacy & Cybersecurity Law Blog
T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
CXSECURITY Database RSS Feed - CXSecurity.com
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cisco Talos Blog
Cisco Talos Blog
Spread Privacy
Spread Privacy
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security Blog
博客园 - 叶小钗
H
Heimdal Security Blog
L
LINUX DO - 热门话题
月光博客
月光博客
Apple Machine Learning Research
Apple Machine Learning Research
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
D
DataBreaches.Net
N
Netflix TechBlog - Medium
The Hacker News
The Hacker News
N
News and Events Feed by Topic
C
Check Point Blog
博客园_首页
Scott Helme
Scott Helme
T
Troy Hunt's Blog
U
Unit 42

博客园 - 港城大白鲨

SQLServer2022安装记录 Go 语言从入门到理解 Oracle日常问题集锦 IO流操作flush应用场景 java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet 源码、反码、补码详解及Byte结构 SSL证书类型OV、EV、DV说明来源阿里云 Servlet导出Excel,Json content-type的理解和日常实践 .NET版本发布历史 C# 部分类 详解C#中 Thread,Task,Async/Await,IAsyncResult【转】 企业微信-自建应用使用审批流程引擎 SQLAlwayson搭建过程之三 AlwaysOn Failover Cluster Instance SQLAlwayson搭建过程之二 AlwaysOn可用性组 SQLAlwayson搭建过程之一故障转移集群WSFC Sharepoint server2019 通讯组(安全组)发送邮件问题 SQLServer链接服务器 C# 操作Exchange 的powershell以实现邮件撤回
C# 操作Excel 常用整理
港城大白鲨 · 2022-03-04 · via 博客园 - 港城大白鲨

目前使用过的

NPOI 来源于Java POI 有一定的受众群体,但里面的注释很少

Spire.XLS 这个当时是为了用转化为PDF功能才体验使用的,推荐程度一般
EPPlus 这个是比较成熟且官方注释也较多,但注释都是英文的,下面将一一举例

NPOI  读写

try
            {
                //读模板的方式加载
                string filePath = System.Web.HttpContext.Current.Server.MapPath("~/Template/BasicInfoTemplete.xlsx");
                IWorkbook workbook;
                FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read);
                workbook = WorkbookFactory.Create(fs);

                ISheet sheet = workbook.GetSheetAt(0);//获取Excel中的第一个Sheet
                ICellStyle cellStyle = workbook.CreateCellStyle();

                //设置单元格上下左右边框线  
                cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
                cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
                cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
                cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
                //文字水平和垂直对齐方式  
                cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
                cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;

                ICell Cell = null;
                IRow row = null;
                //基础车型列表
                List<UserModel> list = 数据库获取的列表;
                for (int i = 2; i < obj.Count + 2; i++)
                    {
                        row = sheet.CreateRow(i);
                        //第一列的No.
                        Cell = row.CreateCell(0);
                        Cell.CellStyle = cellStyle;
                        Cell.SetCellValue(i - 1);
                        //总共6列
                        for (int j = 1; j <= 5; j++)
                        {
                            Cell = row.CreateCell(j);
                            Cell.CellStyle = cellStyle;
                            Cell.SetCellValue("""""");
                        }
                    }

          //错误案例注释掉,虽然也能达到功能
          /********
var ms = new NpoiMemoryStream(); ms.AllowClose = false; workbook.Write(fs); workbook.Write(ms); ms.Flush(); ms.Position = 0; ms.AllowClose = false; return File(ms, "application/vnd.ms-excel", "基础车型信息参考.xlsx");
    
         **********/
         var ms = new MemoryStream();
        

          workbook.Write(ms);
          //ms.Flush(); 我理解这个是写入文件时候才用到,这里导出应该不需要
          //ms.Seek(0,SeekOrigin.Begin);
          ms.Position = 0;
          return File(ms, "application/vnd.ms-excel", "基础车型信息参考.xlsx");


    } catch (Exception) { throw; } 

//此部分代码为错误代码,之前对Stream不理解导致的一个网上错误案例

//处理 文件流已关闭 public class NpoiMemoryStream : MemoryStream { public NpoiMemoryStream() { AllowClose = true; } public bool AllowClose { get; set; } public override void Close() { if (AllowClose) base.Close(); } }