这里有一个类似的问题
In Memory OleDbConnection to Excel File但是,通过另一种方式完全避免它来回答这个问题.
下面是一些使用OleDbConnection从磁盘访问Excel文件的示例代码:
static void Main(string[] args) { String filePathToExcelFile = "c:\\excelfile.xls"; Boolean hasHeaders = true; String connectionString = String.Format( "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};" + "Extended Properties=\"Excel 12.0;HDR={1};IMEX=2\"",filePathToExcelFile,hasHeaders ? "Yes" : "No"); using(OleDbConnection conn = new OleDbConnection(connectionString)) using (OleDbCommand command = new OleDbCommand("SELECT * FROM [Sheet1$]",conn)) { conn.Open(); OleDbDataReader datareader = command.ExecuteReader(); while(datareader.Read()) { Object[] values = new object[datareader.FieldCount]; datareader.GetValues(values); Console.WriteLine(String.Join(",",values)); } } }
我想从NON-SEEKABLE System.IO.Stream中提取Excel文件,而不是磁盘上的持久文件.
问题分为两部分,a)我可以将OleDbConnection“指向”System.IO.Stream吗? b)如果可以,那么只能是前向流而不是可寻找流?
仅供参考:如果您想运行此代码片段,则需要安装Microsoft Access Database Engine 2010 Redistributable.如果您安装64位,则需要将项目定位为x64,反之亦然.
解决方法
检查这是否有帮助:
http://epplus.codeplex.com/
还有示例代码:
string fileName = System.Windows.Forms.Application.StartupPath + "\\Resources\\SUPPLIERDECISIONKEYLIST.xlsx"; using (var pck = new OfficeOpenXml.ExcelPackage()) { using (var stream = File.OpenRead(fileName)) { pck.Load(stream); } var ws = pck.Workbook.Worksheets[SheetNo];