本文介绍了需要您的专业知识 - 优化代码并加快excel的下载速度。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 大家好, 我开发了一个报告工具,我将行插入到excel中(具有与macro enable excel相同的no。列)表格。 这段代码工作得非常好,并且在excel中下载得体。但我需要你的帮助来改善和优化下载速度。 感谢您的建议和时间。 我尝试过: 以下是代码行:App_code下的类。Hi All,I have developed a reporting tool, in which I am inserting the rows into the excel (having same no. columns as in macro enable excel sheet).This snippet of code is working perfectly fine and downloading in the excel in decent time. But I need your help to improve and optimize speed of download.Thanks for your advice and time.What I have tried:Here is the lines of code: Class under App_code.public static bool fillExcelFile(string filePath, DataSet dataSet, string sheetName, string tempSheetName) { DataTable oledbdatatbl = null; using (OleDbDataAdapter oledbadap = new OleDbDataAdapter(string.Format("SELECT * FROM [{0}$]", sheetName), string.Format(@"Data Source={0};Provider=Microsoft.ACE.OLEDB.12.0; Extended Properties=Excel 12.0;", filePath))) { try { oledbdatatbl = new System.Data.DataTable("ReportTable"); oledbadap.Fill(oledbdatatbl); } catch (Exception ex) { // Error_SendMail(ex); return false; } } System.Text.StringBuilder sbColumns = new System.Text.StringBuilder(); int coloumsCount = 0; for (int icol = 0; icol < (oledbdatatbl.Columns.Count); icol++) { coloumsCount++; sbColumns.Append(string.Format(",[{0}]", oledbdatatbl.Columns[icol].ColumnName)); } if (sbColumns.Length > 0) sbColumns.Remove(0, 1); using (OleDbConnection oledbConn = new OleDbConnection(string.Format(@"Data Source={0};Provider=Microsoft.ACE.OLEDB.12.0; Extended Properties=Excel 12.0;", filePath))) { using (OleDbCommand oledbCmd = new OleDbCommand()) { System.Text.StringBuilder sbValues = new System.Text.StringBuilder(); int valuesCount = 0; for (int irow = 0; irow < dataSet.Tables[0].Rows.Count; irow++) { if (sbValues.Length > 0) sbValues.Remove(0, sbValues.Length); try { valuesCount = 0; for (int idatacol = 0; idatacol < dataSet.Tables[0].Columns.Count; idatacol++) { valuesCount++; sbValues.Append(string.Format(",'{0}'", Convert.ToString(dataSet.Tables[0].Rows[irow][idatacol]).Contains("'") ? Convert.ToString(dataSet.Tables[0].Rows[irow][idatacol]).Replace("'", "''") : Convert.ToString(dataSet.Tables[0].Rows[irow][idatacol]))); } if (sbValues.Length > 0) sbValues.Remove(0, 1); } catch (Exception ex) { // Error_SendMail(ex); continue; } if (coloumsCount == valuesCount) { try { oledbCmd.CommandText = string.Format("insert into [{0}$]({1})values({2})", tempSheetName, sbColumns.ToString(), sbValues.ToString()); oledbCmd.CommandType = System.Data.CommandType.Text; // oledbCmd.CommandTimeout = 200; if (oledbConn.State == System.Data.ConnectionState.Closed) { oledbConn.Open(); oledbCmd.Connection = oledbConn; } oledbCmd.ExecuteNonQuery(); // oledbConn.Close(); } catch (Exception ex) { if (oledbConn.State == System.Data.ConnectionState.Open) oledbConn.Close(); // Error_SendMail(ex); } finally { } } } if (oledbConn.State == System.Data.ConnectionState.Open) oledbConn.Close(); } } return true; }推荐答案 这篇关于需要您的专业知识 - 优化代码并加快excel的下载速度。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-06 11:41