Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.67/5 (2 votes)
See more:
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.

C#
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;
    }
Posted
Updated 12-Sep-16 8:08am
v2
Comments
Richard MacCutchan 12-Sep-16 5:53am    
Where is the download taking place, and what is the problem?
ajitsit07 12-Sep-16 6:16am    
There is no problem in the code, i have to improve the speed. so that the excel will get downloaded in less time. Ex. now it is take 40secs and i want to reduce its time to 20 secs.

This excel download in under C#. ASP.net website under a menu.

public static bool fillExcelFile(string filePath, DataSet dataSet, string sheetName, string tempSheetName)

Here: Filepath - is the server mapped path of file (macro enable excel file) available on the server.
Dataset: Contain the data need to insert into the temp sheet of excel.



Do you need any other info. too.
Richard MacCutchan 12-Sep-16 6:45am    
Download speed is dependent on the network connections between the server and the client.
ajitsit07 12-Sep-16 8:16am    
Thanks Richard.

Yes, I do agree with you. But optimized code makes difference.
Is there any scope to optimize the above code?
Dave Kreskowiak 12-Sep-16 8:28am    
You'r combining to distinct operations in your question. You either need help with building the Excel sheet and speeding that up or you need help with the download operation.

So which is it? One has nothing to do with the other.

1 solution

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.
 
Share this answer
 
Comments
ajitsit07 13-Sep-16 8:56am    
Can't zip the file, because here are inserting the rows in the temp sheet of excel and further macro will run to use these dumped data and generate the report.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900