Oledb跳过excel文件的第一列

大卫·杰

当我尝试使用 OLEDB 将 Excel 文件保存到 C# 上的 DataTable 中时遇到问题。

DataTable 没有我的 Excel 文件的第一列,但有一个额外的空列作为最后一列。我不知道我做错了什么。如果我的 Excel 文件有这样的内容:

身份证、姓名、地址

数据表列是:

姓名、地址、F3(空)

这是我的功能:

    public DataTable GetExcel(string[] files, string sheetName, string pathName)
    {
        for (int i = 0; i < files.Length; i++)
        {
            string strConn = string.Empty;
            if (string.IsNullOrEmpty(sheetName)) { sheetName = "Sheet1"; }
            FileInfo file = new FileInfo(files[i]);
            if (!file.Exists) { throw new Exception("Error, file doesn't exists!"); }
            string extension = file.Extension;
            switch (extension)
            {
                case ".xls":
                    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
                    break;
                case ".xlsx":
                    strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + file + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'";
                    break;
                default:
                    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
                    break;
            }
            OleDbConnection cnnxls = new OleDbConnection(strConn);
            OleDbDataAdapter oda = new OleDbDataAdapter(string.Format("select * from [{0}$]", sheetName), cnnxls);
            oda.Fill(dataTable);
        }
        return dataTable;
    }

谢谢!

大卫·杰

最后问题出在 Excel 文件上,我只是使用另一台计算机将所有内容复制到另一个 Excel 文件中并工作。我不知道出了什么问题。

我希望我能帮助别人。

感谢您的回答。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章