我的 sqldatareader 正在返回约 200 行数据。这些行中大约有一半有一列包含整个 xml 文档。我假设此列导致 autofit() 方法挂起。我怎样才能让它要么抛出异常,要么成功完成。我不在乎哪一个,我只需要这个[自动化]程序来完成。
Excel.Application xl = null;
Excel._Workbook wb;
Excel._Worksheet ws;
try
{
xl = new Microsoft.Office.Interop.Excel.Application();
xl.Visible = false;
while (reader.HasRows && !done)
{
wb = (Excel._Workbook)(xl.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet));
ws = (Excel._Worksheet)wb.ActiveSheet;
// Insert column headers
for (int counter = 0; counter < reader.FieldCount; counter++)
ws.Cells[1, counter + 1] = reader.GetName(counter);
// Write the data to the excel file
while (reader.Read())
{
for (int counter = 1; counter <= reader.FieldCount; counter++)
{
// Need to format this column so excel doesn't change how it looks
if (report.ReportName == "RPTAAMVANetBatch" && reader.GetName(counter - 1) == "CorrelationID")
ws.Cells[rowCounter, counter] = String.Format("''{0}'", reader.GetValue(counter - 1));
else
ws.Cells[rowCounter, counter] = reader.GetValue(counter - 1);
}
rowCounter++;
}
RecordsProcessed = rowCounter - 1;
// Format the excel file to liking
ws.get_Range(ws.Cells[1, 1], ws.Cells[rowCounter - 1, reader.FieldCount]);
ws.Columns.AutoFit();