猿问

如何在不使用Microsoft.Office.Interop.Excel库的情况下在C#中读取

我在C#中有一个.Net-Windows应用程序。我需要打开一个Excel并对其进行处理。不使用Microsoft.Office.Interop.Excel库怎么办?



慕运维8079593
浏览 1455回答 3
3回答

ibeautiful

var fileName = @"C:\ExcelFile.xlsx";var connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0;IMEX=1;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text\""; ;using (var conn = new OleDbConnection(connectionString)){    conn.Open();    var sheets = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });    using (var cmd = conn.CreateCommand())    {        cmd.CommandText = "SELECT * FROM [" + sheets.Rows[0]["TABLE_NAME"].ToString() + "] ";        var adapter = new OleDbDataAdapter(cmd);        var ds = new DataSet();        adapter.Fill(ds);    }}
随时随地看视频慕课网APP
我要回答