In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you. NET Core the first example of Office open source cross-platform component NPOI Core how to use, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.
Preface
In the recent project, we need to use Excel export, look around and find that there are no Office components that are not suitable for .NET Core and do not rely on Office and operating system restrictions, so I came up with the idea of adapting NPOI and porting it to .NET Core.
The way to transplant NPOI is not as easy as imagined, because it relies on System.Drawing and System.Window.Forms components, as well as a third-party SharpZipLib library. After GitHub cloned the latest code and converted it to NetStandrad 1.6, there were countless errors. There must be thousands of errors. After a day of hard work (including deletion, modification, rewriting), the number of errors has been reduced to more than 100,50. More than 20, compiled through.
In the process of porting, you can really feel the hard work of the authors of NPOI in writing this code, because NPOI was originally written on the .net Framework 1.1. at that time, there were no generics, no var, and not many off-the-shelf class libraries, all of which were implemented by the most basic data structures, although at present, many of the writing methods can be greatly simplified. But it was really not easy under the conditions at that time.
After passing the compilation, I thought it would not be a big problem, so I tested it. Unfortunately, after a long period of adjustment, I was going to give up. So I went to github to search to see if there were any other solutions and so on. I accidentally found a project of NPOI.Core, a NPOI transplanted by foreigners to the Core platform. Someone had already done the migration of Core. After cloning it, I found that it was compiled, but I went in to take a look at the code. This library currently depends on the Windows platform, while our project is run on CentOS, which cannot be run on Linux. It seems that it is still an empty joy.
What shall I do? So, once again, refactoring begins, and with the previous experience of refactoring, this time it can be said to be familiar with it. The NPOI Core library uses a lot of data structures such as Hashtable and ArrayList, which are not supported by the .NET Core netstandrad standard, which have been replaced by the new generic Directory and List, and the dependent SharpZipLib and other compression components have also been replaced by the implementation of NetStandrad. Of course, there are many other miscellaneous not to elaborate on, finally. Finally, it was compiled under netstandrad 1.6.
After passing, a new project was built under the local visual studio, and the function of exporting Excel was simply tested. There was no problem, and there was no error. I was very happy. At this time, I was thinking that the most important thing is whether it can work properly on Linux. In fact, at this time, I thought to myself that I have replaced all the classes that rely on the .NET Framework with net standrad, which should not be a problem.
Then after a meal of dotnet publish, the deployment package was transferred to Linux for testing, and sure enough, it passed without throwing any exceptions, and Excel was also generated. Transfer Excel to windows and open it with office. Perfect.
Then we went on with various tests, and when we tested the export function of Word, something went wrong. Because NPOI naturally does not support the functions of Word very well, I wonder if it is not possible to export under .NET Framewok, so a new project of .NET Framework uses the dll test provided by NPOI Team and finds that it can be exported. Then start with the code, all kinds of testing, refactoring, and finally there is no problem with Word's export function. Then it was sent to Linux for testing, there was no problem, the stone in my heart fell to the ground.
After passing the test, when I wanted to push the adjusted code to the original author in the form of PR, I found that the original author no longer maintained the project and had no choice but to release NuGet on his own.
So I republished it to NuGet. If you have any problems in the process of using it, you can submit issue under my github.
GitHub: https://github.com/yuleyule66/Npoi.Core
NuGet:
Getting Started Export Excel
This sample code contains:
Multiple Sheet
Merge cells
Automatically adjust column width
Fill background color
Var newFile = @ "newbook.core.xlsx"
Using (var fs = new FileStream (newFile, FileMode.Create, FileAccess.Write)) {IWorkbook workbook = new XSSFWorkbook (); ISheet sheet1 = workbook.CreateSheet ("Sheet1"); sheet1.AddMergedRegion (new CellRangeAddress (0,0,0,10))
Var rowIndex = 0; IRow row = sheet1.CreateRow (rowIndex); row.Height = 30 * 80; row.CreateCell (0). SetCellValue ("this is the cell content and can be set for a long time to see if the column width can be adjusted automatically"); sheet1.AutoSizeColumn (0); rowIndex++
Var sheet2 = workbook.CreateSheet ("Sheet2")
Var style1 = workbook.CreateCellStyle (); style1.FillForegroundColor = HSSFColor.Blue.Index2; style1.FillPattern = FillPattern.SolidForeground
Var style2 = workbook.CreateCellStyle (); style2.FillForegroundColor = HSSFColor.Yellow.Index2; style2.FillPattern = FillPattern.SolidForeground
Var cell2 = sheet2.CreateRow (0) .CreateCell (0); cell2.CellStyle = style1; cell2.SetCellValue (0); cell2 = sheet2.CreateRow (1) .CreateCell (0); cell2.CellStyle = style2; cell2.SetCellValue (1); cell2 = sheet2.CreateRow (2) .CreateCell (0); cell2.CellStyle = style1; cell2.SetCellValue (2); cell2 = sheet2.CreateRow (3) .CreateCell (0); cell2.CellStyle = style2; cell2.SetCellValue (3) Cell2 = sheet2.CreateRow (4) .CreateCell (0); cell2.CellStyle = style1; cell2.SetCellValue (4); workbook.Write (fs);} Export Word
This sample code contains:
Set the way paragraphs are aligned to them
Set paragraph fonts
Set paragraph indentation
This is the font size.
Var newFile2 = @ "newbook.core.docx"
Using (var fs = new FileStream (newFile2, FileMode.Create, FileAccess.Write)) {XWPFDocument doc = new XWPFDocument ()
Var p0 = doc.CreateParagraph (); p0.Alignment = ParagraphAlignment.CENTER; XWPFRun R0 = p0.CreateRun (); r0.FontFamily = "microsoft yahei"; r0.FontSize = 18; r0.IsBold = true; r0.SetText ("here is the title")
Var p1 = doc.CreateParagraph (); p1.Alignment = ParagraphAlignment.LEFT; p1.IndentationFirstLine = 500; XWPFRun R1 = p1.CreateRun (); r1.FontFamily = "imitating Song"; r1.FontSize = 12; r1.IsBold = true R1.SetText ("here is the text, here is the text"); doc.Write (fs);} the above is the .NET Core's first example of how to use Office open source cross-platform component NPOI Core. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.