尝试使用EPPlus读取Excel文件在服务器上工作,但不能通过浏览器 [英] Trying to read an Excel file with EPPlus works on the server, but not through a browser

查看:619
本文介绍了尝试使用EPPlus读取Excel文件在服务器上工作,但不能通过浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我发布我的项目并在服务器上运行它的工作。 EPPlus发现所有4个工作表,遍历它们,并将我的数据上传到SQL。

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.

但是当我通过浏览器或我的同事浏览器运行它时,它显示0个工作表。

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)
        {


推荐答案

EPPlus可以将文件加载到内存中。你只是不这样做。我想如果你这样做,你不太可能遇到麻烦,从文件系统读取它。您可以将上传的文件转换为字节数组,而不必先将其作为文件,但在我的示例中,我打开一个现有的文件。如果您提供上传文件的代码,我可以更新我的示例。

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)
        {

这篇关于尝试使用EPPlus读取Excel文件在服务器上工作,但不能通过浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆