简体   繁体   中英

C# read in Excel file

I want to get the data of an excel-file(from A2-A28--> first column) into my C# program. My code looks like this:

    using System.IO;    
    using Excel = Microsoft.Office.Interop.Excel; 

    private void btnEinlesen1_Click(object sender, EventArgs e)
    {
        ofd.Filter = "Excel (*.xls;*.xlsx)|*.xls;*.xlsx;|" + "All files (*.*)|*.*";
        ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        ofd.Title = "Exceldatei öffnen";
        ofd.FileName = "";
        DialogResult dr = ofd.ShowDialog();
        if (dr == DialogResult.OK)
        {
            Excel.Application xlApp;      //Excel starten
            Excel.Workbook xlWorkBook;   // Dokument anlegen
            Excel.Worksheet xlWorkSheet; // Blatt anlagen
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open(ofd.FileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);  //Zugriff auf erstes Sheet

            string aux = "";
            for (int i = 2; i <= 28; i++)
            {
                aux += xlWorkSheet.get_Range("A" + i.ToString(), "A" + i.ToString()).Value.toString() + "/";
            }
            aux.ToArray(); //wandelt string aux in ein array um
            MessageBox.Show(aux.ToString());

            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);

        }
    }
    private void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
            MessageBox.Show("Unable to release the Object " + ex.ToString());
        }
        finally
        {
            GC.Collect();
        }
    } 
}

}

But every time 1 read in the excel file (where A2-A28 is full with numbers) i get an error:

在此处输入图片说明

what's the problem?

http://aspxdeveloper.blogspot.com.au/2013/03/read-excel-file-in-c.html

protected void BtnReadExcel_Click(object sender, EventArgs e)
{
    try
    {
        OleDbConnection con = new OleDbConnection("
            Provider=Microsoft.Jet.OLEDB.4.0;
            Data Source=" + Server.MapPath("TestExcel.xlsx") + ";
            Extended Properties='Excel 8.0;HDR=Yes;'" 
        );

        OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
    }
    catch (Exception ex)
    {
        throw;
    }
}

There is no need to loop with the get_Range method.

Remove

string aux = "";
for (int i = 2; i <= 28; i++)
{
    aux += xlWorkSheet.get_Range("A" + i.ToString(), "A" + i.ToString()).Value.toString() + "/";
}
aux.ToArray(); //wandelt string aux in ein array um

And just add this:

Excel.Range xlRange = xlWorkSheet.get_Range("A2:A28", System.Reflection.Missing.Value);
System.Array dataArray = (System.Array)(xlRange.Cells.Value2); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM