1

I m developing a web project in asp.net 3.5

I want to export datatable to Excel. But there are 20.000 rows in datatable. Sometimes timeout problem happens..

 protected string Worksheet97_Header()
        {
            string s = "<tr>";
            foreach (ExcelColumn col in Columns)
            {
                s += "<th>" + col.Header_Text + "</th>";
            }
            s+="</tr>";
            return s;
        }
        protected string Worksheet97_Data()
        {
            string s = "";
            try
            {
                for (int i = 0; i < data.Rows.Count; i++)
                {
                    s += "<tr>";
                    foreach (ExcelColumn col in Columns)
                    {
                        if (col.Column_Type == "System.String")
                            s += "<td>" + data.Rows[i][col.Field_Name].ToString() + "</td>";
                        if (col.Column_Type == "System.DateTime")
                            s += "<td>" + Convert.ToDateTime(data.Rows[i][col.Field_Name]).ToString("dd.MM.yyyy HH:mm:ss") + "</td>";
                        if (col.Column_Type == "System.Int32")
                            s += "<td>" + data.Rows[i][col.Field_Name].ToString() + "</td>";
                        if ((col.Column_Type == "System.Double") |
                            (col.Column_Type == "System.Decimal") |
                            (col.Column_Type == "System.Int16") |
                            (col.Column_Type == "System.Int32") |
                            (col.Column_Type == "System.Int64"))

                            s += "<td>" + Convert.ToDouble(data.Rows[i][col.Field_Name]).ToString("0.00") + "</td>";
                    }
                }
            }
            catch (Exception ex)
            {
                string a = ex.ToString();            

            }

            return s;
        }

        public string Export_Excel97()
        {
            string s = "";
            s = "<table border=\"1\">";
            s += Worksheet97_Header();
            s += Worksheet97_Data();
            s += "</table>";
            return s;
        }

Thanks.

1
  • Please post more information (preferably the code that is timing-out) and we may be able to help you.
    – Codesleuth
    Nov 5, 2010 at 9:22

2 Answers 2

1

IMHO, i think you should page the query so that you don't load everything into memory.

To write the excel file you may want to try this solution too and compare results of performance: http://msmvps.com/blogs/deborahk/archive/2009/07/23/writing-data-from-a-datatable-to-excel.aspx which will use Microsoft Excel Object Library, so you'll need to have Excel installed in the machine where you're running your code.

HTH somehow.

Regards!

1

This might help... http://www.dotnetjohn.com/PrintFriend.aspx?articleid=36

It is in VB.NET but you should be able to convert it anyway ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.