【发布时间】: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