NPOI export to Excel

Some time ago, I have studied Microsoft's Excel export and table output Excel, and they also have some disadvantages:

  1. There are some disadvantages for Microsoft Excel export, such as: Office software needs to be installed, speed problems;

  2. Table output Excel found a fatal drawback in the use process. In certain cases, the data will be lost, because the person in charge of the department will export the data and distribute it to the relevant personnel, and the receiver finds that the data is damaged after receiving it. For data loss, I guess it should belong to the export method. For table output, I guess that some cache files will be generated locally for the exported Excel to dynamically call, but the cached data sent to another machine is not sent, resulting in data loss.

NPOI export to Excel:

  It is indeed very convenient to use NPOI, and it can quickly create the export function we need. The method is simple and straightforward without other dependencies, and the speed is also good.

  1. You need to reference NPOI.dll before use

 

 

    You can retrieve the NPOI and add it to the project through NuGet package management:

 

 

   2. How to use. For simple use, we only need to pay attention to: HSSFWorkbook , ISheet , IRow and ICell , we can know their approximate use from the name, and we can use it like writing an article.

    HSSFWorkbook is equivalent to an Excel object

    ISheet is equivalent to a workbook in Excel

    IRow is equivalent to row

    ICell is equivalent to column

    

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 using NPOI.HSSF.UserModel;
 8 using NPOI.SS.UserModel;
 9 using Sunc.Framework.Repository.Utility.Excel;
10 
11 namespace TestConsole
12 {
13     class Program
14     {
15         static void Main(string[] args)
16         {
17          
18             HSSFWorkbook workBook = new HSSFWorkbook();
19             ISheet sheet = workBook.CreateSheet("工作簿");
20             IRow row = sheet.CreateRow(0);
21             ICell cell = row.CreateCell(0);
22             cell.SetCellValue("孙超");
23 
24             MemoryStream ms = new MemoryStream();
25             workBook.Write(ms);
26             var buf = ms.ToArray();
27             using (FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "a.xls", FileMode.Create, FileAccess.Write))
28             {
29                 fs.Write(buf, 0, buf.Length);
30                 fs.Flush();
31             }
32             Console.ReadKey();
33         }
34 
35 
36     }
37 }

  Of course, SetCellValue is an overloaded method for us to access various types of values

  and style settings

Of course, the demand decides to use it. For Microsoft, we can call macros, which is a relatively powerful function, but we can hardly use it for those with low demand, so the one that suits us is the best.

Personal source code

Reference Manual: NPOI User Manual

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325789460&siteId=291194637