Npoi action Excel 003: Write empty Excel

Source: Internet
Author: User

For NPOI operations Excel has already had a simple understanding (http://blog.csdn.net/yysyangyangyangshan/article/details/42614209). Continue to see how to save content to Excel.
Based on the previous experience Npoi operations Excel mainly several objects are:
Workbook,sheet and the row and cell within the sheet.
So saving to Excel also operates on these objects. Of course, we usually use Excel not only to save content in cells, but also to format cells and font size, such as formatting and style. These have a corresponding method in the Npoi component.
Here's a simple example: the DataTable content is saved to Excel, along with the table header, from the code to see how Excel is manipulated.
First of all, to prepare the work, you need to use the DLL, as in the previous article, as long as the reference to the project.
http://blog.csdn.net/yysyangyangyangshan/article/details/42614181
Then we look at how to use it.
Look at the code below,
     protected void Btn_writeexcel (object sender, EventArgs e) {//the content to be saved, which is generated in code here, and in practice can be read by the database,            or the contents of the page input datatable dt = new DataTable (); Dt.            Columns.Add ("serial number"); Dt.            Columns.Add ("name"); Dt.            Columns.Add ("Age"); Dt.            Columns.Add ("position"); for (int i = 0; i < 5; i++) {DataRow row = dt.                NewRow ();                row["serial number"] = i + 1;                row["name"] = "Test" +i;                Row["Age" = + I; row["position"] = i% 2 = = 0?                "Engineer": "Manager"; Dt.            Rows.Add (row);            }//In order to better see how to use Npoi, here are two lines of headings.            Show caption can see how to merge cell string maintitle = "Main title";            string secondtitle = "subtitle";            Save the Excel path, and the file name is generated with a GUID of string fileindex = HttpRuntime.AppDomainAppPath.ToString ();            String tempexcel = FileIndex + @ "\excelfile\{0}.xls"; Tempexcel = string. Format (Tempexcel, SysteM.guid.newguid ());                        int rowIndex = 0;            operates several main objects of Excel, declared here Hssfworkbook workbook = new Hssfworkbook (); Hssfsheet sheet = workbook.            Createsheet (); Row0 and Row1 are two lines of title Hssfrow row0 = Sheet.            CreateRow (RowIndex); Hssfcell cell0 = row0.            Createcell (0); Cell0.            Setcellvalue (Maintitle); Hssfcellstyle style = workbook.            Createcellstyle (); Style.            Alignment = Cellhorizontalalignment.center; Hssffont font = workbook.            CreateFont (); Font. Boldweight = short.            MaxValue; Style.            SetFont (font); Cell0.            CellStyle = style; Merge cell sheet here. Addmergedregion (New Npoi. HSSF.            Util.cellrangeaddress (rowindex,rowindex,0,5));            rowindex++; Hssfrow row1 = sheet.            CreateRow (RowIndex); Hssfcell cell1 = Row1.            Createcell (0); Cell1.            Setcellvalue (Secondtitle); Cell1.            CellStyle = style; Sheet. AddmergEdregion (New Npoi. HSSF.            Util.cellrangeaddress (RowIndex, RowIndex, 0, 5));            Because the column name has been specified, occupy a row rowindex++; This line shows the table header hssfrow Row2 = sheet.            CreateRow (RowIndex);            int row2cellindex = 0; foreach (DataColumn col in dt. Columns) {Hssfcell cell = row2.                Createcell (Row2cellindex); Cell. Setcellvalue (Col.                Columnname.tostring ());            row2cellindex++;            } rowindex++; The contents of the DataTable for (int i= 0;i< dt. rows.count;i++) {Hssfrow row = sheet.                CreateRow (RowIndex); foreach (DataColumn col in dt. Columns) {row. Createcell (Col. Ordinal). Setcellvalue (dt. Rows[i][col].                ToString ());            } rowindex++;            }//Use file stream to save MemoryStream ms = new MemoryStream (); Workbook.            Write (MS); Ms.            Flush (); Ms. Position = 0;            workbook = null;            sheet = null;            FileStream fs = new FileStream (tempexcel,filemode.create,fileaccess.readwrite); Ms.            WriteTo (FS); Fs.            Close (); Ms.        Close (); }

In order,
This is: Create an Excel file, use Workbook,sheet to find the current sheet page, then create rows, columns, and write the content on the specified column, and finally save it with a file stream.
It's easy to use, just remember a few key objects: Hssfworkbook,hssfsheet,hssfrow,hssfcell can read and write to Excel.
If you want to make Excel layout and format better, then use some such as hssfcellstyle,hssffont and so on to decorate.
The final content saved to Excel looks like this:

Code Download: http://download.csdn.net/detail/yysyangyangyangshan/8373785

Npoi action Excel 003: Write empty Excel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.