读取生成的Excel文件时缺少行

本文关键字:文件 Excel 读取 | 更新日期: 2023-09-27 18:22:05

我正在使用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添加的行。

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

提前感谢

读取生成的Excel文件时缺少行

以前从未尝试过,但如果

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

应该是

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