Datatable is converted to excel and exported to the server, which provides client download.

Source: Internet
Author: User

I haven't written any notes for a long time. It's not easy to write. Let's take a look!

This is a method for exporting an Excel file to a file specified by the server for the client to download. First, you must obtain the physical path of the storage server, that is, the absolute path. You can use

Httpcontext. Current. server. mappath ("."); can be obtained. The most convenient way is to directly convert a able into an Excel format. When a file is generated, a record is inserted into the database;

 

Code:

Public void datatabletransmissionexcel (bool withheaders, datatable ){

VaR strbuilder = Nwe system. Text. stringbuilder ();

If (DT! = NULL | DT. Rows. Count = 0 ){

If (withheaders) {// whether to display the column header name

For (INT I = 0; I <DT. Columns. Count; I ++ ){

Strbuilder. append (Dt. Columns [I]. columnname + "\ t"); // column: automatically jumps to the next cell.

}

Strbuilder. appendline (); // line feed

}

Foreach (datarow _ row in DT. Rows ){

For (INT I = 0; I <DT. Columns. Count; I ++ ){
Strbuilder. append (Dt. Columns [I]. columnname + "\ t"); // column: automatically jumps to the next cell.

 

}
Strbuilder. appendline (); // line feed

}

}

Return strbuilder. tostring ();

}

 

Public int rawexportdataaction (string orgid, string tablecodes, list <datatable> tables, Action E ){

Int saverows = 0;

String [] _ tablenames = NULL; // name of the exported database table

_ Tablenames = tablecodes. Split ('#');

Int insrows = 0; // The total number of inserted tables.

String _ excelfilename = string. Empty; // Add the Excel name

String _ temptablename = string. Empty; // The name of the table with data generated and exported.

Action <int32> callback = NULL;

Callback = (INT rowindex) => {

VaR DATA = datatabletransmissionexcel (true, tables [rowindex]);

String uri = httpcontext. Current. server. mappath (".");

Int Index = URI. indexof (@ "\ Services ");

Uri = URI. substring (0, index );

// Save the Excel table name format: Organization ID + Table name + year, month, day, hour, minute, and second

String savefilename = orgid + _ tablenames [rowindex] + "-" + datetime. Now. Year + datetime. Now. Month + datetime. Now. day +

Datetime. Now. Hour + datetime. Now. Minute + datetime. Now. Second + ". xls ";

// Save to the specified path address

String filename = string. Format (@ "{0} \ Excel \ {1}", Uri, savefilename );

// Create a file when the file does not exist

If (! System. Io. file. exists (filename )){

System. Io. filestream stream = system. Io. file. creat (filename );

Stream. Close ();

Stream. Dispose ();

}

Using (VAR writer = new system. Io. streamwriter (filename, true, system. Text. encoding. Unicode ))

{

Writer. Write (data); // read/write data

Writer. Close (); // close the stream

}

Insrows ++;

If (insrows <tables. Count ){

_ Temptablename + = _ tablenames [rowindex] + "#";

_ Excelfilename + = savefilename + "#";

}

If (insrows = tables. Count ){

_ Temptablename + = _ tablenames [rowindex];

_ Excelfilename + = savefilename;

// Insert a piece of downloaded data

Saverows = new xywdemoservice. Web. Ds. epmsgds. inserlotepmsg (orgid, _ temptablename, _ excelfilename );

}

If (rowindex> = tables. Count-1 ){

If (E! = NULL) {e ();}

} Else {callback (rowindex + 1 );}

};

Callback (0 );

Return saverows;

}

Datatable is converted to excel and exported to the server, which provides client download.

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.