需要您的专业知识 - 优化代码并加快excel的下载速度。 [英] Need your expertise - to optimise the code and speed the download of excel.

查看:61
本文介绍了需要您的专业知识 - 优化代码并加快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;
    }

推荐答案

,sheetName),
string .Format( @ 数据源= {0};提供者= Microsoft.ACE.OLEDB .12.0;扩展属性= Excel 12.0;,filePath)))
{
尝试
{
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 < /跨度>);

使用(OleDbConnection oledbConn = new OleDbConnection( string .Format( @ 数据源= {0};提供者= Microsoft.ACE .OLEDB.12.0;扩展属性= Excel 12.0;,filePath)))
{
使用(OleDbCommand oledbCmd = < span class =code-keyword> new
OleDbCommand())
{
System.Text.StringBuilder sbValues = new 系统.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);
尝试
{
valuesCount = 0 ;
for int idatacol = 0 ; idatacol < dataSet.Tables [ 0 ]。Columns.Count; idatacol ++)
{
valuesCount ++;

sbValues.Append( string .Format( ,'{0}',Convert.ToString(dataSet.Tables [ 0 ]。行[irow] [idatacol]) .Contains( ')?
Convert.ToString(dataSet.Tables [ 0 ]。行[irow] [idatacol])。替换( ' ''):
Convert.ToString( dataSet.Tables [ 0 ]。行[irow] [idatacol])));
}

如果(sbValues.Length > 0
sbValues.Remove( 0 1 < /跨度>);
}
catch (例外情况)
{
// Error_SendMail(ex);
continue ;
}

if (coloumsCount == valuesCount)
{
尝试
{
oledbCmd.CommandText = string .Format( 插入[{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 (例外情况)
{

if (oledbConn.State == System.Data.ConnectionState.Open)
oledbConn.Close();
// Error_SendMail(ex);
}
最后
{

}
}
}

if (oledbConn.State == System.Data.ConnectionState.Open)
oledbConn.Close();
}
}
返回 true ;
}
({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; }


在允许用户下载之前尝试压缩文件。使用c#代码将文件压缩为zip / 7z格式。



这样你可以优化下载速度。
Try to compress the file before allowing the user to download it. Use c# code to compress the file to zip/7z format.

This way you can optimise download speed.


这篇关于需要您的专业知识 - 优化代码并加快excel的下载速度。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆