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

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

问题描述

当我发表我的项目并运行它在服务器上的,它的工作。 EPPlus发现的所有4工作表,通过他们迭代,并上传我的数据到SQL。

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

任何想法,为什么这可能发生?没有太多的code。在这一点上,但这里的那一部分:

 使用(ExcelPackage包=新ExcelPackage(新的FileInfo(strFile)))
{
    如果(package.Workbook.Worksheets.Count&下; = 0)
        STRERROR =您的Excel文件不包含任何工作单;
    其他
    {
        的foreach(在package.Workbook.Worksheets ExcelWorksheet工作表)
        {


解决方案

EPPlus可以将文件加载到内存中。你只是没有做这种方式。我想如果你这样做,你就不太可能遇到麻烦从文件系统读取它。你可以把上传的文件到字节数组,而不必首先它作为一个文件,但在我的例子中,我打开现有的文件。如果你提供code你是如何上传文件,我可以更新我的例子。

 字节[] =文件File.ReadAllBytes(@C:\\ file.xlsx);
MemoryStream的毫秒=新的MemoryStream(文件);
使用(ExcelPackage包=新ExcelPackage(MS))
{
    如果(package.Workbook.Worksheets.Count == 0)
        STRERROR =您的Excel文件不包含任何工作单;
    其他
    {
        的foreach(在package.Workbook.Worksheets ExcelWorksheet工作表)
        {

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

解决方案

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

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

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