Example #1
0
        private void tsbtn_ASCSort_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, P_obj_End); //定义查找范围
            switch (searchRange.Columns.Count)
            {
            case 1:
                //按1列升序排序
                searchRange.Sort(
                    searchRange.Columns[1, missing], Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
                    missing, missing, Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
                    missing, Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
                    Microsoft.Office.Interop.Excel.XlYesNoGuess.xlNo, missing, missing,
                    Microsoft.Office.Interop.Excel.XlSortOrientation.xlSortColumns,
                    Microsoft.Office.Interop.Excel.XlSortMethod.xlPinYin,
                    Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal,
                    Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal,
                    Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal);
                break;

            case 2:
                //按2列升序排序
                searchRange.Sort(
                    searchRange.Columns[1, missing], Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
                    searchRange.Columns[2, missing], missing, Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
                    missing, Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
                    Microsoft.Office.Interop.Excel.XlYesNoGuess.xlNo, missing, missing,
                    Microsoft.Office.Interop.Excel.XlSortOrientation.xlSortColumns,
                    Microsoft.Office.Interop.Excel.XlSortMethod.xlPinYin,
                    Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal,
                    Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal,
                    Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal);
                break;

            case 3:
            default:
                //按3列升序排序
                searchRange.Sort(
                    searchRange.Columns[1, missing], Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
                    searchRange.Columns[2, missing], missing, Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
                    searchRange.Columns[3, missing], Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
                    Microsoft.Office.Interop.Excel.XlYesNoGuess.xlNo, missing, missing,
                    Microsoft.Office.Interop.Excel.XlSortOrientation.xlSortColumns,
                    Microsoft.Office.Interop.Excel.XlSortMethod.xlPinYin,
                    Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal,
                    Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal,
                    Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal);
                break;
            }
            excel.DisplayAlerts = false;          //设置保存Excel时不显示对话框
            workbook.Save();                      //保存工作表
            CloseProcess("EXCEL");                //关闭所有Excel进程
            WBrowser_Excel.Navigate(P_str_Excel); //在窗体中重新显示Excel文件内容
        }