/// <summary> /// 向文本框写入数据,对指定WorkSheet操作 /// </summary> /// <param name="ht">Hashtable的键值对保存文本框的ID和数据</param> public void SetTextBoxes(int sheetIndex, Hashtable ht) { if (ht.Count == 0) return; if (sheetIndex > this.WorkSheetCount) { this.KillExcelProcess(); throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!"); } workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex); foreach (DictionaryEntry dic in ht) { try { textBox = (Excel.TextBox)workSheet.TextBoxes(dic.Key); textBox.Text = dic.Value.ToString(); } catch { this.KillExcelProcess(); throw new Exception("不存在ID为\"" + dic.Key.ToString() + "\"的文本框!"); } } }
/// <summary> /// 向文本框写入数据,对每个WorkSheet操作 /// </summary> /// <param name="ht">Hashtable的键值对保存文本框的ID和数据</param> public void SetTextBoxes(Hashtable ht) { if (ht.Count == 0) return; for (int i = 1; i <= this.WorkSheetCount; i++) { workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i); foreach (DictionaryEntry dic in ht) { try { textBox = (Excel.TextBox)workSheet.TextBoxes(dic.Key); textBox.Text = dic.Value.ToString(); } catch { this.KillExcelProcess(); throw new Exception("不存在ID为\"" + dic.Key.ToString() + "\"的文本框!"); } } } }
/// <summary> /// 向指定文本框写入数据,对每个WorkSheet操作 /// </summary> /// <param name="textboxName">文本框名称</param> /// <param name="text">要写入的文本</param> public void SetTextBox(string textboxName, string text) { for (int i = 1; i <= this.WorkSheetCount; i++) { workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i); try { textBox = (Excel.TextBox)workSheet.TextBoxes(textboxName); textBox.Text = text; } catch { this.KillExcelProcess(); throw new Exception("不存在ID为\"" + textboxName + "\"的文本框!"); } } }
/// <summary> /// 向指定文本框写入数据,对指定WorkSheet操作 /// </summary> /// <param name="sheetIndex">工作表索引</param> /// <param name="textboxName">文本框名称</param> /// <param name="text">要写入的文本</param> public void SetTextBox(int sheetIndex, string textboxName, string text) { workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex); try { textBox = (Excel.TextBox)workSheet.TextBoxes(textboxName); textBox.Text = text; } catch { this.KillExcelProcess(); throw new Exception("不存在ID为\"" + textboxName + "\"的文本框!"); } }
private void Dispose() { workBook.Close(null, null, null); app.Workbooks.Close(); app.Quit(); if (range != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(range); range = null; } if (range1 != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(range1); range1 = null; } if (range2 != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(range2); range2 = null; } if (textBox != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(textBox); textBox = null; } if (workSheet != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet); workSheet = null; } if (workBook != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook); workBook = null; } if (app != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(app); app = null; } GC.Collect(); this.KillExcelProcess(); }//end Dispose