Click here to Skip to main content
15,883,749 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
See more:
I'm getting an error when I'm exporting Excel to C# , I can't find where my code is wrong and the solution for my problem :s

Error :

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in GestãoSI.exe

Additional information: Índice inválido. (Excepção de HRESULT: 0x8002000B (DISP_E_BADINDEX))

The error appear when the code is running

C#
// Add a workbook.
oBook = oExcel_12.Workbooks.Add(oMissing);

// Get worksheets collection
oSheetsColl = oExcel_12.Worksheets;

// Get Worksheet "Sheet1"
oSheet = (Excel_12.Worksheet)oSheetsColl.get_Item("Sheet1");



Here is all my code

C#
public static void ExportDataGridViewTo_Excel12(DataGridView itemDataGridView)
    {
        Excel_12.Application oExcel_12 = null;                //Excel_12 Application
        Excel_12.Workbook oBook = null;                       // Excel_12 Workbook
        Excel_12.Sheets oSheetsColl = null;                   // Excel_12 Worksheets collection
        Excel_12.Worksheet oSheet = null;                     // Excel_12 Worksheet
        Excel_12.Range oRange = null;                         // Cell or Range in worksheet
        Object oMissing = System.Reflection.Missing.Value;

        // Create an instance of Excel_12.
        oExcel_12 = new Excel_12.Application();

        // Make Excel_12 visible to the user.
        oExcel_12.Visible = true;

        // Set the UserControl property so Excel_12 won't shut down.
        oExcel_12.UserControl = true;

        // System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");

        // Add a workbook.
        oBook = oExcel_12.Workbooks.Add(oMissing);

        // Get worksheets collection 
        oSheetsColl = oExcel_12.Worksheets;

        // Get Worksheet "Sheet1"
        oSheet = (Excel_12.Worksheet)oSheetsColl.get_Item("Sheet1");

        // Export titles
        for (int j = 0; j < itemDataGridView.Columns.Count; j++)
        {
            oRange = (Excel_12.Range)oSheet.Cells[1, j + 1];
            oRange.Value2 = itemDataGridView.Columns[j].HeaderText;
        }

        // Export data
        for (int i = 0; i < itemDataGridView.Rows.Count - 1; i++)
        {
            for (int j = 0; j < itemDataGridView.Columns.Count; j++)
            {
                oRange = (Excel_12.Range)oSheet.Cells[i + 2, j + 1];
                oRange.Value2 = itemDataGridView[j, i].Value;
            }
        }

        // Release the variables.
        //oBook.Close(false, oMissing, oMissing);
        oBook = null;

        //oExcel_12.Quit();
        oExcel_12 = null;

        // Collect garbage.
        GC.Collect();
    }
Posted
Updated 14-May-13 1:19am
v2
Comments
Rockstar_ 14-May-13 7:19am    
Put break point and check line by line...
Raja Soosai 14-May-13 7:20am    
Yes. please let us know the problematic code line no atleast by debugging.
Pedro Brito 14-May-13 7:21am    
The error is in this line : oSheet = (Excel_12.Worksheet)oSheetsColl.get_Item("Sheet1");

I changed to oSheet = (Excel_12.Worksheet)oSheetsColl.get_Item(1); and now i can get the collumns name but i can´t get the data that is on the datagridview
CHill60 14-May-13 7:27am    
If you use itemDataGridView[j, i].Value.ToString() does it make a difference?
Pedro Brito 14-May-13 7:35am    
no , the same ... , just appear the title

Hi
HRESULT: 0x8002000B (DISP_E_BADINDEX)) this means invalid index, so within your for loop you are accessing a index that doesn't exists.

1) check where the index is going wrong;
2) make sure the cell is converted to range as below

C#
// Export titles
for (int j = 0; j < itemDataGridView.Columns.Count; j++)
{
    oRange = oSheet.Cells[1, j + 1] as Excel_12.Range;
    oRange.Value2 = itemDataGridView.Columns[j].HeaderText;
}

// Export data
for (int i = 0; i < itemDataGridView.Rows.Count - 1; i++)
{
    for (int j = 0; j < itemDataGridView.Columns.Count; j++)
    {
        oRange = oSheet.Cells[i + 2, j + 1] as Excel_12.Range;
        oRange.Value2 = itemDataGridView.Rows[i].Cells[j].Value;
    }
}


you can see I removed the cast and used the "as"; the reason for this is if the cell returns null, then you cannot cast them, but the "as" will assign it as null.

hope this helps.

Jegan
 
Share this answer
 
v2
Comments
Pedro Brito 14-May-13 7:39am    
I get an error on Range , "the type or namespace range can not be found"
Jegan Thiyagesan 14-May-13 7:40am    
ok, then give the full namespace Excel_12.Range.
Pedro Brito 14-May-13 7:45am    
I am not understanding what to do , when i put Excel_12.Range on my namespace it returns an error to

This are my namespaces that i am using

using VBIDE_12 = Microsoft.Vbe.Interop;
using Office_12 = Microsoft.Office.Core;
using Excel_12 = Microsoft.Office.Interop.Excel;
Jegan Thiyagesan 14-May-13 7:49am    
No! I didn't mean you add to the namespace it self, use the alias name i.e.

oRange = oSheet.Cells[1, j + 1] as Excel_12.Range;

oRange = oSheet.Cells[i + 2, j + 1] as Excel_12.Range;
Pedro Brito 14-May-13 7:53am    
I get the same problem , it just give me the title and not the data that is on the datagridview :(
By Default when you create an excel Workbook using C# visual Studio it can creates only 3 sheets, in order to avoid this error, you must add new sheet by using
method excelapp.Worksheets.Add(); it will resolve the exception.
 
Share this answer
 
Thanks all for your answer , i discover my problem :)))


C#
public static void ExportDataGridViewTo_Excel12(DataGridView itemDataGridView)
       {
           Excel_12.Application oExcel_12 = null;                //Excel_12 Application
           Excel_12.Workbook oBook = null;                       // Excel_12 Workbook
           Excel_12.Sheets oSheetsColl = null;                   // Excel_12 Worksheets collection
           Excel_12.Worksheet oSheet = null;                     // Excel_12 Worksheet
           Excel_12.Range oRange = null;                         // Cell or Range in worksheet
           Object oMissing = System.Reflection.Missing.Value;

           // Create an instance of Excel_12.
           oExcel_12 = new Excel_12.Application();

           // Make Excel_12 visible to the user.
           oExcel_12.Visible = true;

           // Set the UserControl property so Excel_12 won't shut down.
           oExcel_12.UserControl = true;

           // System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");

           // Add a workbook.
           oBook = oExcel_12.Workbooks.Add(oMissing);

           // Get worksheets collection
           oSheetsColl = oExcel_12.Worksheets;

           // Get Worksheet "Sheet1"
           oSheet = (Excel_12.Worksheet)oSheetsColl.get_Item(1);

           // Export titles
           for (int j = 0; j < itemDataGridView.Columns.Count; j++)
           {
               oRange = oSheet.Cells[1, j + 1] as Excel_12.Range;
               oRange.Value2 = itemDataGridView.Columns[j].HeaderText;
           }

           // Export data
           for (int i = 0; i < itemDataGridView.Rows.Count ; i++)
           {
               for (int j = 0; j < itemDataGridView.Columns.Count; j++)
               {
                   oRange = oSheet.Cells[i + 2, j + 1] as Excel_12.Range;
                   oRange.Value2 = itemDataGridView.Rows[i].Cells[j].Value;

               }
           }

           // Release the variables.
           //oBook.Close(false, oMissing, oMissing);
           oBook = null;

           //oExcel_12.Quit();
           oExcel_12 = null;

           // Collect garbage.
           GC.Collect();
       }


The error was in for (int i = 0; i <<big> itemDataGridView.Rows.Count ; i++)
 
Share this answer
 

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