Use the Npoi control to export Excel files and Word files

Source: Internet
Author: User

1  PublicHttpresponsemessage getreportrateoutput (DateTime? begin_time =NULLDatetime? End_time =NULL,stringType ="Large Buoy")2         {3             varDataList =_adapter. Datareportrate (Type, Begin_time, End_time). ToList ();4 5Npoi. HSSF. Usermodel.hssfworkbook book =NewNpoi. HSSF. Usermodel.hssfworkbook ();6Npoi. Ss. Usermodel.isheet sheet = Book. Createsheet ("""+ Type +""to the reporting rate statistics");7 8             //first column9Npoi. Ss. Usermodel.irow row = sheet. CreateRow (0);TenRow. Createcell (0). Setcellvalue ("Station name"); OneRow. Createcell (1). Setcellvalue ("should go to count off"); ARow. Createcell (2). Setcellvalue ("chlorophyll to report rate"); -Row. Createcell (3). Setcellvalue ("air pressure to reporting rate"); -Row. Createcell (4). Setcellvalue ("wind speed to reporting rate"); theRow. Createcell (5). Setcellvalue ("temperature to report rate"); -Row. Createcell (6). Setcellvalue ("water temperature to reporting rate"); -Row. Createcell (7). Setcellvalue ("wave height to reporting rate"); -Row. Createcell (8). Setcellvalue ("salinity to rate of report"); +  -Npoi. Ss. Usermodel.icellstyle style =Book . Createcellstyle (); +Style. Alignment =HorizontalAlignment.Center; AStyle. Fillbackgroundcolor =Npoi. HSSF. Util.HSSFColor.DarkBlue.Index; atStyle. Fillforegroundcolor =Npoi. HSSF. Util.HSSFColor.DarkBlue.Index; -Row. Getcell (0). CellStyle =style; -Row. Getcell (1). CellStyle =style; -Row. Getcell (2). CellStyle =style; -Row. Getcell (3). CellStyle =style; -Row. Getcell (4). CellStyle =style; inRow. Getcell (5). CellStyle =style; -Row. Getcell (6). CellStyle =style; toRow. Getcell (7). CellStyle =style; +Row. Getcell (8). CellStyle =style; -Sheet. Setcolumnwidth (0, the* the); theSheet. Setcolumnwidth (1, the* the); *Sheet. Setcolumnwidth (2, the* the); $Sheet. Setcolumnwidth (3, the* the);Panax NotoginsengSheet. Setcolumnwidth (4, the* the); -Sheet. Setcolumnwidth (5, the* the); theSheet. Setcolumnwidth (6, the* the); +Sheet. Setcolumnwidth (7, the* the); ASheet. Setcolumnwidth (8, the* the); the  +  -             intindex =1; $             foreach(Datatransferstatus DatainfoinchdataList) $             { -                 //second column -Npoi. Ss. Usermodel.irow Row2 =sheet. CreateRow (index); the  -Row2. Createcell (0). Setcellvalue (datainfo.name);WuyiRow2. Createcell (1). Setcellvalue (datainfo.report_number); theRow2. Createcell (2). Setcellvalue (Datainfo.phyll_report_rate +"%"); -Row2. Createcell (3). Setcellvalue (Datainfo.pressure_report_rate +"%"); WuRow2. Createcell (4). Setcellvalue (Datainfo.speed_report_rate +"%"); -Row2. Createcell (5). Setcellvalue (Datainfo.temp_report_rate +"%"); AboutRow2. Createcell (6). Setcellvalue (Datainfo.water_report_rate +"%"); $Row2. Createcell (7). Setcellvalue (Datainfo.wave_report_rate +"%"); -Row2. Createcell (8). Setcellvalue (Datainfo.salt_report_rate +"%"); -  -index++; A             } +System.IO.MemoryStream stream =NewSystem.IO.MemoryStream (); the Book . Write (stream); -Stream. Seek (0, seekorigin.begin); $Book =NULL; theHttpresponsemessage Mresult =Newhttpresponsemessage (System.Net.HttpStatusCode.OK); theMresult.content =NewStreamcontent (stream); theMResult.Content.Headers.ContentDisposition =NewContentdispositionheadervalue ("Attachment"); theMResult.Content.Headers.ContentDisposition.FileName = Type +"_ to report rate statistics"+ DateTime.Now.ToString ("YYYY-MM-DD") +". xls"; -MResult.Content.Headers.ContentType =NewMediatypeheadervalue ("Application/ms-excel"); in  the             returnMresult; the}

1. First import npoi DLL, this online has a lot of self-download. Go to the official website: http://npoi.codeplex.com/The download requires the introduction of a DLL (you can select the. net2.0 or. net4.0 dll), and then add references to the Web site.

2. Reference the namespace using Npoi. Ss. Usermodel

3. (For example, export Excel here) We need to understand what part of a complete Excel file is made up of!

(1) A workbook book, a worksheet sheet, and then include row rows, column columns, cell cells

4.

Create each part separately
Npoi. HSSF. Usermodel.hssfworkbook book = new Npoi. HSSF. Usermodel.hssfworkbook ();//npoi. Ss. Usermodel.isheet sheet = Book. Createsheet ("" "+ Type +" "to report rate statistics"); Npoi. Ss. Usermodel.irow row = sheet. CreateRow (0);

Set the style of an Excel file
Npoi. Ss. Usermodel.icellstyle style = Book . Createcellstyle (); style. Alignment = Horizontalalignment.center;style. Fillbackgroundcolor = Npoi. HSSF. Util.HSSFColor.DarkBlue.Index;
Style. Fillforegroundcolor = Npoi. HSSF. Util.HSSFColor.DarkBlue.Index;

Use the Npoi control to export Excel files and Word files

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.