简体   繁体   English

c# 使用 oledb 从 excel 读取错误

[英]c# wrong reading from excel with oledb

 private void OnCreated(object sender, FileSystemEventArgs e)
        {
            excelDataSet.Clear();
            string extension = Path.GetExtension(e.FullPath);
            if (extension == ".xls" || extension == ".xlsx")
            {
                string ConnectionString = "";
                if (extension == ".xls") { ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = '" + e.FullPath + "';Extended Properties=\"Excel 8.0;HDR=YES;\""; }
                if (extension == ".xlsx") { ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source = '" + e.FullPath + "';Extended Properties=\"Excel 12.0;HDR=YES;\""; }

                using (OleDbConnection conn = new OleDbConnection(ConnectionString))
                {
                    conn.Open();
                    OleDbDataAdapter objDA = new OleDbDataAdapter("select * from [Sheet1$]", conn);
                    objDA.Fill(excelDataSet);
                    conn.Close();
                    conn.Dispose();
                }
            }
        }

This is my code.这是我的代码。 It's working when my filewatcher triggers.当我的 filewatcher 触发时它正在工作。 Problem is the excel file I read has 1 header row and 3 row that has values.问题是我读取的 excel 文件有 1 行 header 和 3 行有值。 When I use this code and check my dataset row count I get 9.. I've no idea where does it take that 9 from, am I doing something wrong?当我使用此代码并检查我的数据集行数时,我得到 9.. 我不知道它从哪里获取 9,我做错了什么吗? I'm checking my code for last 30-35 min and still couldn't find what I'm doing wrong.. I get the column's right but the rows are not working.我检查了最后 30-35 分钟的代码,但仍然找不到我做错了什么。我得到了正确的列,但行不工作。 I don't need the header line btw顺便说一句,我不需要 header 行

Update: my example excel file had 3 rows and I was getting 9 as row count.更新:我的示例 excel 文件有 3 行,我得到 9 作为行数。 I just copied these rows and made my file 24 row + 1 header row and when I did rows.count I got 24 as answer.我只是复制了这些行并使我的文件成为 24 行 + 1 header 行,当我执行 rows.count 时,我得到了 24 作为答案。 So it worked fine?所以它工作正常? Is that normal?那是正常的吗?

There is a Nuget called Linq to Excel. I used this nuget in several projects to query the data inside.csv and.xlsx files without any difficulty, it is easy to implement.有一个Nuget叫Linq到Excel,我在几个项目中都用这个nuget查询里面的数据.csv和.xlsx文件没有任何难度,很容易实现。 It might be poor in performance but it can resolve your problem.它的性能可能很差,但可以解决您的问题。

Here is the documentation of Linq to Excel这是Linq 到 Excel的文档

I would highly recommend you to take a look at EPPLUS library https://github.com/JanKallman/EPPlus/wiki我强烈建议你看看 EPPLUS 库https://github.com/JanKallman/EPPlus/wiki

I have plently of trouble with oledb until i found EPPLUS.在找到 EPPLUS 之前,我在 oledb 上遇到了很多麻烦。 It's really easy to use for creating and updating excel files.创建和更新 excel 个文件非常容易使用。 There are plenty of good examples out there like the one under which is from How do i iterate through rows in an excel table using epplus?那里有很多很好的例子,比如我如何使用 epplus 遍历 excel 表中的行?

var package = new ExcelPackage(new FileInfo("sample.xlsx"));

ExcelWorksheet workSheet = package.Workbook.Worksheets[1];
var start = workSheet.Dimension.Start;
var end = workSheet.Dimension.End;
for (int row = start.Row; row <= end.Row; row++)
{ // Row by row...
    for (int col = start.Column; col <= end.Column; col++)
    { // ... Cell by cell...
        object cellValue = workSheet.Cells[row, col].Text; // This got me the actual value I needed.
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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