我正在使用以下代码使用EPplus将背景图像添加到Excel。文档在Polaris office中可以正常打开,但是在使用Excel时会出现以下错误
[我们在'filename.xlsx'中发现了某些内容的问题。您想尽我们最大的努力来恢复吗
代码
foreach (var file in Filelist)
{
// Load workbook
//var fileInfo = new FileInfo(@file);
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//ExcelPackage pkg = new ExcelPackage(fs);
using (var package = new ExcelPackage(fs))
{
// Itterate through workbook sheets
foreach (var sheet in package.Workbook.Worksheets)
{
sheet.BackgroundImage.Image = bmp;
sheet.Protection.IsProtected = false;
}
package.SaveAs(new FileInfo(Path.GetFileNameWithoutExtension(file)+".xlsx"));
}
fs.Close();
}
我检查了相关的SO问题,有人提到该问题可能是由于多次保存所致。我确保单个文件的循环仅被命中一次。
string fileName = "Image.jpg";
using (Image image = Image.FromFile(HttpContext.Current.Server.MapPath("..\\Media\\Images\\" + fileName))
{
OfficeOpenXml.Drawing.ExcelPicture excelImage = workSheet.Drawings.AddPicture("My Logo", image);
excelImage.SetPosition(0, 0, 0, 0);
}