外部表格不是预期的格式

时间:2011-05-15 04:31:27

标签: c# linq excel oledb

我一直没有获得预期格式的外部表格,我不知道为什么。我尝试了2003和2007连接字符串技术,但它仍然没有用。

以下是我的代码。谢谢!

string filename = Path
            .GetDirectoryName(System.Reflection.Assembly
            .GetExecutingAssembly().GetName().CodeBase) +
            "\\acu.xls";
        filename = filename.Substring(6);

        //string connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
            //"Data Source=" + filename + ";" +
            //"Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1;\"";

        string connString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
            "Data Source=" + filename + ";" +
            "Extended Properties=\"Excel 12.0;HDR=YES\"";


        OleDbDataAdapter dbAdapter = new OleDbDataAdapter("Select * from [Sheet1$]", connString);

        DataSet myDataSet = new DataSet();
        dbAdapter.Fill(myDataSet, "ACUList");

        DataTable dataTable = myDataSet.Tables["ACUList"];

        var query = from r in dataTable.AsEnumerable()
                    select new
                        {
                            Firstname = r.Field<string>("FIRSTNAME"),
                            CardNo = r.Field<string>("CARDNO")
                        };

        foreach (var item in query)
        {
            Console.WriteLine(item);
        }
        Console.ReadKey();

0 个答案:

没有答案