Writing to Excel data using Npoi (ASP)

Source: Internet
Author: User

The first time to do this write data function, study the next Npoi class

Iworkbook WB =NewHssfworkbook (); //Create a tableIsheet sh = wb. Createsheet ("XXX"); //set the width of a cellSh. Setcolumnwidth (0, -* the); #regionMerge cellsIRow row0= Sh. CreateRow (0); Row0. Height= -* -; Icell icell1top0= Row0. Createcell (0); Icell1top0. CellStyle=Getcellstyle (WB, Stylexls Head); Icell1top0.        Setcellvalue (GetTitle ()); #endregion        #regionCreate a problem topicIRow Row1= Sh. CreateRow (1); //Row1. Height = *;Icell Icell=NULL; Icell Icell1top= Row1. Createcell (0); Icell1top. CellStyle=Getcellstyle (WB, Stylexls Head); Icell1top. Setcellvalue ("name/Questionnaire title"); stringSqlall ="SELECT * FROM dbo. Checkmgr where userid="+ Rqid +"ORDER BY t_sort ASC"; DataTable Dtall=SystemDAL.SQLHelper.GetTable (Sqlall); if(Dtall! =NULL&& DtAll.Rows.Count >0)        {             for(intj =0; J < DtAll.Rows.Count; J + +) {sh. Setcolumnwidth (J+1, +* the); //Merge Cells//table Header
//cellrangeaddress () The parameter order of the method is: Start line number, end line number, start column number, end column number. Sh. Addmergedregion (NewNpoi. Ss. Util.cellrangeaddress (0,0,0, DtAll.Rows.Count)); stringtitle = dtall.rows[j]["T_title"]. ToString (); Icell= Row1. Createcell (j +1); Icell. Setcellvalue (title); } } #endregion #regionCreate answers to the corresponding questionsDataTable Dtuser=GetUserInfo (); IRow ROW11=NULL; for(intA =0; A < DtUser.Rows.Count; a++)//Student{ROW11= Sh. CreateRow (A +2); stringuser = dtuser.rows[a]["Realname"]. ToString (); stringid = dtuser.rows[a]["ID"]. ToString (); for(intb =0; b < DtAll.Rows.Count; b++)//Topics { //the first column of each row//User nameHssfcell cell11 = Row11. Createcell (0) asHssfcell; Cell11. Setcellvalue (user); DataTable Dtinfo=getanwer (ID); if(Dtinfo! =NULL&& DtInfo.Rows.Count >0) { for(inti =0; i < DtInfo.Rows.Count; i++) { stringKey = dtinfo.rows[b]["Ckey"]. ToString (); stringValue = dtinfo.rows[b]["Cvalue"]. ToString (); stringtitle =getmgrtitle (key, value); Hssfcell Cell1= Row11. Createcell (b +1) asHssfcell; Cell1. Setcellvalue (title); } } } } #endregion #regionDownload ExcelRandom R=NewRandom (); string_filename =string. Format ("{0:yyyymmddhhmmssfff}-{1}", DateTime.Now, R.next (1,9999)) +". xls"; using(MemoryStream ms =NewMemoryStream ()) {wb. Write (MS); Response.Clear (); Response.ClearHeaders (); Response.Buffer=false; Response.ContentType="Application/octet-stream"; Response.appendheader ("content-disposition","attachement;filename="+Httputility.urlencode (_filename, System.Text.Encoding.ASCII)); Response.appendheader ("Content-length", Ms. Length.tostring ()); Response.BinaryWrite (Ms. GetBuffer ()); Response.Flush (); Response.End (); } #endregion


Effect:

Writing to Excel data using Npoi (ASP)

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.