/// <summary> ///Export/// </summary> /// <param name= "table" >Data Sheet</param> /// <param name= "SheetName" >Workbook Name</param> /// <param name= "ReportName" >Report Name</param> Public Static voidOutput (DataTable table,stringSheetName,stringReportName) { stringresult =string. Empty; Try{Hssfworkbook WorkBook=NewHssfworkbook (); Sheet Sheet= Workbook.createsheet (SheetName);//Sheet page nameNpoi. Ss. Usermodel.font Font =Workbook.createfont (); Font. FontName="Microsoft Ya-Black"; Font. Fontheight=175; CellStyle style=Workbook.createcellstyle (); Style. Alignment=HorizontalAlignment.Center; Style. VerticalAlignment=Verticalalignment.center; Style. SetFont (font); Style. WrapText=true; Row rows= Sheet. CreateRow (0); Rows. Height= -; Rows. Createcell (0). Setcellvalue (ReportName);//Report NameRows. Getcell (0). CellStyle =style; Sheet. Addmergedregion (NewCellrangeaddress (0,0,0, table. Columns.count-1)); Rows= Sheet. CreateRow (1); for(inti =0; I < table. Columns.count; i++) {rows. Createcell (i). Setcellvalue (table. Columns[i]. Columnname.tostring ()); Rows. Sheet.setcolumnwidth (i,4000); Rows. Getcell (i). CellStyle=style; } CellStyle style2=Workbook.createcellstyle (); Style2. Alignment=HorizontalAlignment.Left; Style2. VerticalAlignment=Verticalalignment.center; Style2. SetFont (font); Style2. DataFormat= Hssfdataformat.getbuiltinformat ("@"); //Style2. WrapText = true; intTEM =0; intSheetcount =1; for(intj =1; J <= table. Rows.Count; J + +) {tem++; if(TEM = =60000)//export up to 60000 per page{tem=1; Sheetcount++; Sheet= Workbook.createsheet (SheetName +sheetcount); Rows= Sheet. CreateRow (0); for(inti =0; I < table. Columns.count; i++) {rows. Createcell (i). Setcellvalue (table. Columns[i]. Columnname.tostring ()); Rows. Sheet.setcolumnwidth (i,4000); Rows. Getcell (i). CellStyle=style; }} Row Row= Sheet. CreateRow (tem +1); for(intK =0; K < table. Columns.count; k++) {row. Createcell (k). Setcellvalue (table. Rows[j-1][k]. ToString ()); Row. Getcell (k). CellStyle=Style2; } #region //Row row = sheet. CreateRow (j + 1); //for (int k = 0; k < table. Columns.count; k++)//{ //row. Createcell (k). Setcellvalue (table. ROWS[J-1][K]. ToString ()); //row. Getcell (k). CellStyle = Style2; //} #endregion } intMaxcolumn =table. Columns.count; //column width Adaptive, only valid for English and numerals for(inti =0; I <= maxcolumn; i++) {sheet. Autosizecolumn (i); } using(MemoryStream ms =NewMemoryStream ()) {Workbook.write (MS); Ms. Flush (); Ms. Position=0; WorkBook=NULL; HttpContext.Current.Response.ContentType="Application/vnd.ms-excel";//HttpContext.Current.Response stringBrowser =HttpContext.Current.Request.Browser.Browser.ToString (); stringHeader =string. Empty; if(Browser = ="Firefox") {Header=string. Format ("attachment; filename={0}",string. Format ("{0}-{1}.xls", ReportName, DateTime.Now.ToString ("YYYY-MM-DD") ), Encoding.UTF8). ToString (); } Else{Header=string. Format ("attachment; filename={0}", Httputility.urlencode (string. Format ("{0}-{1}.xls", ReportName, DateTime.Now.ToString ("YYYY-MM-DD")) ( Encoding.UTF8)). ToString (); } HttpContext.Current.Response.AddHeader ("content-disposition", header); HttpContext.Current.Response.Clear (); HttpContext.Current.Response.BinaryWrite (Ms. GetBuffer ()); HttpContext.Current.Response.End (); } } Catch { Throw NewException ("Export Exception"); } }
Export to Excel