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.