我有一个应用程序,其中将打开按钮单击 excel 表并将数据插入其中。如果我在插入数据之前关闭 excel 工作表,则会发生
异常。来自 HRESULT 的异常:0x800401A8
有什么办法可以处理这个。我的代码是
Excel.Application objApp = null;
Excel._Workbook objBook = null;
Excel.Workbooks objBooks = null;
Excel.Worksheet workSheet = null;
objApp = new Excel.Application();
objBooks = objApp.Workbooks;
objBook = objBooks.Open(path, 2, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", true, false, 0, true);
objBook.Activate();
objApp.Visible = true;
workSheet = (Excel.Worksheet)objBook.Worksheets[1];
//sqlQueryForShoppingCenter.Append(" select shp_ctr_id,ctr_name,addr_city,addr_line1 from shp_ctr where shp_ctr_id='SCL00002' ");
sqlQueryForShoppingCenter.Append(" select Shp_ctr_id,Ctr_Name,addr_city,Addr_line1 from shp_ctr where shp_ctr_id='68729'");
using (OracleDataReader reader1 = SqlStatementUtil.ExecuteReader(sqlQueryForShoppingCenter.ToString()))
{
while (reader1.Read())
{
if (workSheet != null)
{
workSheet.Cells[4, 6] = reader1["Shp_ctr_id"].ToString();
workSheet.Cells[5, 6] = reader1["Ctr_Name"].ToString();
workSheet.Cells[6, 6] = reader1["addr_city"].ToString();
workSheet.Cells[7, 6] = reader1["Addr_line1"].ToString();
}
}
}
如果 Excel 没有关闭,数据将被成功插入。但是如果在插入数据之前关闭了excel,我想跳过插入如何处理它......
我试图通过设置各种条件来处理这个问题。请帮忙