This article is a mirror article of machine translation, please click here to jump to the original article.

View: 49496|Reply: 2

[Source] NPOI Operation Excel Detailed Explanation

[Copy link]
Posted on 2021-5-16 21:04:14 | | | |
NPOI can read and write Word or Excel documents without Office installed. NPOI is an open-source C# project for reading and writing Microsoft OLE2 component documents such as Excel and WORD.
Review:

NPOI judgment of cell date type
https://www.itsvse.com/thread-9649-1-1.html

Import Pictures to Excel Sheets Using NPOI [with Source Code]
https://www.itsvse.com/thread-9477-1-1.html

NPOI sets the width and height of the cells
https://www.itsvse.com/thread-3522-1-1.html

. NPOI read and write operation Excel table plug-in for Net
https://www.itsvse.com/thread-2288-1-1.html

Assembly name
modules/namespaces
illustrate
NPOI.DLL
NPOI. POIFS
OLE2/ActiveX document attribute read/write library
NPOI.DLL
NPOI. DDF
Microsoft Office Drawing read/write library
NPOI.DLL
NPOI. HPSF
OLE2/ActiveX document read/write library
NPOI.DLL
NPOI. HSSF
Microsoft Excel BIFF (Excel 97-2003, doc) format read/write library
NPOI.DLL
NPOI. SS
Excel public interface and Excel formula calculation engine
NPOI.DLL
NPOI. Util
The basic library provides many useful functions for the development of other read/write file format projects
NPOI. OOXML.DLL
NPOI. XSSF
Excel 2007 (xlsx) format read and write library
NPOI. OOXML.DLL
NPOI. XWPF
Word 2007 (docx) format read/write library
NPOI. OpenXml4Net.DLL
NPOI. OpenXml4Net
OpenXml underlying zip package read and write library
NPOI. OpenXmlFormats.DLL
NPOI. OpenXmlFormats
Microsoft Office OpenXml object relationship library

HSSFWorkbook: is a version of Excel 2003 or earlier (including 2003) with a .xls extension
XSSFWorkbook: is the version of Excel 2007 with .xlsx extension
Automatically recognizes Excel versions

When reading excel, npoi calls WorkbookFactory.Create to automatically recognize the version:



Source code address:The hyperlink login is visible.



Implementation code:

XSSFWorkbook 2007 throws a Cannot access a closed Stream exception



Test code:



When exporting a file to a stream, calling the Write method throws an exception:



Unhandled exception. System.ObjectDisposedException: Cannot access a closed Stream.
   at System.IO.MemoryStream.Seek(Int64 offset, SeekOrigin loc)

HSSFWorkbook does not have this problem, and there is no way to overload the IWorkbook interface Write, as shown in the figure below:



ButThe XSSFWorkbook object will have an additional leaveOpen parameter, source code address:The hyperlink login is visible.



Solution 1:

Solution 2:

Create a new NpoiMemoryStream object and override the Close method as follows:

Use it as follows:

XSSFWorkbook CopySheet exception

When the sheet is my custom template, calling the CopySheet method will throw the following exception:

Object reference not set to an instance of an object.

Clone sheet using CloneSheet, the code is as follows:



(End)




Previous:Redis MISCONF Redis is configured to save RDB snapshots
Next:How to save a stream to a file in C#/.NET
Posted on 2021-9-17 09:46:51 |
kk learn
Posted on 2023-1-29 15:03:59 |
Learn from the little slag
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com