我正在尝试使用.Net在excel工作表中写入数据。但是,由于某种原因,当行计数器增加到大约900时,整个应用程序终止(没有例外,没有警告,没有消息)。然而,最多可以有10000个条目。
此外,如果我在填充内容时输入睡眠声明,它也会终止。我怀疑excel对象存在某种超时?
任何想法可能导致这个?感谢
这是主要功能 -
public void ProduceExcelReport(string reportDirPath, List<MedlineRecord> medlineRecords, List<AccessionRecord> accessionRecords,
List<string> dataBanksOfInterest, bool overwrite)
{
Excell.Application excelApp = null;
Excell.Workbooks excelWorkBooks = null;
Excell.Workbook excelWorkBook = null;
Excell.Sheets worksheets = null;
Excell.Worksheet overAllWorkSheet = null;
Excell.Worksheet parsingWorkSheet = null;
Excell.Worksheet errorWorkSheet = null;
Excell.Worksheet noCitationsSheet = null;
Excell.Worksheet outOfScopeWorksheet = null;
try
{
excelApp = new Excell.Application();
string fullReportPath = Path.Combine(reportDirPath, ReportName);
if (File.Exists(fullReportPath))
{
if (!overwrite)
{
Logger.Warning(string.Format("File {0} already exists. Skipping report production." +
"You can change behaviour by editing OverwriteFilesWithNameConflict tag in settings",
fullReportPath));
return;
}
Logger.Warning(string.Format("File {0} already exists. Overwritting old report." +
"You can change behaviour by editing OverwriteFilesWithNameConflict tag in settings",
fullReportPath));
}
// Initialise
excelApp.DisplayAlerts = false;
object misValue = System.Reflection.Missing.Value;
excelWorkBooks = excelApp.Workbooks;
excelWorkBook = excelWorkBooks.Add(misValue);
worksheets = excelWorkBook.Worksheets;
overAllWorkSheet = (Excell.Worksheet) worksheets.Item[1];
parsingWorkSheet = (Excell.Worksheet) worksheets.Item[2];
errorWorkSheet = (Excell.Worksheet) worksheets.Item[3];
noCitationsSheet = (Excell.Worksheet) worksheets.Add();
outOfScopeWorksheet = (Excell.Worksheet)worksheets.Add();
// Filling contents
FillOverallReportContents(ref overAllWorkSheet, accessionRecords);
FillMedlineParsingReportContents(ref parsingWorkSheet, medlineRecords, dataBanksOfInterest); // <- Terminates when writting contents
FillErrorReportContents(ref errorWorkSheet, medlineRecords);
FillNoCitationReportContents(ref noCitationsSheet, medlineRecords);
FillOutOfScopeReportContents(ref outOfScopeWorksheet, medlineRecords, dataBanksOfInterest);
excelWorkBook.SaveAs(fullReportPath);
excelWorkBook.Close(true, misValue, misValue);
excelApp.Quit(); // Quiting excel app
Logger.Info(string.Format("Excel report at {0} was successfully created", fullReportPath));
}
catch (Exception e)
{
Logger.ExceptionError("Exception occured when trying to write Excel Report", e);
}
finally
{
if (overAllWorkSheet != null) Marshal.ReleaseComObject(overAllWorkSheet);
if (parsingWorkSheet != null) Marshal.ReleaseComObject(parsingWorkSheet);
if (noCitationsSheet != null) Marshal.ReleaseComObject(noCitationsSheet);
if (errorWorkSheet != null) Marshal.ReleaseComObject(errorWorkSheet);
if (outOfScopeWorksheet != null) Marshal.ReleaseComObject(outOfScopeWorksheet);
if (worksheets != null) Marshal.ReleaseComObject(worksheets);
if (excelWorkBook != null) Marshal.ReleaseComObject(excelWorkBook);
if (excelWorkBooks != null) Marshal.ReleaseComObject(excelWorkBooks);
if (excelApp != null) Marshal.ReleaseComObject(excelApp);
}
}
以下是填充工作表内容的功能之一
private void FillMedlineParsingReportContents(ref Excell.Worksheet excelWorkSheet, List<MedlineRecord> medlineRecords, List<string> dataBanksOfInterest)
{
// Write the worksheet contents
AddFirstRow(ref excelWorkSheet);
excelWorkSheet.Name = "Medline XML Parsing Summary";
excelWorkSheet.Cells[2, 6] = string.Format("Total articles: {0}", medlineRecords.Count);
excelWorkSheet.Cells[3, 6] = string.Format("Total number dataBank list tags: {0}", (from element in medlineRecords
where element.DataBanksDict.Count > 0
select element).Count());
excelWorkSheet.Cells[4, 6] = string.Format("Total number dataBank tags: {0}", (from element in medlineRecords
select element.DataBanksDict.Count).Sum());
excelWorkSheet.Cells[5, 6] = string.Format("Total number accession tags: {0}", (from element in medlineRecords
select element.GetAccessionRecords().Count).Sum());
int i = 2;
foreach (MedlineRecord record in Utilities.FilterOutMedlineRecords(medlineRecords, dataBanksOfInterest))
{
foreach (AccessionRecord accessionRecord in record.GetAccessionRecords())
{
excelWorkSheet.Cells[i, 1] = accessionRecord.BankName;
excelWorkSheet.Cells[i, 2] = accessionRecord.AccessionId;
excelWorkSheet.Cells[i, 3] = accessionRecord.PMID;
excelWorkSheet.Cells[i, 4] = accessionRecord.FileName;
i++;
// Terminates when I reaches around 800-900
Console.WriteLine(i);
}
}
}