我可以使用记录保存excel文件,但保存后会出现异常。
Application excelApp = new Application();
excelApp.Visible = true;
var workbook = (_Workbook)(excelApp.Workbooks.Add(Type.Missing));
var worksheet = (_Worksheet)workbook.ActiveSheet;
worksheet.Cells[1, 1] = "Firm ID";
worksheet.Cells[1, 2] = "AccountNumber";
worksheet.Cells[1, 3] = "Portfolio Name";
worksheet.Cells[1, 4] = "Currency";
worksheet.Cells[1, 5] = "Amount";
worksheet.Cells[1, 6] = "Value Date";
worksheet.Cells[1, 7] = "Offset Account Type";
worksheet.Cells[1, 8] = "Offset Account Number";
worksheet.Cells[1, 9] = "Description Code";
worksheet.Cells[1, 10] = "Treatment Code";
int row = 1;
var result = DealingContext.PopulateUnitRebateData(unitRebate, Convert.ToDateTime(date)).ToList();
if (result.Count > 0)
{
foreach (var i in result)
{
row++;
worksheet.Cells[row, 1] = i.FirmID;
worksheet.Cells[row, 2] = i.AccountNumber;
worksheet.Cells[row, 3] = i.PortfolioName;
worksheet.Cells[row, 4] = i.Currency;
worksheet.Cells[row, 5] = i.Amount;
worksheet.Cells[row, 6] = i.ValueDate;
worksheet.Cells[row, 7] = i.OffsetAccountType;
worksheet.Cells[row, 8] = i.OffsetAccountNumber;
worksheet.Cells[row, 9] = i.DescriptionCode;
worksheet.Cells[row, 10] = i.TreatmentCode;
}
excelApp.UserControl = true;
excelApp.Save();
}
}
下面是错误堆栈
System.Runtime.InteropServices.COMException (0x800A03EC): Cannot access read-only document 'RESUME.XLW'.
at Microsoft.Office.Interop.Excel.ApplicationClass.Save(Object Filename)
at BestInvest.Admin.DataAccess.Repositories.RebateRepository.CreateFile(Boolean unitRebate, String date) in c:\Projects\Trunk\BestInvest.Admin.DataAccess\Repositories\RebateRepository.cs:line 156
at BestInvest.WebSite.Administration.parameters.RebateFileProcess.btnCreateFile_Click(Object sender, EventArgs e) in c:\Projects\Trunk\BestInvest.WebSite.Administration\parameters\RebateFileProcess.aspx.cs:line 38
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)