最近項目上有導入excel的需求,其實導入一個固定格式的excel數據非常容易,但是,發現一個問題就是,導入excel后,用戶在打開excel時,必須要打開2次才能打開excel,這讓用戶很不爽;開始查找原因:excel的進程關閉不了,資源不能釋放,於是就想盡辦法釋放com對象excel實例,在博客園就找到一篇文章,所以就做個學習筆記了。
public class ExcelHelper
{
#region 成員變量
private string templetFile = null;
private string outputFile = null;
private object missing = Missing.Value;
private DateTime beforeTime; //Excel啟動之前時間
private DateTime afterTime; //Excel啟動之后時間
Microsoft.Office.Interop.Excel.Application app;
Microsoft.Office.Interop.Excel.Workbook workBook;
Microsoft.Office.Interop.Excel.Worksheet workSheet;
Microsoft.Office.Interop.Excel.Range range;
Microsoft.Office.Interop.Excel.Range range1;
Microsoft.Office.Interop.Excel.Range range2;
Microsoft.Office.Interop.Excel.TextBox textBox;
private int sheetCount = 1; //WorkSheet數量
private string sheetPrefixName = "頁";
#endregion
#region 公共屬性
/// <summary>
/// WorkSheet前綴名,比如:前綴名為“頁”,那么WorkSheet名稱依次為“頁-1,頁-2...”
/// </summary>
public string SheetPrefixName
{
set { this.sheetPrefixName = value; }
}
/// <summary>
/// WorkSheet數量
/// </summary>
public int WorkSheetCount
{
get { return workBook.Sheets.Count; }
}
/// <summary>
/// Microsoft.Office.Interop.Excel模板文件路徑
/// </summary>
public string TempletFilePath
{
set { this.templetFile = value; }
}
/// <summary>
/// 輸出Excel文件路徑
/// </summary>
public string OutputFilePath
{
set { this.outputFile = value; }
}
#endregion
public static void MakeExcelDocs(string sourcepath, string destinpath, string newname)
{
destinpath = destinpath + "\\" + newname + ".xls";
// Ensure that the target does not exist.
File.Delete(destinpath);
// Copy the file.
File.Copy(sourcepath, destinpath);
// Try to copy the same file again, which should succeed.
File.Copy(sourcepath, destinpath, true);
}
public static void OpenExcelDocs(string filename,string[] content)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); //引用Excel對象
Microsoft.Office.Interop.Excel.Workbook book = excel.Workbooks.Open(filename,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing
,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing); //引用Excel工作簿
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.Sheets.get_Item(1); ; //引用Excel工作頁面
excel.Visible = false;
string[] newcontent = StringHelper.Modify(content);
sheet.Cells[10, 3] = newcontent[0];
sheet.Cells[11, 3] = newcontent[1];
sheet.Cells[12, 3] = newcontent[2];
sheet.Cells[13, 3] = newcontent[3];
sheet.Cells[14, 3] = newcontent[4];
sheet.Cells[15, 3] = newcontent[5];
sheet.Cells[16, 3] = newcontent[6];
sheet.Cells[17, 3] = newcontent[7];
book.Save();
book.Close(Type.Missing, Type.Missing, Type.Missing);
excel.Quit();
IntPtr t = new IntPtr(excel.Hwnd);
int k = 0;
GetWindowThreadProcessId(t, out k);
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
//sheet = null;
//book = null;
//excel = null
//System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
//System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
//System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
}
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
public static void OpenExcelDocs2(string filename, double[] content)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); //引用Excel對象
Microsoft.Office.Interop.Excel.Workbook book = excel.Workbooks.Open(filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing
, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); //引用Excel工作簿
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.Sheets.get_Item(1); ; //引用Excel工作頁面
excel.Visible = false;
sheet.Cells[24, 3] = content[1];
sheet.Cells[25, 3] = content[0];
book.Save();
book.Close(Type.Missing, Type.Missing, Type.Missing);
excel.Quit(); //應用程序推出,但是進程還在運行
IntPtr t = new IntPtr(excel.Hwnd); //殺死進程的好方法,很有效
int k = 0;
GetWindowThreadProcessId(t, out k);
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
//sheet = null;
//book = null;
//excel = null; //不能殺死進程
//System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet); //可以釋放對象,但是不能殺死進程
//System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
//System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
}
}
原文引自:http://www.cnblogs.com/zhangjun1130/archive/2010/12/06/1897717.html
下面是我自己用到的代碼,記下來以備后用。
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); //引用Excel對象
Microsoft.Office.Interop.Excel.Workbook book = excel.Workbooks.Open(textBox1.Text.Trim(), Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing
, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); //引用Excel工作簿
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.Sheets.get_Item(1); ; //引用Excel工作頁面
excel.Visible = false;
//讀取excel內容代碼部分
book.Save();
book.Close(Type.Missing, Type.Missing, Type.Missing);
excel.Quit();
IntPtr t = new IntPtr(excel.Hwnd);
int k = 0;
GetWindowThreadProcessId(t, out k);
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
而且 在上述代碼的方法上面,需要導入win32組件,代碼如下:
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
