【问题标题】:Error message when trying to save my created Excel file in c#尝试在 C# 中保存我创建的 Excel 文件时出现错误消息
【发布时间】:2019-10-01 17:00:16
【问题描述】:

我正在尝试使用 Interop 在 C# 中创建一个 Excel 文件。我已经创建了基本格式,正准备从我的 SQL 数据库中提取数据,但首先要确保它可以正常工作。

每次我尝试保存文件时,我都会得到一般的

HRESULT 异常:0x800A03EC

消息。我已经广泛搜索了一个解决方案,并尝试了我能做的一切,但我似乎无法让它发挥作用。当它到达 SaveAs 行时,它会给我一条错误消息。

下面是我创建 Excel 文件的 void 函数的代码:

public static void createFile()
{
    //Figures out how many days are in the month
    string month = File.ReadAllText(@"C:\Users\noahc\Documents\TimesheetInvoiceMonth\month.txt", Encoding.UTF8);
    string year = DateTime.Now.Year.ToString();
    int intYear = Convert.ToInt32(year);
    int columns;

    if (month == "September" || month == "April" || month == "June" || month == "November")
    {
        columns = 34;
    }
    else if (month == "January" || month == "March" || month == "May" || month == "July" || month == "August"
        || month == "October" || month == "December")
    {
        columns = 35;
    }
    else if (DateTime.IsLeapYear(intYear))
        columns = 33;
    else
        columns = 32;

    //Create excel application
    Excel._Application xlApp = new Excel.Application();
    Excel.Workbooks xlWorkbooks = xlApp.Workbooks;
    Excel.Workbook xlWorkbook = xlWorkbooks.Add(Type.Missing);
    Excel.Worksheet xlSheet = xlWorkbook.ActiveSheet;
    xlApp.Visible = true;

    xlApp.StandardFont = "Arial Narrow";
    xlApp.StandardFontSize = 10;

    //Format top row
    xlSheet.Cells[1, "A"] = "monthly time sheet | lee calisti";
    xlSheet.Cells[1, "A"].Font.Size = 14;
    xlSheet.Cells[1, "A"].Font.Color = Color.White;

    //colNames is an array of column names
    string[] colNames = new string[columns];

    //Entering column names into array
    colNames[0] = month + " " + year;
    colNames[2] = "date";

    for (int i = 3; i < columns - 1; i++)
    {
        colNames[i] = (i - 2).ToString();
        xlSheet.Columns[i + 1].ColumnWidth = 4.25;
    }

    colNames[columns - 1] = "Project";
    string lastColumn;
    switch (columns)
    {
        case 32:
            lastColumn = "AF";
            break;
        case 33:
            lastColumn = "AG";
            break;
        case 34:
            lastColumn = "AH";
            break;
        case 35:
            lastColumn = "AI";
            break;
        default:
            lastColumn = "A";
            MessageBox.Show("Something is wrong...");
            break;
    }
    //Back color of first row
    xlSheet.get_Range("A1", lastColumn + "1").Interior.Color = Color.FromArgb(197, 90, 17);

    //Date on last column of first row
    DateTime thisDay = DateTime.Today;
    xlSheet.Cells[1, lastColumn] = thisDay.ToString("d");
    xlSheet.Cells[1, lastColumn].Font.Color = Color.White;

    //Second row
    xlSheet.get_Range("A2", lastColumn + "2").Value2 = colNames;
    xlSheet.get_Range("A2", "C2").Font.Bold = true;
    xlSheet.get_Range("B2", lastColumn + "2").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
    xlSheet.Columns[1].AutoFit();

    //Third row
    xlSheet.Cells[3, lastColumn] = "Total Hours";

    //Project name, phase, and number headings
    xlSheet.Cells[4, "C"] = "project";
    xlSheet.Cells[4, "C"].Font.Bold = true;
    xlSheet.Cells[5, "A"] = "project name";
    xlSheet.Cells[5, "B"] = "phase";
    xlSheet.Cells[5, "C"] = "number";
    xlSheet.get_Range("A5", "C5").Font.Bold = true;
    xlSheet.get_Range("B4", "C5").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
    xlSheet.Columns[columns].AutoFit();

    // Save the excel file at specified location
    xlWorkbook.SaveAs(@"C:\Users\noahc\OneDrive\Documents\NOAH\TestSave\test.xlsx");

    //Cleanup
    xlWorkbook.Close(true);
    xlApp.Quit();
    GC.Collect();
    GC.WaitForPendingFinalizers();
    Marshal.FinalReleaseComObject(xlApp);
    Marshal.FinalReleaseComObject(xlWorkbooks);
    Marshal.FinalReleaseComObject(xlWorkbook);
    Marshal.FinalReleaseComObject(xlSheet);
}

【问题讨论】:

  • 我没有立即看到任何东西。要解决此问题,请尝试使用xlApp.StandardFont = "Arial Narrow";xlSheet.Columns[columns].AutoFit(); 注释掉所有staritng 并查看是否运行。然后从几行中删除 cmets 并再次运行,直到隔离触发错误的部分。另外,请注意 COM 对象应该以它们被实例化的相反顺序释放 - IOW 在最后反转该列表。例如,xlSheet 取决于它上面的所有内容。以错误的顺序发布可能会使事情“悬而未决”。

标签: c# excel office-interop com-interop excel-interop


【解决方案1】:

就我而言,我遇到了“Arial Narrow”字体的问题。这不仅导致保存文件出现问题,而且导致我无法打开 任何 Excel 文件(因为运行此脚本会更改 Excel 的默认字体 - 而不仅仅是此工作簿的字体)!

这可能会导致特别难以调试问题,因为您不能只是注释掉这些行并重试,因为您实际上已经更改了已安装 Excel 应用程序的状态。

最终对我有用的是进入 Excel 选项,并将默认字体改回 Arial。然后,按照建议,注释掉尽可能多的行,向后工作,直到您隔离了问题。

再次,我必须添加的具体说明是,当您更改 xlApp 上的属性时,您会在应用程序级别创建副作用。确保您已恢复默认设置,然后继续调试您的脚本。

【讨论】:

  • 这很奇怪。我会试一试,但我还要说我已经能够在其他文件上完美地使用 Excel。这个程序也会完美地创建文件,它只是在尝试保存时抛出一个错误。
猜你喜欢
  • 2018-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-07
  • 1970-01-01
  • 2017-12-10
  • 2016-09-26
  • 2017-10-02
相关资源
最近更新 更多