我想知道打开文件时是否可以使用C#/ EPPlus写入Excel文件。尝试使用程序编写程序时,我继续遇到异常,但是我找不到任何在线内容。
这是我必须附加到现有工作表上的代码,当未打开电子表格时,它可以正常工作
public static void AppendExistingMailingWorkbook(string workSheet, string filePath, IList<MailingReportItem> reportData)
{
//create a fileinfo object of an excel file on the disk
FileInfo file = new FileInfo(filePath);
Object thisLock = new Object();
lock (thisLock)
{
//create a new Excel package from the file
using (ExcelPackage excelPackage = new ExcelPackage(file))
{
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[workSheet];
var rowToAppend = worksheet.Dimension.End.Row + 1;
for (int i = 0; i < reportData.Count; i++, rowToAppend++)
{
worksheet.Cells[rowToAppend, 1].Value = reportData[i].BatchDate.Date.ToString("MM/dd/yyyy");
worksheet.Cells[rowToAppend, 2].Value = reportData[i].BatchId;
worksheet.Cells[rowToAppend, 3].Value = reportData[i].FileName;
worksheet.Cells[rowToAppend, 4].Value = reportData[i].PageCount;
worksheet.Cells[rowToAppend, 5].Value = reportData[i].MailDate;
}
//save the changes
excelPackage.Save();
}
}
}
答案 0 :(得分:2)
在Excel中,设置要共享的工作簿。来自office help:
- Shared
将显示在Excel窗口顶部文件名旁边。然后将以非独占方式打开文件,允许其他人在Excel打开时对其进行编辑。