C # Npoi Export Excel

Source: Internet
Author: User
Tags rowcount

Need to introduce DLL files

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacecsr_web.common{ Public   classNpoiexport { Public StaticNpoi. HSSF. Usermodel.hssfworkbook Doexport (System.Data.DataTable DT,stringnotile) {            //Create a workbookNpoi. HSSF. Usermodel.hssfworkbook book =NewNpoi. HSSF.            Usermodel.hssfworkbook (); //Create a tableNpoi. Ss. Usermodel.isheet sheet =Book .            Createsheet (Notile); //Adaptive column Widths//sheet.            Autosizecolumn (1, true); //header row Merge cellSheet. Addmergedregion (NewNpoi. Ss. Util.cellrangeaddress (0,0,0Dt. columns.count-1)); Npoi. Ss. Usermodel.irow FirstRow= Sheet. CreateRow (0); Npoi. Ss. Usermodel.icell FirstCell= FirstRow. Createcell (0); //Table name StyleNpoi. Ss. Usermodel.icellstyle Styleheader =Book .            Createcellstyle (); Npoi. Ss. Usermodel.ifont Fontheader=Book .            CreateFont (); Styleheader.alignment=Npoi. Ss.            UserModel.HorizontalAlignment.Center; Styleheader.verticalalignment=Npoi. Ss.            UserModel.VerticalAlignment.Center; Fontheader.fontheightinpoints= -;            Styleheader.setfont (Fontheader); FirstCell. CellStyle=Styleheader; FirstCell.                        Setcellvalue (Notile); Try            {                //Column Name StyleNpoi. Ss. Usermodel.icellstyle Stylecolname =Book .                Createcellstyle (); Npoi. Ss. Usermodel.ifont Fontcolname=Book .                CreateFont (); Stylecolname.alignment=Npoi. Ss.                UserModel.HorizontalAlignment.Center; Stylecolname.verticalalignment=Npoi. Ss.                UserModel.VerticalAlignment.Center; Fontcolname.fontheightinpoints= -;                Stylecolname.setfont (Fontcolname); //style of data, font sizeNpoi. Ss. Usermodel.icellstyle Stylebody =Book .                Createcellstyle (); Npoi. Ss. Usermodel.ifont Fontbody=Book .                CreateFont (); Stylebody.alignment=Npoi. Ss.                UserModel.HorizontalAlignment.Center; Stylebody.verticalalignment=Npoi. Ss.                UserModel.VerticalAlignment.Center; Fontbody.fontheightinpoints= A;                               Stylebody.setfont (Fontbody); //Create specific cell data                intRowCount =dt.                Rows.Count; intColCount =dt.                Columns.count; Npoi. Ss. Usermodel.irow Colnamerow= Sheet. CreateRow (1);  for(intx =0; x < ColCount; X + +) {//Write column names to cellsNpoi. Ss. Usermodel.icell Colnamecell =Colnamerow.createcell (x); Colnamecell.setcellvalue (dt. COLUMNS[X].                    ColumnName); Colnamecell.cellstyle=Stylecolname; }                     for(inti =0; i < RowCount; i++) {Npoi. Ss. Usermodel.irow Row= Sheet. CreateRow (i +2);//data starts from third first row table name second column name                         for(intj =0; J < ColCount; J + +)                        {                            //Populating DataNpoi. Ss. Usermodel.icell cell =row.                            Createcell (j); if(dt. ROWS[I][J]! =NULL) {cell. Setcellvalue (dt. ROWS[I][J].                            ToString ()); }                            Else{cell. Setcellvalue (""); } cell. CellStyle=Stylebody; }                    }                //Adaptive column Widths                 for(intx =0; x < ColCount; X + +) {sheet. Autosizecolumn (x,true); }                //here the code is to send the XLS file to the page through the browser directly downloaded to the local can be put into the interface call place//System.IO.MemoryStream ms = new System.IO.MemoryStream (); //Book .                Write (MS); //Response.AddHeader ("Content-disposition", String. Format ("attachment;                Filename= performance statistics. xls ")); //Response.BinaryWrite (Ms.                ToArray ()); //Book = null; //Ms.                Close (); //Ms.  Dispose ();                 returnBook ; }            Catch {             Throw NewException (); }finally{ Book=NULL; }        }                               }}
  /// <summary>        ///Export/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        protected voidBtnexport_click (Objectsender, EventArgs e) {DataTable dt=Cmbll.getdt (); Npoi. HSSF. Usermodel.hssfworkbook Book= Npoiexport.doexport (DT,"XXX Reports"); //Write to client            Try{writeclient (book); }            Catch            {            }            finally{ Book=NULL; }                 }         Public voidWriteClient (Npoi. HSSF. Usermodel.hssfworkbook book) {System.IO.MemoryStream ms=NewSystem.IO.MemoryStream (); Book.            Write (MS); Response.AddHeader ("content-disposition",string. Format ("attachment; filename= Customer Information"+datetime.now.tostring ("YYYYMMDDHHMMSS")+". xls")); Response.BinaryWrite (Ms.            ToArray ()); Book=NULL; Ms.            Close (); Ms.        Dispose (); }

C # Npoi Export 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.