我正在编写一个winform应用程序,它从sql数据库中读取数据,将其粘贴到excel中,然后将文件保存在给定的位置。
我目前的问题是遇到系统错误:保存excel文件时为0x 800 AC 472。
到目前为止,我已经尝试添加
GC.Collect();
GC.WaitForPendingFinalizers();
调用Excel保存函数后,将其添加到单击方法。
从我的谷歌搜索看,这似乎可能是由于COM对象在使用后没有被删除?这些GC方法旨在清除这一点,但它似乎没有正常工作。我认为它没有正常工作的原因是,在执行代码并收到错误后,仍有一个microsoftoffice.exe进程在任务管理器中运行。
值得一提的是,如果我将代码 Package 在try-catch中,错误将粘贴到控制台,Excel文件将保存,没有任何问题,唯一的问题是进程仍在任务管理器中运行。
这里有人能帮我找出代码中导致这个错误的问题吗?谢谢。
下面是按下按钮时调用的代码;
else if (((DataGridView)sender).Columns[e.ColumnIndex].DataPropertyName == "Run")
{
// return SQL into datatable
var returnedDT = SQLAcess.SQLtoDataTable(dataGridView1[0, e.RowIndex].Value.ToString()!);
//find item to open
string loadstring = DataGridClass.CellColumn(dataGridView1, "Load_Location", e.RowIndex);
//finds workbook to paste into.
var SettingsDataset = XMLData.ReturnXMLDataset(2);
var workbookstring = XMLData.returnXMLcellwithcolumnname(SettingsDataset, "Data_Dump_Worksheet_name", e.RowIndex);
//find location to save it
string savestring = DataGridClass.CellColumn(dataGridView1, "Save_location", e.RowIndex);
//execute export to excel, with the locations saved from above.
GXOMIClassLibrary.My_DataTable_Extensions.ExportToExcelDetailed(returnedDT, loadstring, workbookstring, savestring);
GC.Collect();
GC.WaitForPendingFinalizers();
}
GXOMIClassLibrary.My_DataTable_Extensions.ExportToExcelDetailed(returnedDT, loadstring, workbookstring, savestring);
方法引用了我用下面的方法创建的类库;
public static void ExportToExcelDetailed(this System.Data.DataTable DataTable, string ExcelLoadPath, string WorksheetName, string ExcelSavePath)
{
try
{
int ColumnsCount;
//if datatable is empty throw an exception.
if (DataTable == null || (ColumnsCount = DataTable.Columns.Count) == 0)
throw new Exception("ExportToExcel: Null or empty input table!\n");
// load excel, and create a new workbook
//Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
//Excel.Workbooks.Add();
var excelApp = new Excel.Application();
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(ExcelLoadPath);
//TELL THE PROGRAM WHAT WORKBOOK TO OPEN
// select the right worksheet.
var Worksheet = excelWorkbook.Sheets[WorksheetName];
// DataCells
int RowsCount = DataTable.Rows.Count;
object[,] Cells = new object[RowsCount, ColumnsCount];
for (int j = 0; j < RowsCount; j++)
for (int i = 0; i < ColumnsCount; i++)
Cells[j, i] = DataTable.Rows[j][i];
//find last row
var xlRange = (Excel.Range)Worksheet.Cells[Worksheet.Rows.Count, 1];
long lastRow = (long)xlRange.get_End(Excel.XlDirection.xlUp).Row;
long newRow = lastRow + 1;
///cells[2,1] needs to become cell below last paste
Worksheet.Range((Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[newRow, 1]), (Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[newRow + RowsCount -1, ColumnsCount])).Value = Cells;
// check fielpath
if (ExcelSavePath != null && ExcelSavePath != "")
{
try
{
Worksheet.SaveAs(ExcelSavePath);
excelApp.Quit();
// Worksheet.Close(0);
//richTextBox1("Excel file saved!");
}
catch (Exception ex)
{
throw new Exception("ExportToExcel: Excel file could not be saved! Check filepath.\n"
+ ex.Message);
excelApp.Quit();
}
}
else // no filepath is given
{
excelApp.Visible = true;
}
excelApp.Quit();
}
catch (Exception ex)
{
throw new Exception("ExportToExcel: \n" + ex.Message);
}
}
1条答案
按热度按时间u4dcyp6a1#
更改ExportToExcelDetailed的这一部分似乎已更正了此问题;