Exception occurs reading spreadsheet after saving ExcelPackage (EPPlus) -
i manipulating data in .xlsx spreadsheet using epplus (4.0.4) , saving file, afterwards loading parts of data datatables.
after call .save() method spreadsheet saved 'external table not in expected format.' exception when trying fill dataadapter.
i've created console app minimise amount of code required demonstrate issue, here code:
using officeopenxml; using system; using system.collections.generic; using system.data; using system.data.oledb; using system.io; using system.linq; using system.text; using system.threading.tasks; namespace epplussaveissue { class program { static void main(string[] args) { string pathtofile = "c:\\users\\peter\\documents\\test.xlsx"; fileinfo newfile = new fileinfo(pathtofile); using (var pck = new excelpackage(newfile)) { pck.save(); } string connectionstringformat = "provider=microsoft.ace.oledb.12.0;data source={0};extended properties=\"excel 12.0;hdr=no;imex=1\""; using (oledbconnection con = new oledbconnection(string.format(connectionstringformat, pathtofile))) { using (oledbdataadapter dataadapter = new oledbdataadapter("select * [sheet1$] ", con)) { datatable datatable = new datatable(); dataadapter.fill(datatable); console.writeline(datatable.rows.count.tostring()); } } } } }
steps reproduce:
- run application against newly created spreadsheet, exception occurs
- comment out pck.save(); line , exception still occurs (the spreadsheet in way mangled @ point)
- open spreadsheet in excel , save it
- run application again (with pck.save() still commented out) , runs
- put pck.save(); line in , exception occurs.
so, there's workaround impractical, i.e. manipulation save file using epplus save in excel run process without saving using epplus.
it seems .save() method putting file in unusual state, don't know else try, advice appreciated.
you cannot call save()
because workbook doesn't have worksheet bro. @ least 1 sheet required normal excel file.
try add var ws = pck.workbook.worksheets.add("sheet1");
before save()
.
i used epplus long time ago. don't know why document removed dev team may put time, below link.
Comments
Post a Comment