Aspose. Cells export excel, aspose. cellsexcel

Source: Internet
Author: User

Aspose. Cells export excel, aspose. cellsexcel

Use Aspose. Cells to export excel

Notes

1. Processing of DataTable

2. encoding to facilitate download of Chinese name files

3. Do not forget Aspose. Cells. dll (you can search for it on the Internet)

Public static bool DataTableToExcel2 (DataTable datatable, string filepath, out string error) {error = ""; Aspose. cells. workbook wb = new Aspose. cells. workbook (); try {if (datatable = null) {error = "DataTableToExcel: datatable is empty"; return false;} // Add a style Aspose for the cell. cells. style style = wb. styles [wb. styles. add ()]; // set the Center style. horizontalAlignment = Aspose. cells. textAlignmentType. center; // set the background color st Yle. foregroundColor = System. drawing. color. fromArgb (153,204, 0); style. pattern = BackgroundType. solid; style. font. isBold = true; int rowIndex = 0; for (int I = 0; I <datatable. columns. count; I ++) {DataColumn col = datatable. columns [I]; string columnName = col. caption ?? Col. columnName; wb. worksheets [0]. cells [rowIndex, I]. putValue (columnName); wb. worksheets [0]. cells [rowIndex, I]. setStyle (style);} rowIndex ++; foreach (DataRow row in datatable. rows) {for (int I = 0; I <datatable. columns. count; I ++) {wb. worksheets [0]. cells [rowIndex, I]. putValue (row [I]. toString ();} rowIndex ++;} for (int k = 0; k <datatable. columns. count; k ++) {wb. worksheets [0]. autoFitColumn (k, 0,150);} 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 void btnExport_Click (object sender, EventArgs e) {// export int ClassID = 0; int. tryParse (hidClassID. value, out ClassID); string error = ""; string filepath = ""; BLL. TUser bll_TUser = new BLL. TUs Er (); BLL. TClass bll_Class = new BLL. TClass (); Model. TClass model = (new BLL. TClass ()). getModel (ClassID); // process DataTable dt able dt = bll_TUser.GetListByClass (ClassID); DataTable dtNew = new DataTable (); dtNew. columns. add ("name", typeof (string); dtNew. columns. add ("student ID", typeof (string); dtNew. columns. add ("gender", typeof (string); dtNew. columns. add ("phone", typeof (string); if (dt! = Null & dt. rows. count> 0) {DataRow drNew = dtNew. newRow (); foreach (DataRow dr in dt. rows) {// drNew = dtNew. newRow (); drNew ["name"] = dr ["UserName"]; drNew ["student ID"] = dr ["IDNO"]; drNew ["gender"] = dr ["Sex"]. toString () = "1 "? "Male": (dr ["Sex"]. ToString () = "2 "? "Female": ""); drNew ["Phone"] = dr ["Phone"]; dtNew. Rows. Add (drNew. ItemArray) ;}} (model! = Null) {filepath = "/UploadFiles/ExportClass/"; // + model. className + ". xlsx "; string filaname = model. className + ". xlsx "; string finalPath = MapPath ("~ "+ Filepath + filaname); // check whether an if (! Directory. Exists (MapPath ("~ /UploadFiles/ExportClass/") {Directory. CreateDirectory (MapPath ("~ /UploadFiles/ExportClass/");} if (DataTableToExcel2 (dtNew, finalPath, out error) {string SiteRoot =" http: // "+ Request. url. authority. toString () + filepath + Uri. escapeDataString (filaname); // It is encoded to facilitate the download of the Chinese name file // download the excel ClientScript. registerStartupScript (this. getType (), "", ", <script type = 'text/javascript '> window. open ('"+ SiteRoot +"'); </script> ");} else {ClientScript. registerStartupScript (thi S. GetType (), "", "<script type = 'text/javascript '> alert ('hprompt', '" + error + "! '); </Script> ") ;}} else {ClientScript. registerStartupScript (this. getType (), "", "<script type = 'text/javascript '> alert ('hprompt', 'class does not exist! '); </Script> ");}}

 

Related Article

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.