简体   繁体   中英

ExcelLibrary and EPPlus throwing exceptions when opening network file

I have an excel file on a server. I access it with a path looking like this: "\\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.

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...

//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. I want a library that only need to add a reference to a dll. I need to open excel 2010 files. 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.

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.

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. 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? Because on NT family FileInfo constructor is calling Path.NormalizeSlow and that method is throwing exception:

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

Try with new Epplus library 4.1

Most of bugs are fixed in this version

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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