3

问题是第一列是空的,无法读取。 在此处输入图像描述

为了读取文件,我编写了以下代码:

            MyConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "'" + filename + "'" + ";Extended Properties=\"Excel 8.0;IMEX=1;HDR=NO;TypeGuessRows=0\"");

            DtSet = new DataSet();
            MyConnection.Open();
            var dt = MyConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

                MyCommand = new OleDbDataAdapter("select * from [$first]", MyConnection);
                MyCommand.Fill(DtSet); // filldata

                dataExcel.AddRange(fillData(DtSet.Tables["Table"]).Select(info => info.ToList())); 
            }

List<List<string>> fillData(DataTable DtSet) //reader
        {
            List<List<string>> data = new List<List<string>>();
            foreach (DataRow item in DtSet.Rows)
            {
                if (item == null) continue;
                List<string> gettedData = new List<string>();
                int i = 0;
                foreach (var columnitem in item.ItemArray)
                {
                    if (i == 0 && columnitem.ToString() == "") ;
                    else gettedData.Add(columnitem.ToString()); //add data

                    i++;
                }
                data.Add(gettedData);
            }
            return data;
        }

此代码有效,但第一列,他没有看到。谢谢!

4

0 回答 0