简体   繁体   English

使用ADO.NET读取内存中的Excel文件(字节数组)?

[英]Read an in-memory Excel file (byte array) with ADO.NET?

I want to use ADO.net to extract some data from an Excel file. 我想使用ADO.net从Excel文件中提取一些数据。 This process is pretty well documented on the internet. 这个过程在互联网上有很好的记录。 My catch is that my file has been uploaded by a user, and so exists only as a byte array in memory. 我的问题是我的文件已被用户上传,因此仅作为内存中的字节数组存在。 For security and performance reasons I would rather not write this file to disk. 出于安全性和性能原因,我宁愿不将此文件写入磁盘。

Is there a way of constructing a connection string that connects to a byte array? 有没有办法构建连接到字节数组的连接字符串? Or perhaps exposing that array as a file that is actually stored in memory (like a RAM disk I guess)? 或者可能将该数组暴露为实际存储在内存中的文件(如我猜的RAM磁盘)?

You can't connect if it only exists in memory. 如果仅存在于内存中,则无法连接。 OLE is also ruled out (though using Office Automation for a server application is poor design to begin with). OLE也被排除(尽管使用Office Automation作为服务器应用程序的设计很糟糕)。

The only way I can think of is to read the binary Excel data yourself - for example use SpreadSheetGear.Net with something like: 我能想到的唯一方法是自己读取二进制Excel数据 - 例如使用SpreadSheetGear.Net,例如:

SpreadsheetGear.Factory.GetWorkbookSet().Workbooks.OpenFromStream(*stream*);

Like Rich B said in his answer, I do not think it is possible to connect in a standard ado.net way to an excel file that is just hanging around in memory. 就像Rich B在他的回答中所说,我认为不可能以标准的ado.net方式连接到只是在内存中闲置的excel文件。 The simplest work-around would probably be to actually save the excel file to the disk, connect to it using the Jet engine with your connection string, and then when you are done performing all your tasks, get rid of the file. 最简单的解决方法可能是将excel文件实际保存到磁盘,使用带有连接字符串的Jet引擎连接到它,然后在完成所有任务后,删除文件。 This may not be the ideal in terms of performance, but it is lacking that certain WTFiness which would cause you to pull your hair out. 这在性能方面可能不是理想的,但缺乏某种可能会让你脱发的WTFiness。

I don't think it is possible to do this via just a connection string. 我不认为只通过连接字符串就可以做到这一点。

If you want help with an Excel file on disk: http://connectionstrings.com/excel-2007 如果您需要有关磁盘上Excel文件的帮助,请访问: http//connectionstrings.com/excel-2007

In general: http://connectionstrings.com/ 一般来说: http//connectionstrings.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM