0

我正在使用 OleDB 创建一个 Excel 文件,它运行良好,接下来,我直接在 Office Excel 中使用向文件添加行,之后,当我再次尝试使用 OleDB 读取文件时,我手动添加的行不会出现,只有我创建文件时写的列和行。

这是代码:

//Creation of the file
        String connectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + path + "; Mode=ReadWrite; Extended Properties= 'Excel 8.0; HDR=Yes; IMEX=0'";
        OleDbConnection oledbconnection;

        oledbconnection = new OleDbConnection(connectionString);
        oledbconnection.Open();

        String query = "CREATE TABLE [Sheet1] (....) ";

        OleDbCommand cmd = new OleDbCommand(query, oledbconnection);
        cmd.ExecuteNonQuery();

        oledbconnection.Close();

它运行良好,文件创建正确,然后,当我在修改后尝试读取文件时,我正在这样做

        String connectionStringRead = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + path + "; Extended Properties= 'Excel 8.0; HDR=Yes; IMEX=1'";

        OleDbConnection oledbconnection;
        oledbconnection = new OleDbConnection(connectionStringRead);
        oledbconnection.Open();

        DataTable table = new DataTable();
        String sheetName;

        sheetName = oledbconnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString();

        OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [" + sheetName + "]", oledbconnection);
        adapter.Fill(table);

        oledbconnection.Close();
        return table;

该表仅包含原始信息,而不是我使用 MS Excel 添加的行。

我不明白这个,你能帮我吗?

提前致谢

4

1 回答 1

1

从来没有这样尝试过,但如果

sheetName = "Sheet1" '-------> or whatever

它应该是

OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [" + sheetName + "$]", oledbconnection);
于 2013-07-12T01:58:30.037 回答