Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All!

I have a task to READ and WRITE excel values using JET OLEDB (not interop because it is so slow and portability issue).

My question is can i UPDATE,DELETE,and manipulate excel thought DATAGRIDVIEW. Meaning can i BIND the excel to the datagriedview. Can i use the UpdateChanges method? thank you!
Posted

i am currently using open xml sdk to process document - just a hint.

and yes, you can ;-)
note jet is depricated ace is 32 and 64 bit but must be downloaded seperatly ...
to use jet change the connection string!
and... excel sheet names may have to start with a $
-- ^hell knows why!? --
and... to read csv you might want to use a special schema.ini file to get correct imports <- in case search on google about it

so far, try something like this:

C#
// Source for DataGridView
OledbFileParser excelDataSet = new OledbFileParser(Properties.Settings.Default.CurrentExcelFile);
// use databing object here if required
excelFileGridView1.DataSource = excelDataSet.Tables["excel.xlsx"];

class OledbFileParser : DataSet
{
    public ArrayList SheetNames
    { get; private set; }

    public FileInfo File
    { get; private set; }

    public OledbFileParser(string FileName)
    {
        this.File = new FileInfo(Path.Combine(System.Windows.Forms.Application.StartupPath, FileName));
        if (File.Exists)
        {
            this.DataSetName = File.Name;
            SheetNames = new ArrayList();
            string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                                      File.FullName.ToString() + ";Extended Properties=Excel 12.0;";
            OleDbConnection con = new OleDbConnection(connectionString);
            try
            {
                con.Open();
                DataTable dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new Object[] { null, null, null, "Table" });

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    SheetNames.Add(dt.Rows[i]["TABLE_NAME"].ToString());
                    Tables.Add(new System.Data.DataTable(SheetNames[i].ToString()));
                    // Select using a Worksheet name
                    string selectString = "SELECT * FROM [" + SheetNames[i] + "]"; // here without $
                    OleDbCommand cmd = new OleDbCommand(selectString, con);
                    cmd.CommandType = CommandType.Text;
                    OleDbDataReader oleDataReader2 = cmd.ExecuteReader();
                    int cCnt = oleDataReader2.FieldCount;
                    for (int j = 0; j < cCnt; j++)
                    {
                        Tables[SheetNames[i].ToString()].Columns.Add(oleDataReader2.GetName(j).ToString());
                    }
                    string[] rowData = new string[cCnt];
                    while (oleDataReader2.Read())
                    {
                        for (int k = 0; k < cCnt; k++)
                        {
                            try
                            {
                                // pass all as text
                                if (oleDataReader2.GetFieldType(k).ToString() == "System.Int32")
                                {
                                    if (!oleDataReader2.IsDBNull(k)) { rowData[k] = oleDataReader2.GetInt32(k).ToString(); }
                                }
                                else if (oleDataReader2.GetFieldType(k).ToString() == "System.String")
                                {
                                    if (!oleDataReader2.IsDBNull(k)) { rowData[k] = oleDataReader2.GetString(k); }
                                }
                                else if (!oleDataReader2.IsDBNull(k)) { rowData[k] = oleDataReader2.GetValue(k).ToString(); }
                            }
                            catch (Exception ex) { ErrorHandling.printUniversalErrorMassage(ex, "erro" + k); }
                        }
                        Tables[SheetNames[i].ToString()].Rows.Add(rowData);
                    }
                }
            }
            catch (Exception ex) { ErrorHandling.printUniversalErrorMassage(ex, "read error"); }
            finally
            {
                con.Dispose();
            }
        }
        else
        {
            MessageBox.Show(File.FullName.ToString() + " not found");
        }
    }
}


writing may be a pain - i dont know. i used this clumsy code for contact import from outlook and it worked (well).
 
Share this answer
 
v3
There is the C# Excel tutorialhttp://csharp.net-informations.com/excel/csharp-excel-tutorial.htm[^]. At the bottom, itfous on JET OLEDB.
This is talking about three ways to work with workbook and their cons and pros: http://paradise4csharp.wordpress.com/2012/04/18/three-ways-to-operate-excel-workbook-via-c/[^]

Regards
 
Share this answer
 
You need to integrate into Excel itself... Excel offers an interface to achieve exactly what you describe... it is based on COM and called RTD (Realtime Data-Server) and gets called from Excel.

You have to build a COM component which adheres to the official spec...

Some usfeul links to get you started:

http://msdn.microsoft.com/en-us/library/aa140059%28v=office.10%29.aspx[^]

http://www.add-in-express.com/docs/net-excel-rtd-servers.php[^]
 
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