Aspose. Cells export Excel (2), aspose. cellsexcel

Source: Internet
Author: User

Aspose. Cells export Excel (2), aspose. cellsexcel

DataTable dtTitle = ds. tables [0]; DataTable dtDetail = ds. tables [1]; int columns = dtTitle. columns. count; string error = ""; // cause of export error Aspose. cells. workbook wb = new Aspose. cells. workbook (); // Workbook Worksheet sheet = wb. worksheets [0]; // The first worksheet, Cells = sheet. cells; // cell cells. setColumnWidth (0, 15f); // The width of cells in the first column. setColumnWidth (1, 10f); cells. setColumnWidth (2, 10f); cells. setColumnWidth (3, 10f); DataRow dr = Null; string p = ""; if (dtTitle! = Null & dtTitle. rows. count> 0) {try {# region // Add the style Aspose to the header cell of the first row. cells. style styleTitle = wb. styles [wb. styles. add ()]; // Add a style // set the center styleTitle. horizontalAlignment = TextAlignmentType. center; // set the background color styleTitle. foregroundColor = System. drawing. color. fromArgb (153,204, 0); styleTitle. pattern = BackgroundType. solid; // bold styleTitle. font. isBold = true; // set the border // styleTitle. borders [BorderType. left Border]. lineStyle = CellBorderType. thin; // styleTitle. borders [BorderType. leftBorder]. color = System. drawing. color. black; // styleTitle. borders [BorderType. rightBorder]. lineStyle = CellBorderType. thin; // styleTitle. borders [BorderType. rightBorder]. color = System. drawing. color. black; // styleTitle. borders [BorderType. topBorder]. lineStyle = CellBorderType. thin; // styleTitle. borders [BorderType. topBorder]. C Olor = System. drawing. color. black; // styleTitle. borders [BorderType. bottomBorder]. lineStyle = CellBorderType. thin; // styleTitle. borders [BorderType. bottomBorder]. color = System. drawing. color. black; # endregion # region splicing header for (int I = 0; I <dtTitle. rows. count; I ++) {dr = dtTitle. rows [I]; # region Student ID name average total score cells. merge (0, 0, 2, 1); // Merge cells [0, 0]. putValue (dr ["F4"]); // enter the content of cells [0, 0]. setStyle (StyleTitle); // Header style cells. merge (0, 1, 2, 1); // Merge cells [0, 1]. putValue (dr ["F5"]); // enter the content of cells [0, 1]. setStyle (styleTitle); // Header style cells. merge (0, 2, 2, 1); // Merge cells [0, 2]. putValue (dr ["F9"]); // enter the content of cells [0, 2]. setStyle (styleTitle); // Header style cells. merge (0, 3, 2, 1); // Merge cells [0, 3]. putValue (dr ["F8"]); // enter the content of cells [0, 3]. setStyle (styleTitle); // Header style cells. merge (0, 4, 1, columns-9); // Merge cells [0, 4]. putValue ("job score record"); // enter the content of cells [0, 4]. setStyle (styleTitle); // Header style # endregion for (int n = 9; n <columns; n ++) {p = "F" + (n + 1 ); cells [1, n-5]. putValue (dr [p]); // enter the content of cells [1, n-5]. setStyle (styleTitle); // Header style cells. setColumnWidth (n-5, 15f) ;}// make the columns adaptive width // sheet. autoFitColumns (); # endregion} catch (Exception e) {error + = "DataTableToExcel-Error:" + e. message ;}} if (dtDetail! = Null & dtDetail. rows. count> 0) {Aspose. cells. style styleCell = wb. styles [wb. styles. add ()]; // Add a style // set to center // styleCell. horizontalAlignment = TextAlignmentType. center; float result = 0.00f; for (int I = 0; I <dtDetail. rows. count; I ++) {dr = dtDetail. rows [I]; # The total score of region Student ID name sharing is styleCell. horizontalAlignment = TextAlignmentType. left; cells [I + 2, 0]. putValue (dr ["F4"]); // enter the content of cells [I + 2, 0]. setStyle (StyleCell); // The Header style styleCell. horizontalAlignment = TextAlignmentType. center; cells [I + 2, 1]. putValue (dr ["F5"]); // enter the content of cells [I + 2, 1]. setStyle (styleCell); // Header style styleCell. horizontalAlignment = TextAlignmentType. right; if (float. tryParse (dr ["F9"]. toString (), out result) {cells [I + 2, 2]. putValue (result. toString ("0.00"); // enter the content} else {cells [I + 2, 2]. putValue (dr ["F9"]); // enter the content} cells [I + 2, 2]. SetStyle (styleCell); // Header style if (float. tryParse (dr ["F8"]. toString (), out result) {cells [I + 2, 3]. putValue (result. toString ("0.00"); // enter the content} else {cells [I + 2, 3]. putValue (dr ["F8"]); // enter the content} cells [I + 2, 3]. setStyle (styleCell); // Header style # endregion for (int n = 9; n <columns; n ++) {p = "F" + (n + 1 ); if (float. tryParse (dr [p]. toString (), out result) {cells [I + 2, n-5]. putValue (result. toString ("0. 00 "); // enter the content} else {cells [I + 2, n-5]. putValue (dr [p]); // enter the content} cells [I + 2, n-5]. setStyle (styleCell); // style }}string finalPath = MapPath ("~ "+"/UploadFiles/ExportClass/"+ filename +". xls "); wb. Save (finalPath );
// Encode the file to facilitate download of the Chinese name
String SiteRoot = "http: //" + Request. url. authority. toString () + "/UploadFiles/ExportClass/" + Uri. escapeDataString (filename + ". xls "); // download excel
ClientScript. registerStartupScript (this. getType (), "", ", <script type = 'text/javascript '> window. open ('"+ SiteRoot +"'); </script> ");

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.