Reputation: 1240
All of a sudden this code has stopped outputting an Excel File. It was converting a DataTable, dtOutput, into an Excel file:
if (dtOutput.Rows.Count > 0)
{
DataView dv = dtOutput.DefaultView;
dv.Sort = "Outage Start Time";
dtOutput = dv.ToTable();
// Populates a column of the table
dtOutput = addIncidentCounts(dtOutput);
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet ws = (Worksheet)wb.Worksheets[1];
ws.Name = "Retail Network Tracking Log";
int col = 1;
foreach (DataColumn column in dtOutput.Columns)
{
ws.Cells[1, col].Value2 = column.ColumnName;
col++;
}
for (int i = 0; i < dtOutput.Rows.Count; i++)
{
for (int j = 0; j < dtOutput.Columns.Count; j++)
{
ws.Cells[i + 2, j + 1].Value2 = dtOutput.Rows[i][j];
}
}
foreach (Range range in ws.UsedRange.Columns)
{
range.HorizontalAlignment = XlHAlign.xlHAlignLeft;
range.Columns.AutoFit();
}
Range rangeE = ws.get_Range("E:E", Type.Missing);
rangeE.NumberFormat = "d-mmm-yyyy hh:mm:ss";
Range rangeF = ws.get_Range("F:F", Type.Missing);
rangeF.NumberFormat = "d-mmm-yyyy hh:mm:ss";
wb.Close(false, false, Type.Missing);
xlApp.Quit();
Marshal.ReleaseComObject(wb);
Marshal.ReleaseComObject(xlApp);
this.Close();
}
It has worked perfectly for weeks. I have made no changes to it. I have restarted VS, rebooted the computer, and finally reinstalled VS but I am still not getting any output. I have run it through the debugger as well and the code is definitely executing and attaining all of the proper values. I'm stumped.
Upvotes: 0
Views: 98
Reputation: 1240
Hans Passant was right. The call to close needed to be:
wb.Close(true, saveDirectory + "\\" + reportName, false);
Now the real mystery is how this code was working in the first place!
Upvotes: 1