This article describes how asp.net implements data to import from a DataTable into an Excel file and create a table. Share to everyone for your reference, specific as follows:
<summary>///Import data from DataTable into Excel file///</summary>///<param name= "dataTable" > Data source </param&
Gt <param name= "Absoluteexcelfilepath" >excel file absolute path </param>///<param name= "Tblcolname" > The corresponding column name </param> ColumnName <returns> operation in the TBL column name </param>///<param name= ">excel"///returns true successfully. Failure returns false</returns> public static bool Exportdatatoexcel (DataTable DataTable, String Absoluteexcelfilepath,
String[] Tblcolname, string[] ColumnName) {int k = 0;
if (dataTable = = null) return false;
OleDbConnection Conn = new OleDbConnection (); try {string strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Absoluteexcelfilepath + "; Mode=share Deny None; Extended Properties=excel 8.0;
Jet oledb:create System database=true ";
Conn = new OleDbConnection (strconn);
Conn.Open ();
OleDbCommand command = Conn.createcommand ();
String strSQL = ""; if (datatable.columns!= null) {//build table strSQL = ' CREATE tabLE "+ Datatable.tablename +" (";
for (int i = 0; i < columnname.length i++) {strSQL + = Columnname[i] + "TEXT,";
} strSQL = strsql.substring (0, strsql.length-1);
strSQL + = ")";
Command.commandtext + = strSQL; Command.
ExecuteNonQuery (); if (DataTable.Rows.Count > 0) {//Import data foreach (DataRow row in datatable.rows) {strSQL = "ins
ert into "+ Datatable.tablename +" (";
for (k = 0; k < tblcolname.length; k++) {strSQL + = Columnname[k] + ",";
} strSQL = strsql.substring (0, strsql.length-1);
strSQL + = ") VALUES (";
for (k = 0; k < tblcolname.length; k++) {strSQL = "' + row[tblcolname[k]] +" ', ";
} strSQL = strsql.substring (0, strsql.length-1);
strSQL + = ")";
Command.commandtext = strSQL; Command.
ExecuteNonQuery ();
catch (Exception ex) {conn.close ()); throw new Exception (ex.
message); Return false;
} conn.close ();
return true;
}
Invoke method:
DataSet ds = (DataSet) session["Listmobile"];//gets the value of the table to be exported if (DS). Tables[0]. Rows.Count <= 0) {page.registerstartupscript ("", "<mce:script type=" Text/javascript "><!--alert (' No content cannot be exported!
')//--></mce:script> ");
else {//excel page name string[] tablename = {"[" +datetime.now.tostring ("YYYYMMDDHHMMSS") + "]"}; String fileName = Tools.
Createid () + ". xls"; String filePath = Server.MapPath ("..
downloadfiles//"+ fileName); if (Tools.
Exportdatatoexcel (DS, FilePath, tablename) ==true) {response.clear ();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.appendheader ("Content-disposition", "attachment;filename=" + filename);
Response.ContentType = "application/vnd.ms-excel"; This.
EnableViewState = false;
Response.WriteFile (FilePath);
Response.Flush ();
if (System.IO.File.Exists (FilePath)) System.IO.File.Delete (FilePath); Response.Redirect (this.
Request.UrlReferrer.AbsoluteUri, True);
Response.End (); }
}
I hope this article will help you with ASP.net programming.