private void tsbtn_Query_Click(object sender, EventArgs e) { CloseProcess("EXCEL"); //关闭所有Excel进程 string P_str_Excel = tstxt_Excel.Text; //记录Excel文件路径 string P_str_SheetName = tscbox_Sheet.Text; //记录选择的工作表名称 object P_obj_Start = tstxt_Start.Text; //记录开始单元格 object P_obj_End = tstxt_End.Text; //记录结束单元格 object missing = System.Reflection.Missing.Value; //定义object缺省值 Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); //实例化Excel对象 //打开Excel文件 Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Open(P_str_Excel, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); Microsoft.Office.Interop.Excel.Worksheet worksheet; //声明工作表 worksheet = ((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[P_str_SheetName]); //获取选择的工作表 Microsoft.Office.Interop.Excel.Range searchRange = worksheet.get_Range(P_obj_Start, missing); //定义查找范围 switch (tscbox_Type.Text) //按照下拉列表中的文本进行条件判断 { case "数字": //以数字序列填充 searchRange.AutoFill(worksheet.get_Range(P_obj_Start, P_obj_End), Microsoft.Office.Interop.Excel.XlAutoFillType.xlFillSeries); break; case "工作日": //以工作日填充 searchRange.AutoFill(worksheet.get_Range(P_obj_Start, P_obj_End), Microsoft.Office.Interop.Excel.XlAutoFillType.xlFillWeekdays); break; case "月份": //以月份填充 searchRange.AutoFill(worksheet.get_Range(P_obj_Start, P_obj_End), Microsoft.Office.Interop.Excel.XlAutoFillType.xlFillMonths); break; case "年份": //以年份填充 searchRange.AutoFill(worksheet.get_Range(P_obj_Start, P_obj_End), Microsoft.Office.Interop.Excel.XlAutoFillType.xlFillYears); break; default: //以默认方式填充 searchRange.AutoFill(worksheet.get_Range(P_obj_Start, P_obj_End), Microsoft.Office.Interop.Excel.XlAutoFillType.xlFillDefault); break; } excel.DisplayAlerts = false; //设置保存Excel时不显示对话框 workbook.Save(); //保存工作簿 workbook.Close(false, missing, missing); //关闭工作簿 CloseProcess("EXCEL"); //关闭所有Excel进程 WBrowser_Excel.Navigate(P_str_Excel); //在窗体中重新显示Excel文件内容 }