Summary: Excel is powerful and easy to use, so that everyone likes to export data as Excel backup. The traditional excelautomation technology has many shortcomings, such as: Need the target machine installs Office, the resource consumes the big, etc. the advantage of using Npoi: binary read and write files, no office dependencies, high efficiency. In this paper, the technical characteristics of Npoi are described in detail, and the sample code for exporting Excel is given.
The power of Excel is that it not only opens documents in Excel format, it also opens documents in the CSV format, tab format, Websitetable, and more. Because Excel is powerful and easy to use, everyone likes to export the data as Excel backup.
In the online search for "C # EXCEL Export", the most-skipped example uses "Excelautomation technology" and references "Microsoft.Office.Interop.Excel". The component can also be exported, but there are some disadvantages:
(a) The traditional operation of Excel encountered problems:
1, if it is . NET, need to install Officeon the server, and update it in time, in case of vulnerability, also need to set permissions to allow . NET access to COM +, If something goes wrong during the export process, the server may be down.
2,Excel will only contain the number of columns for type conversion, originally text-based,Excel will convert it into a numeric type, such as the number 000123 will become 123.
3, export, if the field content with "-" or "=" beginning, Excel will use it as a formula, will be error.
4,Excel will analyze the data type according to the first 8 rows of the Excel file, if it happens that the first 8 rows of a column is just a number, it will consider the column as a numeric type, automatically convert the columns to resemble 1.42702E+ 17 format, the date column becomes the containing date and number.
(ii) Advantages of using Npoi
1. You can use the framework completely for free
2. Contains most Excel features (cell style, data format, formula, etc.)
3. Professional Technical Support Service (24*7) (not free)
4. The file format that supports processing includes XLS, xlsx, docx.
5. Adopt an interface-oriented design architecture (you can view the NPOI.SS namespace)
6. Support file import and export at the same time
7.. NET 2.0 also supports xlsx and docx formats (and of course support. net4.0)
8. A lot of successful and real test cases from all over the world
9, a large number of instance code
11, you do not need to install Microsoft Officeon the server, you can avoid copyright issues.
12, the use of more than Officepia API More convenient, more humane.
13, you do not have to spend a great effort to maintain npoi,npoi Team will continue to update, improve Npoi, absolute cost savings.
Npio Detailed Description: http://baike.baidu.com/view/4177704.htm
Npio: https://npoi.codeplex.com/releases
How to use:
1 introduction of two DLL files: NPOI.dll, Ionic.zip.dll
2 Writing code
Use this code to export a simple Excel file to D:/1.xls
The code looks like this:
Workbook workbook, Sheet page, row line, cell cell hssfworkbook hssfworkbook = new Hssfworkbook (); Isheet Sheet1 = Hssfworkbook. Createsheet ("Sheet1"); IRow Rowheader = Sheet1. CreateRow (0);//Line No. 0 Rowheader.createcell (0,celltype.string). Setcellvalue ("www.meteo-tech.com");//No. 0 column, SetPoint using (Stream stream =file.openwrite ("D:/1.xls")) { Hssfworkbook. Write (stream); }
Examples of how C # exports Excel files