Aspose.cells exporting Excel

Source: Internet
Author: User

Export Excel using Aspose.cells

The problem of attention

1, the processing of the DataTable

2, encoding, easy to download Chinese name file

3, do not forget the Aspose.Cells.dll (you can search on the Internet)

 Public Static BOOLDataTableToExcel2 (DataTable DataTable,stringFilePath out stringerror) {Error=""; Aspose.Cells.Workbook WB=NewAspose.Cells.Workbook (); Try    {        if(DataTable = =NULL) {Error="datatabletoexcel:datatable is empty"; return false; }        //add a style to a cellAspose.Cells.Style Style =WB. STYLES[WB.        Styles.add ()]; //Set CenterStyle. HorizontalAlignment =Aspose.Cells.TextAlignmentType.Center; //Set Background colorStyle. Foregroundcolor = System.Drawing.Color.FromArgb (153,204,0); Style. Pattern=Backgroundtype.solid; Style. Font.isbold=true; intRowIndex =0;  for(inti =0; I < DataTable. Columns.count; i++) {DataColumn col=DataTable.            Columns[i]; stringColumnName = Col. Caption??Col.            ColumnName; Wb. worksheets[0]. Cells[rowindex, I].            Putvalue (ColumnName); Wb. worksheets[0]. Cells[rowindex, I].        SetStyle (style); } RowIndex++; foreach(DataRow rowinchDataTable. Rows) { for(inti =0; I < DataTable. Columns.count; i++) {wb. worksheets[0]. Cells[rowindex, I]. Putvalue (Row[i].            ToString ()); } RowIndex++; }         for(intK =0; K < DataTable. Columns.count; k++) {wb. worksheets[0]. Autofitcolumn (k,0, Max); } WB. worksheets[0]. FreezePanes (1,0,1, DataTable.        Columns.count); Wb.        Save (filepath); return true; }    Catch(Exception e) {error= Error +"Datatabletoexcel:"+E.message; return false; }}protected voidBtnexport_click (Objectsender, EventArgs e) {//Export    intClassID =0; int. TryParse (Hidclassid.value, outClassID); stringError =""; stringfilepath =""; Bll. TUser Bll_tuser=NewBLL.    TUser (); Bll. Tclass Bll_class=NewBLL.    Tclass (); Model.tclass Model= (NewBLL. Tclass ()).    Getmodel (ClassID); //Working with DataTableDataTable dt =Bll_tuser.getlistbyclass (ClassID); DataTable dtnew=NewDataTable (); DTNEW.COLUMNS.ADD ("name",typeof(string)); DTNEW.COLUMNS.ADD ("School Number",typeof(string)); DTNEW.COLUMNS.ADD ("Sex",typeof(string)); DTNEW.COLUMNS.ADD ("Telephone",typeof(string)); if(dt! =NULL&& dt. Rows.Count >0) {DataRow drnew=Dtnew.newrow (); foreach(DataRow Drinchdt. Rows) {//drnew = Dtnew.newrow ();drnew["name"] = dr["UserName"]; drnew["School Number"] = dr["Idno"]; drnew["Sex"] = dr["Sex"]. ToString () = ="1"?"male": (dr["Sex"]. ToString () = ="2"?"female":""); drnew["Telephone"] = dr["Phone"];        DTNEW.ROWS.ADD (Drnew.itemarray); }    }    if(Model! =NULL) {filepath="/uploadfiles/exportclass/";//+ model. ClassName + ". xlsx";        stringFilaname = model. ClassName +". xlsx"; stringFinalpath = MapPath ("~"+ filepath +filaname); //Check to see if the path is created        if(! Directory.Exists (MapPath ("~/uploadfiles/exportclass/")) {directory.createdirectory (MapPath ("~/uploadfiles/exportclass/")); }        if(DataTableToExcel2 (Dtnew, Finalpath, outerror)) {            stringSiteroot ="/ http"+ Request.Url.Authority.ToString () + filepath + uri.escapedatastring (filaname);//code to facilitate the download of Chinese name files//download ExcelClientscript.registerstartupscript ( This. GetType (),"",", <script type= ' text/javascript ' >window.open ('"+ Siteroot +"');</script>"); }        Else{clientscript.registerstartupscript ( This. GetType (),"","<script type= ' text/javascript ' >alert (' hint ', '"+ Error +"! ');</script>"); }    }    Else{clientscript.registerstartupscript ( This. GetType (),"","<script type= ' text/javascript ' >alert (' Hint ', ' class does not exist! ');</script>"); }}

Aspose.cells exporting 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.