0

突然之间,这段代码停止了输出 Excel 文件。它正在将 DataTable dtOutput 转换为 Excel 文件:

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();
}

它已经完美运行了数周。我没有对其进行任何更改。我重新启动了VS,重新启动了计算机,最后重新安装了VS,但我仍然没有得到任何输出。我也通过调试器运行了它,代码肯定正在执行并获得所有正确的值。我难住了。

4

1 回答 1

1

汉斯·帕桑特是对的。关闭电话需要是:

wb.Close(true, saveDirectory + "\\" + reportName, false);

现在真正的谜团是这段代码最初是如何工作的!

于 2012-08-02T23:32:58.610 回答