我正在尝试将一些信息写入excel文件。我可以使用EPPlus在其中写入数据。并下载excel文件。但是当我运行并下载此文件并打开时,我看到了这个错误。
" Excel在filename.xls中找到了不可读的内容。你想恢复这个工作簿的内容吗?如果您信任此工作簿的来源,请单击是。"
即使我没有写任何文件,我也收到了这个错误。 我在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;
}
}
}
}
答案 0 :(得分:1)
Epplus是Excel的旧工具。如果你看一点,你可以看到很多更好的工具。你应该仔细研究。此外,您应该查看这些工具的网站。最后一次提交或更新是什么时候? NPOI和GemBox是最好的工具之一。祝你好运。
答案 1 :(得分:0)
只是几个简单的问题:
是否有必要将文件放在ExcelPackage构造函数中然后加载它(package.Load)?
为什么在第34行设置package.Stream.Postision = 0并再次在第69行设置?
我在我的解决方案中这样做(wb是工作簿):
var ms = new MemoryStream();
wb.SaveAs(ms);
ms.Seek(0, SeekOrigin.Begin);
您是否尝试过保存到文件(只是为了确保一切正常)。