简体   繁体   English

ExcelLibrary 和 EPPlus 在打开 .network 文件时抛出异常

[英]ExcelLibrary and EPPlus throwing exceptions when opening network file

I have an excel file on a server.我在服务器上有一个 excel 文件。 I access it with a path looking like this: "\\Server\folder\file.xlsx".我使用如下路径访问它:“\\Server\folder\file.xlsx”。

I was able to read it with the Excel API from office, but since I cannot install office on my server, I need to avoid this dependency.我能够从办公室使用 Excel API 读取它,但由于我无法在我的服务器上安装办公室,我需要避免这种依赖性。

I tried using some other libraries, but it does not work...我尝试使用其他一些库,但它不起作用......

I tried opening the file with EPPLUS, but I have an exception saying " This operation is not supported for a relative URI" when I try to open the workbook... I tried with ExcelLibrary, but I have an out of memory exception...我尝试使用 EPPLUS 打开文件,但当我尝试打开工作簿时出现异常,提示“相对 URI 不支持此操作”...我尝试使用 ExcelLibrary,但出现 memory 异常。 .

//EXCELLIBRARY
Workbook book = Workbook.Load(_filename);

//EPPLUS
FileInfo file = new FileInfo(_filename);
ExcelPackage pack = new ExcelPackage(file);
ExcelWorksheet sheet = pack.Workbook.Worksheets[1];

Does someone have an idea on how to solve this?有人知道如何解决这个问题吗?

If not, you have another library to suggest?如果没有,您有另一个图书馆可以推荐吗? I don't want to use OleDB and already tried NOPI.我不想使用 OleDB 并且已经尝试过 NOPI。 I want a library that only need to add a reference to a dll. I need to open excel 2010 files.我想要一个只需要添加对 dll 的引用的库。我需要打开 excel 2010 文件。 I also need to be able to determine what is the used range in the sheet and get a array with the values.我还需要能够确定工作表中的使用范围并获取包含值的数组。 Here is the code that I used and worked with the office API.这是我在 API 办公室使用和工作的代码。

Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;

xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Open(_filename, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

 _range = (System.Array)xlWorkSheet.UsedRange.Value2;
 _headers = GetHeaders(_range);

 _lastRow = xlWorkSheet.Cells.Find("*", misValue, misValue, misValue, Excel.XlSearchOrder.xlByRows,
 Excel.XlSearchDirection.xlPrevious, false, misValue, misValue).Row;

 xlWorkBook.Close(false, misValue, misValue);
 xlApp.Quit();

After searching and trying many libraries all day, I finally found something that works, Koogra.在整天搜索和尝试许多库之后,我终于找到了一些有用的东西,Koogra。

http://sourceforge.net/projects/koogra/?_test=b http://sourceforge.net/projects/koogra/?_test=b

Workbook wb = new Workbook(_filename);
_worksheet = wb.GetWorksheet(0);

//You can do the same to find LastCol, LastRow or FirstCol
uint firstRow = _worksheet.CellMap.FirstRow
string aHeader = (string)_worksheet.GetRow(firstRow).GetCell(3);

That's not what a.network path looks like.那不是 a.network 路径的样子。 It's supposed to be something like它应该是这样的

\\server\path\to\file\image.jpg

Notice the double slashes at the beginning.注意开头的双斜杠。 With only one slash it becomes a path relative to the current drive (which is what the error is telling you).只有一个斜线,它成为相对于当前驱动器的路径(这是错误告诉你的)。

It seems to me that FileInfo constructor is throwing that exception, are you sure that EEPlus is throwing it?在我看来,FileInfo 构造函数正在抛出该异常,您确定 EEPlus 正在抛出它吗? Because on NT family FileInfo constructor is calling Path.NormalizeSlow and that method is throwing exception:因为在 NT 系列上,FileInfo 构造函数调用 Path.NormalizeSlow 并且该方法抛出异常:

throw new ArgumentException(Environment.GetResourceString("Argument_PathUriFormatNotSupported"));

Try with new Epplus library 4.1尝试使用新的 Epplus 库 4.1

Most of bugs are fixed in this version大多数错误已在此版本中修复

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

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