简体   繁体   English

MVC 5 Epplus Excel发现不可读的内容

[英]MVC 5 Epplus Excel Found Unreadable Content

I am trying to write some information to an excel file. 我正在尝试将一些信息写入Excel文件。 I can write data in it with using EPPlus. 我可以使用EPPlus在其中写入数据。 And download the excel file. 并下载Excel文件。 But when I run and download this file and open, I see this error. 但是,当我运行并下载此文件并打开时,我看到此错误。

"Excel found unreadable content in filename.xls. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes." “ Excel在filename.xls中发现了不可读的内容。是否要恢复此工作簿的内容?如果您信任此工作簿的来源,请单击“是”。

Even if I did not write anything to file, I got this error. 即使我没有向文件写入任何内容,也出现了此错误。 My code in Excel class is: 我在Excel类中的代码是:

using MaasRaporlari.Models;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace MaasRaporlari.Controllers
{
    class AyrintiliHarcamaRapor
    {
        public List<AyrintiliHarcamaProgramiVM2> Ayrintili;

        private static int RaporaEklenebilecekIcerikSayısı = 33;

        public string ExcelTemplatePath { get; set; } // = HostingEnvironment.MapPath(Url.Content("~/Content/Xsl/")); //@"C:\test\OdemeEmriBelgesi.xlsx";
        public string ExcelResultPath { get; set; } // = @"C:\test\OdemeEmriSonuc.xlsx";
        public FileStream Save()
        {
            try
            {
                ExcelTemplatePath = HttpContext.Current.Server.MapPath("~/Images/AyrintiliHarcamaProgrami.xlsx");
                ExcelResultPath = HttpContext.Current.Server.MapPath("~/Images/Harcama.xls");
                var File = new FileInfo(ExcelTemplatePath);
                using (ExcelPackage package = new ExcelPackage(File))
                {
                    package.Load(new FileStream(ExcelTemplatePath, FileMode.Open));
                    int sheetCount = (int)Math.Ceiling((double)Ayrintili.Count / RaporaEklenebilecekIcerikSayısı);
                    ExcelWorksheet workSheet = package.Workbook.Worksheets["Sheet1"];
                    List<ExcelWorksheet> workSheets = new List<ExcelWorksheet>();
                    workSheets.Add(workSheet);
                    package.Stream.Position = 0;
                    if (sheetCount >= 2)
                    {
                        for (int i = 0; i < sheetCount - 1; i++)
                        {
                            ExcelWorksheet tempSheet = package.Workbook.Worksheets.Add(string.Format("Sheet{0}", i + 2), workSheet);
                            workSheets.Add(tempSheet);
                        }
                    }

                    int icerikSayac = 0;
                    foreach (ExcelWorksheet workSheetItem in workSheets)
                    {
                        package.Stream.Position = 0;
                        decimal MiktarToplam1 = 0;
                        decimal MiktarToplam2 = 0;
                        decimal MiktarToplam3 = 0;
                        decimal MiktarToplam4 = 0;
                        decimal MiktarToplam5 = 0;
                        decimal OranToplam1 = 0;
                        decimal OranToplam2 = 0;
                        decimal OranToplam3 = 0;
                        decimal OranToplam4 = 0;
                        decimal OranToplam5 = 0;

                        MiktarToplam Toplam = new MiktarToplam();

                        int rowPointer = 10;

                        for (int i = 0; i < RaporaEklenebilecekIcerikSayısı; i++)
                        {
                            if (icerikSayac < Ayrintili.Count)
                            {
                                var item = Ayrintili[icerikSayac];

                                package.Stream.Position = 0;

                                MiktarToplam5 = item.Miktar1 + item.Miktar2 + item.Miktar3 + item.Miktar4;
                                OranToplam5 = item.Oran1 + item.Oran2 + item.Oran3 + item.Oran4;

                                MiktarToplam1 += item.Miktar1;
                                MiktarToplam2 += item.Miktar2;
                                MiktarToplam3 += item.Miktar3;
                                MiktarToplam4 += item.Miktar4;

                                OranToplam1 += item.Oran1;
                                OranToplam2 += item.Oran2;
                                OranToplam3 += item.Oran3;
                                OranToplam4 += item.Oran4;


                                workSheetItem.Cells["A" + rowPointer.ToString()].Value = item.One;
                                workSheetItem.Cells["B" + rowPointer.ToString()].Value = item.Two;
                                workSheetItem.Cells["C" + rowPointer.ToString()].Value = item.Aciklama;
                                workSheetItem.Cells["D" + rowPointer.ToString()].Value = item.Miktar1;
                                workSheetItem.Cells["F" + rowPointer.ToString()].Value = item.Miktar2;
                                workSheetItem.Cells["H" + rowPointer.ToString()].Value = item.Miktar3;
                                workSheetItem.Cells["J" + rowPointer.ToString()].Value = item.Miktar4;
                                workSheetItem.Cells["E" + rowPointer.ToString()].Value = item.Oran1;
                                workSheetItem.Cells["G" + rowPointer.ToString()].Value = item.Oran2;
                                workSheetItem.Cells["I" + rowPointer.ToString()].Value = item.Oran3;
                                workSheetItem.Cells["K" + rowPointer.ToString()].Value = item.Oran4;
                                workSheetItem.Cells["AC" + rowPointer.ToString()].Value = MiktarToplam5;
                                workSheetItem.Cells["AD" + rowPointer.ToString()].Value = OranToplam5;

                                rowPointer++;
                                icerikSayac++;

                            }
                        }


                        workSheetItem.Cells["D33"].Value = MiktarToplam1;
                        workSheetItem.Cells["E33"].Value = OranToplam1;
                        workSheetItem.Cells["F33"].Value = MiktarToplam2;
                        workSheetItem.Cells["G33"].Value = OranToplam2;
                        workSheetItem.Cells["H33"].Value = MiktarToplam3;
                        workSheetItem.Cells["I33"].Value = OranToplam3;
                        workSheetItem.Cells["J33"].Value = MiktarToplam4;
                        workSheetItem.Cells["K33"].Value = OranToplam4;

                    }
                    using (FileStream outStream = new FileStream(ExcelResultPath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        package.Stream.Position = 0;

                        package.SaveAs(outStream);
                        outStream.Position = 0;
                        package.Stream.Dispose();


                        return (outStream);
                    }

                }

            }


            catch (Exception)
            {
                throw;

            }
        }
        public class MiktarToplam
        {
            public decimal Toplam { get; set; }

            public MiktarToplam()
            {
                Toplam = 0;

            }
        }
    }
}

Epplus is an old tool for Excel. Epplus是Excel的旧工具。 If you look a little, you can see lots of better tools. 如果您稍微看一点,就会看到很多更好的工具。 And you should research carefully. 您应该仔细研究。 Additionally, you should look to these tool's websites. 此外,您应该访问这些工具的网站。 When was the last commit or update? 上一次提交或更新的时间是什么? NPOI and GemBox one of the best tools. NPOI和GemBox是最好的工具之一。 Good luck. 祝好运。

Just a few quick questions: 几个简单的问题:

Is it necessary to put the file in the ExcelPackage constructor and then load it(package.Load)? 是否有必要将文件放入ExcelPackage构造函数中,然后加载(package.Load)?

Why do you set package.Stream.Postision = 0 on line 34 and again line 69? 为什么在第34行和第69行上设置package.Stream.Postision = 0?

I am doing this in my solution(wb is the workbook): 我在我的解决方案中这样做(wb是工作簿):

 var ms = new MemoryStream();
 wb.SaveAs(ms);
 ms.Seek(0, SeekOrigin.Begin);

Have you tried saving to file instead (Just to make sure that everything is working). 您是否尝试过保存文件(只是确保一切正常)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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