Trying to read an Excel file with EPPlus works on the server, but not through a browser

被刻印的时光 ゝ 提交于 2019-12-21 09:26:13

问题


When I published my project and ran it on the server, it worked. EPPlus found all 4 worksheets, iterated through them, and uploaded my data to SQL.

But when I run it through my browser, or my coworkers browser, it shows 0 worksheets.

Any idea why this might be happening? There's not much to the code at that point, but here's that part:

using (ExcelPackage package = new ExcelPackage(new FileInfo(strFile)))
{
    if (package.Workbook.Worksheets.Count <= 0)
        strError = "Your Excel file does not contain any work sheets";
    else
    {
        foreach (ExcelWorksheet worksheet in package.Workbook.Worksheets)
        {

回答1:


EPPlus can load a file into memory. You're just not doing it that way. I think if you do this, you're less likely to run into trouble reading it from the file system. You can turn uploaded files into a byte array without having it as a file first, but in my example I'm opening an existing file. If you provide the code for how you're uploading the file, I can update my example.

byte[] file = File.ReadAllBytes(@"C:\file.xlsx");
using (MemoryStream ms = new MemoryStream(file))
using (ExcelPackage package = new ExcelPackage(ms))
{
    if (package.Workbook.Worksheets.Count == 0)
        strError = "Your Excel file does not contain any work sheets";
    else
    {
        foreach (ExcelWorksheet worksheet in package.Workbook.Worksheets)
        {


来源:https://stackoverflow.com/questions/22717271/trying-to-read-an-excel-file-with-epplus-works-on-the-server-but-not-through-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!