C# .net 从 .xls 读取缺少内容为 1 的单元格

发布于 2024-10-31 01:24:53 字数 775 浏览 2 评论 0原文

我尝试使用 oledb 从 xls 文件读取数据。该工作表有一列,所有单元格中都有数字。当我执行代码时,每次都会缺少特定的列。我使用的代码是:

OleDbConnection excel_connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file_name + ";Extended Properties=Excel 8.0");
        excel_connection.Open();
        DataSet excel_data = new DataSet();
        OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [XX$]", excel_connection);
        myCommand.Fill(excel_data);
        excel_connection.Close();
        int index = 0;
        string excel_columns;
        foreach (DataRow excel_row in excel_data.Tables[0].Rows)
        {
                excel_columns = String.Join(",", cells);
                richTextBox1.AppendText(excel_columns);

        }

为什么会发生这种情况?

I tried to read data from a xls file using oledb. The sheet had a column which has numbers in all the cells. When I executed the code that particular column alone is missing everytime. The code I used is:

OleDbConnection excel_connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file_name + ";Extended Properties=Excel 8.0");
        excel_connection.Open();
        DataSet excel_data = new DataSet();
        OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [XX$]", excel_connection);
        myCommand.Fill(excel_data);
        excel_connection.Close();
        int index = 0;
        string excel_columns;
        foreach (DataRow excel_row in excel_data.Tables[0].Rows)
        {
                excel_columns = String.Join(",", cells);
                richTextBox1.AppendText(excel_columns);

        }

Why is this happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牛↙奶布丁 2024-11-07 01:24:53

我曾经像你一样阅读 Excel 文档,但我一直遇到列格式问题,所以我决定切换到 NPOI
这是一个很好的产品并且效果很好。实施非常非常容易。

这是一些代码:

using (FileStream Xlfile = new FileStream(MyFileName, FileMode.Open, FileAccess.Read))
{
    using (HSSFWorkbook XLBook = new HSSFWorkbook(Xlfile))
    {
    using (NPOI.SS.UserModel.Sheet XLSheet = XLBook.GetSheetAt(0))
    {
        NPOI.HSSF.UserModel.HSSFRow CurrentRow;
        NPOI.SS.UserModel.Cell CurrentCell;

        IEnumerator RowEnum = XLSheet.GetRowEnumerator();

        while (RowEnum.MoveNext())
        {
        iLoopRows++;

        if (RowEnum.Current != null)
        {
            rowCounter++;
            CurrentRow = RowEnum.Current as NPOI.HSSF.UserModel.HSSFRow;

            for (Int32 iLoop = 0; iLoop < CurrentRow.Cells.Count; iLoop++)
            {
            CurrentCell = CurrentRow.Cells[iLoop];

            switch (CurrentCell.CellType)
            {
                case NPOI.SS.UserModel.CellType.STRING:
                // Reading STRING value
                // CurrentCell.StringCellValue;
                break;
                case NPOI.SS.UserModel.CellType.NUMERIC:
                // Reading NUMERIC and DATA VALUES
                // (CurrentCell.DateCellValue == null) ? "" : CurrentCell.DateCellValue.ToString();
                break;
                default:
                break;
            }
            }
        }
        }
    }
    }
    Xlfile.Close();
}

I used to read Excel document like you do but I kept having problems with column formats so I decided to switch to NPOI.
It is a good product and works pretty well. The implementation is very very easy.

Here is some code:

using (FileStream Xlfile = new FileStream(MyFileName, FileMode.Open, FileAccess.Read))
{
    using (HSSFWorkbook XLBook = new HSSFWorkbook(Xlfile))
    {
    using (NPOI.SS.UserModel.Sheet XLSheet = XLBook.GetSheetAt(0))
    {
        NPOI.HSSF.UserModel.HSSFRow CurrentRow;
        NPOI.SS.UserModel.Cell CurrentCell;

        IEnumerator RowEnum = XLSheet.GetRowEnumerator();

        while (RowEnum.MoveNext())
        {
        iLoopRows++;

        if (RowEnum.Current != null)
        {
            rowCounter++;
            CurrentRow = RowEnum.Current as NPOI.HSSF.UserModel.HSSFRow;

            for (Int32 iLoop = 0; iLoop < CurrentRow.Cells.Count; iLoop++)
            {
            CurrentCell = CurrentRow.Cells[iLoop];

            switch (CurrentCell.CellType)
            {
                case NPOI.SS.UserModel.CellType.STRING:
                // Reading STRING value
                // CurrentCell.StringCellValue;
                break;
                case NPOI.SS.UserModel.CellType.NUMERIC:
                // Reading NUMERIC and DATA VALUES
                // (CurrentCell.DateCellValue == null) ? "" : CurrentCell.DateCellValue.ToString();
                break;
                default:
                break;
            }
            }
        }
        }
    }
    }
    Xlfile.Close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文