Original text: "Originality". NET read-write Excel Tool Spire.xls use (1) Introduction
In the. NET platform, manipulating Excel files is a very common requirement, and there are several ways to do this today:
How 1.Office COM components work: This is a very tiring way, Microsoft things are always so complex, it can be very inconvenient to use, need to install Excel, for the server, and sometimes need to configure IIS permissions. Toss people, see a lot of people in the group use this thing, there are all kinds of crazy mistakes;
2. Open source. NET component Npoi way : This way should be used by many people, this open source component compared to the first, although a little bit simpler (2003 support is better, the new version also supports xlsx), but it is very useful Recently, when browsing the open source website of Npoi, suddenly found that spire is actually npoi sponsor. Like a bar, help each other, serve the public, unlike domestic what hundred poison, penguins, fight all the way, mutual impeaches, but also to ban, really poor ah. Check out Npoi's CodePlex website:
Today's introduction to you. NET platform Operations Excel components Spire.xls, should be more than the above 2 methods to be better and more stable, more functions, first look at the basic introduction:
1.spire.office for. NET
Spire.office for. NET is a collection of software developed by E-iceblue that is an enterprise-level, editor of office, including Spire.doc,spire Xls,spire.pdf,spire.barcode, and so on. NET platform can be used by developers of Spire.office for. NET easily operates applications that contain document editing features, and can also be exported to Ms Word/excel/rtf/acces, PDF, XPS, HTML, XML, and other formats, using Spire.office, without the need to install Microsoft Office. Although Spire.office for. NET is a commercial product, but each of its basic functional components is provided with a free version . can go to the official website to see: http://www.e-iceblue.com/. Although there are some limitations, simple things can still be tried. The Spire.xls described in this article is just a component of the spire.office below that operates Excel.
2.spire.xls Features
Spire.xls for. NET supports all Excel format types (97-2003, $) files. With Spire.xls for. NET, developers can quickly and easily view, generate, read, write, and customize Excel files. Spire.xls for. NET has the following main functions:
1. Support the new Excel file, modify the editing, cell split Merge, property settings, lock encryption and so on;
2. Support Database export to Excel worksheet, or import data from Excel data into database;
3. Provide a powerful setting cell, formatting numbers, text indentation, positioning adjustment, cell filling and protection, etc.;
4. Support to the Excel file to write formulas, tables, pictures, different types of text, links, annotations and even VBA and other content;
5. Support to convert Excel files into PDF, HTML, CSV, Text, XML and image format.
There are many versions of the supported Excel files, see the following diagram to understand, the following is the enumeration type excelversion member list:
Spire.xls also offers a free version: http://www.e-iceblue.com/Introduce/ Free-xls-component.html, with a slight limitation, the main limitation is a single workbook up to 5 sheet, and each sheet supports up to 150 rows. Since I am using genuine, the following demo does not have this problem.
3. Write a Hello Excel
Because Microsoft Office is too large, I have been abandoned, these years have been using domestic WPS, the effect can also be, mainly small, only dozens of m, commonly used functions are not inferior with office. has been really convenient, but also free, the second is now the software are in contention for users, software is very big, occupy the memory is also increasing, in fact, the suffering is the user. Huge office is not a special requirement, try not to complete, suffer.
I am using the Spire.office 2.1 version, because the unit has a genuine, all do not use the free version, the system win7-32bit+wps the latest fresh version. Tools are VS2013, project type. NET 4.0 (of course the components themselves support a lot of environments, such as. NET 2.0,3.5,4.0,4.0 CLIENTPROFILE,SILVERLIGHT3/4/5,WPF, and so on).
1. Basic Preparation
The first step, the new project is not said, mainly refers to the corresponding version of the DLL file, as well as the reference to add a namespace:
2. Writing Basic code
Now that's popular with Hello World, before using this, simply try out a Hello Excel. The code is very simple and has comments.
1 usingSystem;2 usingSpire.xls;//This is one of the most important namespaces3 4 namespacexlstest5 {6 class Program7 {8 Static voidMain (string[] args)9 {Ten Helloexcel (); One console.readline (); A } - Static voidHelloexcel () - { the //Create 1 workbooks, equivalent to 1 Excel files
The document structure of Excel is Workbook->worksheet (1 book can contain multiple sheet) -Workbook Workbook =NewWorkbook (); - - //get the first sheet, take action, subscript is starting from 0 +Worksheet sheet = workbook. worksheets[0]; - //Of course you can add 1 named worksheet to the book yourself. +Workbook. Worksheets.add ("Test Sheet"); A at //write text to cell A1 -Sheet. range["A1"]. Text ="hello,world!"; - - //Save the Excel file to the specified file, and you can also specify the Excel version -Workbook. SaveToFile ("Sample.xls", excelversion.version97to2003); - } in } -}
3. Look at the effect
The code is simple and looks at the actual effect. 2 places, one is text, one is a new sheet, the default Excel file is 3 sheet.
4. Differences with Microsoft COM operations and Npoi
At present, the use of Microsoft COM operation and Npoi components is the most common, of course, many users have shared the use of commercial components of the article. The beginning of this article has been introduced, Microsoft COM operation, the most headache is to rely on office, the environment is very large, many people because this thing is abandoned, and the use of open-source or commercial, independent of the office environment, such as npoi,spire, such as the operating components. Since I have not used Microsoft Office COM to manipulate documents, I do not do much comparison here, focus on the comparison I used to use Npoi and now this spire.xls a little difference.
1.NPOI started supporting Office 2007 versions of Excel after the 2.0 release (which should have been released in 2014), many features were relatively weak, and support for Office 2003 was perfect, and I used to use only 2003 in my previous work;
2.spire.xls support for the Excel version is much better, as explained above, the support version of Office 2010/2013 is very perfect, this is the main reason to use it;
3. Before using Npoi 2.0 before the version, with Npoi made a very complex report, but there is a very deep feeling, Npoi namespace more, and more difficult to remember, such as HSSF,POIFS,SS and so do not understand what the meaning, not intuitive, so often can not find where, Especially in the late maintenance time, to add a bit of function;
4.NPOI in the Excel to generate the chart support is not enough, it seems that the version of 2.0 is not available at all, now the new version I have not tried, I do not know how the situation. One of the reasons I use Spire.xls is that the auto-generated report will generate the chart function, which Spire.xls support is very good;
5.NPOI in the functional grasp is very accurate, very core, in fact, as an open source component, has been very good. Some auxiliary operations with Excel files, such as file format conversion, data export import and so on, Spire.xls is a little better, of course, these through the npoi extension can also be done.
6. Other advantages and disadvantages and use of the problem, after the completion of this project, there is a more in-depth comparison, then share to everyone, but also welcome to add.
5. Resources
Npoi Open Source website: http://npoi.codeplex.com/
Spire.office Official free version: http://www.e-iceblue.com/Introduce/free-xls-component.html
Next post: "Original". NET read-write Excel tool Spire.xls using (2) Excel file control is coming soon, so stay tuned.
. NET read-write Excel Tool Spire.xls use (1) Introduction