您好,登錄后才能下訂單哦!
這篇文章主要介紹asp.net如何實(shí)現(xiàn)數(shù)據(jù)從DataTable導(dǎo)入到Excel文件并創(chuàng)建表,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
具體如下:
/// <summary> /// 把數(shù)據(jù)從DataTable導(dǎo)入到Excel文件里 /// </summary> /// <param name="dataTable">數(shù)據(jù)源</param> /// <param name="AbsoluteExcelFilePath">Excel文件的絕對(duì)路徑</param> /// <param name="TblColName">TBL里對(duì)應(yīng)的列名</param> /// <param name="ColumnName">Excel中對(duì)應(yīng)的列名</param> /// <returns>操作成功返回True,失敗返回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) { //建表 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) { //導(dǎo)入數(shù)據(jù) foreach (DataRow row in dataTable.Rows) { strSQL = "insert 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; }
調(diào)用方法:
DataSet ds = (DataSet)Session["listMobile"];//獲得要導(dǎo)出的表格的值 if (ds.Tables[0].Rows.Count <= 0) { Page.RegisterStartupScript("", "<mce:script type="text/javascript"><!-- alert('沒(méi)有內(nèi)容不能導(dǎo)出!') // --></mce:script>"); } else { //EXCEL頁(yè)面的名稱 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(); } }
以上是“asp.net如何實(shí)現(xiàn)數(shù)據(jù)從DataTable導(dǎo)入到Excel文件并創(chuàng)建表”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。