我正在撰寫一個使用“Excel 表格”的程式。
我正在通過一個按鈕將它們加載到DataGridView 中。如果我完成了Sheet 的作業,我可以按相同的按鈕來加載另一個Sheet。問題是即使我正在清除和重繪 我的DataGridView舊作業表并沒有消失,新作業表只是添加到舊作業表之下。這是為什么?
我的按鈕單擊事件代碼:
private void button1_Click(object sender, EventArgs e)
{
//Excell Prozess kill (wenn "Datei ?ffnen" geklickt)
Process[] excelProcs = Process.GetProcessesByName("EXCEL");
foreach (Process proc in excelProcs)
{
proc.Kill();
}
//Preparing Excel
Excel.Workbook theWorkbook = null;
Excel.Application ExcelObj = null;
Excel.Sheets sheets = null;
Excel.Worksheet worksheet = null;
if (dataGridView1.DataSource != null)
{
dataGridView1.DataSource = null;
dataGridView1.Refresh();
}
else
{
dataGridView1.Columns.Clear();
}
//Deklaration
string filePath = string.Empty;
string fileExt = string.Empty;
//File dialog
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Excel Datei (*.xlsx *.xls)|*.xlsx; *.xls; |All Files (*.*)|*.*";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
filePath = dialog.FileName; //get the path of the file
fileExt = Path.GetExtension(filePath); //get the extension from the file
//File Path zeigen
label1.Text = filePath;
//Nur .xls und .xlsx
if (fileExt.CompareTo(".xls") == 0 || fileExt.CompareTo(".xlsx") == 0)
{
try
{
ExcelObj = new Excel.Application();
//open ExcelObj and load it into theWorkbook
theWorkbook = ExcelObj.Workbooks.Open(filePath);
sheets = theWorkbook.Worksheets;
//Get the reference of second worksheet
worksheet = (Excel.Worksheet)sheets.get_Item(1);
//Get the name of worksheet
string strWorksheetName = worksheet.Name;
//string for database con
String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
filePath
";Extended Properties='Excel 12.0 XML;HDR=NO;';";
OleDbConnection con = new OleDbConnection(constr);
//select sheet from excel file
OleDbCommand oconn = new OleDbCommand("Select * From [" strWorksheetName "$]", con);
//open db
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
sda.Fill(data);
//insert data into datagridview
dataGridView1.DataSource = data;
//Select all from table date
DataRow[] rows = data.Select();
//close db
con.Close();
//close ExcelObj
ExcelObj.Quit();
}
//if error -> show
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
//ComObject release
if (sheets != null) Marshal.ReleaseComObject(sheets);
if (ExcelObj != null) Marshal.ReleaseComObject(ExcelObj);
if (worksheet != null) Marshal.ReleaseComObject(worksheet);
if (theWorkbook != null) Marshal.ReleaseComObject(theWorkbook);
}
我相信問題是由于“Excel 檔案”在“準備 Excel”部分中沒有正確釋放造成的,但是在許多情況下,“Excel”是通過“ Excel Prozess Kill ”代碼關閉的。我通過任務管理器看了幾次。那么是什么導致了這個問題呢?任何意見?
uj5u.com熱心網友回復:
在您當前的代碼中有一些令人頭疼的事情,您可能需要重新考慮。
首先……殺死所有“Excel”行程很好……奇怪。您的代碼可能沒有啟動這些行程;但是代碼會殺死他們。與開始運行 Excel 并離開去喝咖啡的人見鬼去吧。
代碼“似乎”正確地釋放了 Excel 行程,但是,“在”釋放作業簿之后釋放 Excel 應用程式似乎是合乎邏輯的。具體來說,釋放sheets,然后worksheet,然后theWorkbook和最后 應用程式ExcelObj。因為代碼“似乎”正確釋放了 Excel com 物件,所以我會轉儲所有殺死 Excel 行程的代碼。
接下來,我建議在處理 Excel 檔案時尋找第三方解決方案。對于非商業用途,我發現 EPPlus 是一個很好的替代品,而不是OleDb和/或Interop可以在 NuGet 中找到。只是一個想法。
最后,我確實對您的問題有一個簡單的解決方案。由于您沒有顯示其定義,因此我可以破譯的是變數data. 在我看來,這個data物件是一個DataTable. 在我的小型測驗中,正如您所描述的,當您選擇第二個 Excel 檔案時,它確實將自身“附加”到現有的data DataTable. 由于代碼“似乎”data在填充表格之前永遠不會清除表格,那么當您呼叫時......
sda.Fill(data);
第二次,然后配接器樂于“附加”資料。
要解決這個問題,只需data在填寫表格之前清除表格......
data = new DataTable();
sda.Fill(data);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/331475.html
上一篇:觸發器不會在vba中按預期更新
