Export an Excel file using aspx-convert

Source: Internet
Author: User

Private void datasettoexcelxls (Dataset DS)
{
Case Description # region case description
/**//*
[Input]
Sender:
E:
[Output]
None
[Process definition]
1. Import the generated dataset data to the specified execl file and display it.
[Optional process]

[Problem]

*/
# Endregion

Random RD = new random (Int. parse (datetime. Now. tostring ("mmddhhmmss ")));
String strfilename = datetime. now. tostring ("yyyymmdd") + datetime. now. hour + datetime. now. minute + datetime. now. second + txtbegintime. text. trim () + txtfinishtime. text. trim () + Rd. next (1, 999999 ). tostring () + ". xls ";

// Excel. Application Excel = new excel. applicationclass (); // excel. Application ();
Excel. Application Excel;
// Excel = new excel. Application ();
Excel = new excel. applicationclass ();
Excel. workbookclass owb;
Excel. worksheet osheet;
Owb = (Excel. workbookclass) (Excel. workbooks. Add (true ));
Osheet = (Excel. worksheet) owb. activesheet;

Osheet. cells [1, 1] = lblreporttime. Text; // merge items
Osheet. cells [2, 1] = lblreportcount. Text; // merge items
Osheet. cells [4, 1] = "phone number"; // merged item
Osheet. cells [4, 2] = "month"; // merge items
Osheet. cells [3, 3] = "Total number of calls (times )";
Osheet. cells [3, 7] = "Total call duration (minutes )";
Osheet. cells [3, 11] = "Total Call Amount (RMB )";
Osheet. cells [4, 3] = "international ";
Osheet. cells [4, 4] = "domestic ";
Osheet. cells [4, 5] = "Hong Kong, Macao and Taiwan ";
Osheet. cells [4, 6] = "Total ";
Osheet. cells [4, 7] = "international ";
Osheet. cells [4, 8] = "domestic ";
Osheet. cells [4, 9] = "Hong Kong, Macao and Taiwan ";
Osheet. cells [4, 10] = "Total ";
Osheet. cells [4, 11] = "international ";
Osheet. cells [4, 12] = "domestic ";
Osheet. cells [4, 13] = "Hong Kong, Macao and Taiwan ";
Osheet. cells [4, 14] = "Total ";

Osheet. get_range ("A1", "N4"). Font. size = 9;
Osheet. get_range ("A1", "N4"). Font. Bold = true;
Osheet. get_range ("A1", "N4"). verticalalignment = excel. xlvalign. xlvaligncenter;
Osheet. get_range ("A1", "N4"). horizontalalignment = excel. xlhalign. xlhaligncenter;

// Set an outer border in this range
Osheet. get_range ("A1", "N4 "). borderaround (Excel. xllinestyle. xlcontinuous, Excel. xlborderweight. xlthin, Excel. xlcolorindex. xlcolorindexautomatic, color. black. toargb ());

// Set the borders of all cells in the range
Osheet. get_range ("A1", "N4"). Borders. Weight = excel. xlborderweight. xlthin;
Osheet. get_range ("A1", "N4"). Borders. Color = color. Black. toargb ();
// Add background color to cells in the specified range
Osheet. get_range ("A1", "N2"). cells. Interior. Color = color. fromargb (153,255,204). toargb ();
Osheet. get_range ("A1", "N2"). cells. Interior. pattern = excel. xlbackground. xlbackgroundautomatic;

Osheet. get_range ("A3", "N4"). cells. Interior. Color = color. lemonchiffon. toargb ();
Osheet. get_range ("A3", "N4"). cells. Interior. pattern = excel. xlbackground. xlbackgroundautomatic;

Datatable dt = Ds. Tables [0];
Datarow Dr;
Int COUNT = DT. Rows. Count + 5;
String strexcellength = "N" + count. tostring (). Trim ();

Int I, J;
For (I = 5; I <count; I ++)
{
Dr = DT. Rows [I-5];

For (j = 1; j <(Dt. Columns. Count + 1); j ++)
{
Osheet. cells [I, j] = Dr [J-1]. tostring (). Trim ();

}

}

Osheet. get_range ("A5", strexcellength). Font. size = 9;
Osheet. get_range ("A5", strexcellength). Font. Bold = false;
Osheet. get_range ("A5", strexcellength). verticalalignment = excel. xlvalign. xlvaligncenter;
Osheet. get_range ("A5", strexcellength). horizontalalignment = excel. xlhalign. xlhaligncenter;
// Set an outer border in this range
Osheet. get_range ("A5", strexcellength). borderaround (Excel. xllinestyle. xlcontinuous, Excel. xlborderweight. xlthin, Excel. xlcolorindex. xlcolorindexautomatic, color. Black. toargb ());

// Set the borders of all cells in the range
Osheet. get_range ("A5", strexcellength). Borders. Weight = excel. xlborderweight. xlthin;
Osheet. get_range ("A5", strexcellength). Borders. Color = color. Black. toargb ();
// Add background color to cells in the specified range
Osheet. get_range ("A5", strexcellength). cells. Interior. Color = color. paleturquoise. toargb ();
Osheet. get_range ("A5", strexcellength). cells. Interior. pattern = excel. xlbackground. xlbackgroundautomatic;


Osheet. get_range ("A1", "N1"). Merge (true );
Osheet. get_range ("A2", "N2"). Merge (true );

Osheet. get_range ("A3", "A4"). Merge (true );
Osheet. get_range ("B3", "B4"). Merge (true );
Osheet. get_range ("C3", "F3"). Merge (true );
Osheet. get_range ("G3", "J3"). Merge (true );
Osheet. get_range ("K3", "N3"). Merge (true );

String Path = server. mappath (".") + "//... // .. // Excel-file //" + strfilename;

Owb. saveas (path, missing. value, "", "", true, false, Excel. xlsaveasaccessmode. xlnochange, 1, false, missing. Value, missing. value );

// Owb. savecopyas (PATH );

Owb. Close (false, null, null );
Excel. workbooks. Close ();
Excel. Quit ();

System. runtime. interopservices. Marshal. releasecomobject (Excel );
System. runtime. interopservices. Marshal. releasecomobject (owb );
System. runtime. interopservices. Marshal. releasecomobject (osheet );

Osheet = NULL;
Owb = NULL;
Excel = NULL;

GC. Collect ();

String test = ".../../Excel-file/" + strfilename;
Response. Redirect (test );
}

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.