Export data to excel in windows Forms Application

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • suryam
    New Member
    • Mar 2010
    • 1

    Export data to excel in windows Forms Application

    hi,

    i am using windowsforms application and i have one button for import datagridview to ms excel format... its working but when i am adding large amount of data it shows some error... the error is

    COMException: Exception from HRESULT: 0x800A03EC.

    How shall i solve this error? How shall i get solution? Tell me the solution of this problem...

    Thanks in advance...
    Code:
        Excel.Application oxl;
        Excel.Workbook wbook;
        Excel.Worksheet wsheet;
        Excel.Range range;
    
        private void button1_Click(object sender, EventArgs e)
        {
            oxl = new Excel.Application();
            oxl.Visible = true;
            oxl.DisplayAlerts = false;
    
    
            wbook = oxl.Workbooks.Add(Missing.Value);
    
            wsheet = (Excel.Worksheet)wbook.ActiveSheet;
            wsheet.Name = "Customers"; 
    
    
    
            DataTable dt = InstituteTypeDetail();
    
            int rowCount = 1;
            foreach (DataRow dr in dt.Rows)
            {
                rowCount += 1;
                for (int i = 1; i < dt.Columns.Count + 1; i++)
                {
                    // Add the header the first time through
                    if (rowCount == 2)
                    {
                        wsheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
                    }
                    wsheet.Cells[rowCount, i] = dr[i - 1].ToString();
                }
            }
    
            range = wsheet.get_Range(wsheet.Cells[1, 1],
                          wsheet.Cells[rowCount, dt.Columns.Count]);
            range.EntireColumn.AutoFit();
    
    
            wsheet = null;
            range = null;
           
        }
    Last edited by tlhintoq; Mar 9 '10, 01:59 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    Working...