一、打开Excel
(一)方法一:使用Microsoft.Office.Interop.Excel.dll操作
首先,需要引用一个dll
1 using Excel = Microsoft.Office.Interop.Excel;
然后直接执行如下代码即可:
1 string excelName = "";//你的excel文件的位置 2 string sheetName = "";//你的sheet的名字 3 object missing = Type.Missing; 4 Excel.Application excel = new Excel.Application(); 5 Excel.Workbook book = excel.Workbooks.Open(excelName, missing, 6 missing, missing, missing, missing, missing, missing, missing, 7 missing, missing, missing, missing, missing, missing); 8 Excel.Worksheet sheet = book.Worksheets[sheetName]; 9 excel.Visible = true
最后一定要记得销毁掉excel进程。
(二)方法二:直接调用终端打开
1 string ss = "";//你的excel的路径 2 System.Diagnostics.Process.Start(ss);
二、打开Excel并定位到某个sheet
1 string excelName = "";//你的excel文件的位置 2 string sheetName = "";//你的sheet的名字 3 object missing = Type.Missing; 4 Excel.Application excel = new Excel.Application(); 5 Excel.Workbook book = excel.Workbooks.Open(excelName, missing, 6 missing, missing, missing, missing, missing, missing, missing, 7 missing, missing, missing, missing, missing, missing); 8 Excel.Worksheet sheet = book.Worksheets[sheetName]; 9 sheet.Activate(); 10 excel.Visible = true;
三、打开Excel并定位到某个单元格
1 string excelName = "";//你的excel文件的位置 2 string sheetName = "";//你的sheet的名字 3 string strStart = "A100";//起始单元格 4 string strEnd = "B200";//结束单元格 5 object missing = Type.Missing; 6 Excel.Application excel = new Excel.Application(); 7 Excel.Workbook book = excel.Workbooks.Open(excelName, missing, 8 missing, missing, missing, missing, missing, missing, missing, 9 missing, missing, missing, missing, missing, missing); 10 Excel.Worksheet sheet = book.Worksheets[sheetName]; 11 excel.Application.Goto(sheet.Range[strStart, strEnd], true); 12 excel.Visible = true;
二和三同样需要添加excel的dll