Пример #1
0
        public void CreateNewSheet()
        {
            var    todayDate      = DateTime.Now.ToString("MMDD");
            string dataSourcePath = this.ExecutionContext.DeploymentDirectory + @"\Data\PerformanceTestData1007.xls";
            var    buildnum       = Utility.currentBuild;
            var    filename       = todayDate + "_PerformanceTestData1007.xls";
            String myPath         = "C:\\MatrixTestReport\\" + filename;

            Utility.filepath = myPath;
            var column = Utility.column;
            var row    = Utility.row;

            if (!System.IO.File.Exists(myPath))
            {
                System.IO.File.Copy(dataSourcePath, myPath);
            }


            todayDate = DateTime.Now.ToString("MMdd");

            var sheetName = Utility.plan + "_" + buildnum + "_" + Utility.currentDomain + "_" + todayDate;

            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook    workbook = excelApp.Workbooks.Open(myPath);
            var plan = Utility.plan;

            Microsoft.Office.Interop.Excel._Worksheet xlWorksheet2 = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
            if (plan == "iep")
            {
                xlWorksheet2 = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
            }

            else if (plan == "pssp")
            {
                xlWorksheet2 = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[2];
            }

            else if (plan == "ifsp")
            {
                xlWorksheet2 = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[3];
            }

            xlWorksheet2.Copy(Before: workbook.Sheets[1]);
            Microsoft.Office.Interop.Excel._Worksheet xlWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
            xlWorksheet.Name = sheetName;

            excelApp.Visible = true;
            excelApp.ActiveWorkbook.Save();

            workbook.Close(false, Type.Missing, Type.Missing);
            excelApp.Workbooks.Close();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);

            excelApp.Quit();
            GC.Collect();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
        }
Пример #2
0
        public static void ExportExcel_CTCC(DataTable dt, DataTable dtpb, int pos_row, string pos_column, bool IsOpen, string sTemplateFile, string sfilename, bool Show_Column)
        {
            Microsoft.Office.Interop.Excel.Application ReportFile = new Microsoft.Office.Interop.Excel.Application();
            ReportFile.Visible = false;
            Microsoft.Office.Interop.Excel._Workbook WorkBook;
            try
            {
                if (sfilename != "")
                {
                    if (File.Exists(sfilename))
                    {
                        //// Configure message box
                        //string message = "File đã tồn tại.Bạn muốn lưu chồng lên nó?";
                        //string caption = "Thông Báo";
                        //MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                        //MessageBoxIcon icon = MessageBoxIcon.Question;
                        //// Show message box
                        //if (MessageBox.Show(message, caption, buttons, icon, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                        //{
                        File.Copy(sTemplateFile, sfilename, true);
                        //}
                    }
                    else
                    {
                        File.Copy(sTemplateFile, sfilename);
                    }
                    WorkBook = (Microsoft.Office.Interop.Excel._Workbook)(ReportFile.Workbooks.Open(sfilename,
                                                                                                    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));
                }
                else
                {
                    WorkBook = (Microsoft.Office.Interop.Excel._Workbook)(ReportFile.Workbooks.Add(sTemplateFile));
                }
                Microsoft.Office.Interop.Excel._Worksheet Sheet = (Microsoft.Office.Interop.Excel._Worksheet)WorkBook.ActiveSheet;
                // xu ly xuat theo phong ban
                // begin for phong ban
                DataView dtView = new DataView(dt);
                for (int ii = 0; ii < dtpb.Rows.Count; ii++)
                {
                    dtView.RowFilter = "DepartmentName = '" + dtpb.Rows[ii]["DepartmentName"].ToString() + "'";

                    //  Microsoft.Office.Interop.Excel._Worksheet Sheet = (Microsoft.Office.Interop.Excel._Worksheet)WorkBook.Sheets;

                    //Tinh so luong cot xuat ra
                    string MaxColumn = ((String)(Convert.ToChar(dt.Columns.Count / 26 + 64).ToString() + Convert.ToChar(dt.Columns.Count % 26 + 64))).Replace('@', ' ').Trim();
                    if (dt.Columns.Count % 26 == 0)
                    {
                        MaxColumn = ((String)(Convert.ToChar((dt.Columns.Count) / 26 + 64).ToString() + Convert.ToChar((dt.Columns.Count + 1) % 26 + 64))).Replace('@', ' ').Trim();
                    }
                    //cac tham so
                    int record_count = dtView.Count;
                    int record_mod   = record_count;

                    string pos_begin1 = pos_column;
                    int    pos_begin2 = pos_row;



                    #region move footer
                    //int Buffer_Size = 100;
                    ////move footer
                    //Microsoft.Office.Interop.Excel.Range srcRange = Sheet.get_Range("A" + (pos_row + 1).ToString(), "AZ" + (pos_row + Buffer_Size + 1).ToString());
                    //srcRange.Copy(Type.Missing);

                    //string Cell21 = "A" + (record_count + pos_row + 1 + Buffer_Size).ToString();
                    //string Cell22 = "AZ" + (record_count + pos_row + 2 * Buffer_Size).ToString();

                    //Microsoft.Office.Interop.Excel.Range destRange = Sheet.get_Range(Cell21, Cell22);

                    //Sheet.Paste(destRange, Type.Missing);

                    //srcRange.Rows.Delete(Microsoft.Office.Interop.Excel.XlDeleteShiftDirection.xlShiftUp);

                    //end move footer
                    #endregion

                    //Xuat cot mac dinh trong table neu Show_Column == true
                    if (Show_Column == true)
                    {
                        CreateColumnName(dt, Sheet, pos_row);
                        pos_begin2 = pos_row + 1;
                    }
                    object[,] Array_Record;
                    string Cell_Begin, Cell_End;

                    Cell_Begin = pos_begin1 + (pos_begin2).ToString();
                    Cell_End   = MaxColumn + (record_count + pos_row - 1).ToString();
                    if (Show_Column == true)
                    {
                        Cell_End = MaxColumn + (record_mod + pos_row).ToString();
                    }
                    Array_Record = new object[record_mod, dt.Columns.Count];
                    for (int l = 0; l < record_mod; l++)
                    {
                        for (int m = 0; m < dt.Columns.Count; m++)
                        {
                            Array_Record[l, m] = dtView[l][m];
                        }
                    }
                    Sheet.get_Range(Cell_Begin, Cell_End).Value2            = Array_Record;
                    Sheet.get_Range(Cell_Begin, Cell_End).Borders.LineStyle = 1;

                    //for (int l = 0; l < record_mod; l++)
                    //        for (int m = 0; m < dt.Columns.Count; m++)
                    //        {
                    //            if (m == 9)
                    //            {
                    //                if (Array_Record[l, m]==DBNull.Value)
                    //                {
                    //                   Sheet.get_Range("J"+l).Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DarkOrange);
                    //                }
                    //            }
                    //        }


                    // copy sheet to new
                    //   Sheet.Copy(WorkBook.Sheets[0], WorkBook.Sheets[1]);
                    //Microsoft.Office.Interop.Excel._Worksheet Sheet2 = (Microsoft.Office.Interop.Excel._Worksheet)WorkBook.Worksheets[2];
                    Sheet.Name = dtpb.Rows[ii]["DepartmentName"].ToString();
                    Sheet.Copy(Sheet);

                    // clrear txt
                    Array_Record = new object[record_mod, dt.Columns.Count];
                    for (int l = 0; l < record_mod; l++)
                    {
                        for (int m = 0; m < dt.Columns.Count; m++)
                        {
                            Array_Record[l, m] = "";
                        }
                    }
                    Sheet.get_Range(Cell_Begin, Cell_End).Value2            = Array_Record;
                    Sheet.get_Range(Cell_Begin, Cell_End).Borders.LineStyle = 0;
                    // end cler text in excel
                }
                // end for phong ban


                for (int iii = 1; iii < WorkBook.Worksheets.Count; iii++) // xóa số (2) o sheet name
                {
                    Microsoft.Office.Interop.Excel._Worksheet SheetName = (Microsoft.Office.Interop.Excel._Worksheet)WorkBook.Worksheets[iii];
                    string _sheetname = SheetName.Name;
                    _sheetname     = _sheetname.Replace("(2)", "");
                    SheetName.Name = _sheetname;
                }
                //save file
                if (sfilename == "")
                {
                    ReportFile.AlertBeforeOverwriting = false;
                    ReportFile.DisplayAlerts          = false;
                    ReportFile.Save(Type.Missing);
                    MessageBox.Show("Export Excel Successful !!!");
                }
                else
                {
                    //if (IsOpen)
                    //{
                    //    IsOpen = false;
                    //}
                    WorkBook.Save();
                    //  MessageBox.Show("Export Excel Successful !!!");
                }

                if (IsOpen)
                {
                    ReportFile.Visible     = true;
                    ReportFile.UserControl = true;
                }
                //ReportFile.Quit();
                releaseObject(Sheet);
                releaseObject(WorkBook);
                releaseObject(ReportFile);
                //foreach (Process process in Process.GetProcessesByName("EXCEL"))
                //{
                //    process.Kill();
                //}
            }
            catch (Exception ex)
            {
                Class.App.Log_Write(ex.Message);
            }
        }
Пример #3
0
        public static void ExportExcel_CTCCByGroup(DataTable dt, DataTable dtpb, DataTable dtgroup, int pos_row, string pos_column, bool IsOpen, string sTemplateFile, string sfilename, bool Show_Column)
        {
            Microsoft.Office.Interop.Excel.Application ReportFile = new Microsoft.Office.Interop.Excel.Application();
            ReportFile.Visible = false;
            Microsoft.Office.Interop.Excel._Workbook WorkBook;
            //Microsoft.Office.Interop.Excel.Range chartRange;
            try
            {
                if (sfilename != "")
                {
                    if (File.Exists(sfilename))
                    {
                        File.Copy(sTemplateFile, sfilename, true);
                    }
                    else
                    {
                        File.Copy(sTemplateFile, sfilename);
                    }
                    WorkBook = (Microsoft.Office.Interop.Excel._Workbook)(ReportFile.Workbooks.Open(sfilename,
                                                                                                    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));
                }
                else
                {
                    WorkBook = (Microsoft.Office.Interop.Excel._Workbook)(ReportFile.Workbooks.Add(sTemplateFile));
                }
                Microsoft.Office.Interop.Excel._Worksheet Sheet = (Microsoft.Office.Interop.Excel._Worksheet)WorkBook.ActiveSheet;
                // xu ly xuat theo phong ban
                // begin for phong ban
                DataView dtView      = new DataView(dt);
                DataView dtViewGroup = new DataView(dtgroup);
                for (int ii = 0; ii < dtpb.Rows.Count; ii++)
                {
                    dtView.RowFilter = "DepartmentName = '" + dtpb.Rows[ii]["DepartmentName"].ToString() + "'";
                    // kiem tra trong du lieu group xem co phong ban nay ko
                    dtViewGroup.RowFilter = "DepartmentName = '" + dtpb.Rows[ii]["DepartmentName"].ToString() + "'";
                    if (dtViewGroup.Count > 0)
                    {
                        #region Tim Thay Du lieu
                        for (int tt = 0; tt < dtViewGroup.Count; tt++)
                        {
                            dtView.RowFilter = "DepartmentName = '" + dtpb.Rows[ii]["DepartmentName"].ToString() + "' And GroupName = '" + dtViewGroup[tt]["GroupName"].ToString() + "'";

                            //Tinh so luong cot xuat ra
                            string MaxColumn = ((String)(Convert.ToChar(dt.Columns.Count / 26 + 64).ToString() + Convert.ToChar(dt.Columns.Count % 26 + 64))).Replace('@', ' ').Trim();
                            if (dt.Columns.Count % 26 == 0)
                            {
                                MaxColumn = ((String)(Convert.ToChar((dt.Columns.Count) / 26 + 64).ToString() + Convert.ToChar((dt.Columns.Count + 1) % 26 + 64))).Replace('@', ' ').Trim();
                            }
                            //cac tham so
                            int record_count = dtView.Count;
                            int record_mod   = record_count;

                            string pos_begin1 = pos_column;
                            int    pos_begin2 = pos_row;

                            object[,] Array_Record;
                            string Cell_Begin, Cell_End;

                            Cell_Begin = pos_begin1 + (pos_begin2).ToString();
                            Cell_End   = MaxColumn + (record_count + pos_row - 1).ToString();
                            if (Show_Column == true)
                            {
                                Cell_End = MaxColumn + (record_mod + pos_row).ToString();
                            }
                            Array_Record = new object[record_mod, dt.Columns.Count];
                            for (int l = 0; l < record_mod; l++)
                            {
                                for (int m = 0; m < dt.Columns.Count; m++)
                                {
                                    Array_Record[l, m] = dtView[l][m];
                                }
                                // SET MAU KHONG CHAM CONG
                                //if (Array_Record[l, 9] == DBNull.Value)
                                //{
                                //   // Microsoft.Office.Interop.Excel.Range Range = Sheet.get_Range("J" + l, "J" + l);
                                //    // Range.Cells.Interior.Color = System.Drawing.Color.Gainsboro.ToArgb();

                                //}
                                //if (Array_Record[l, 10] == DBNull.Value)
                                //{
                                //   // Microsoft.Office.Interop.Excel.Range Range = Sheet.get_Range("K" + l, "K" + l);
                                //  //  Range.Cells.Interior.Color = System.Drawing.Color.Gainsboro.ToArgb();

                                //}
                            }
                            Sheet.get_Range(Cell_Begin, Cell_End).Value2            = Array_Record;
                            Sheet.get_Range(Cell_Begin, Cell_End).Borders.LineStyle = 1;

                            Sheet.Name = dtViewGroup[tt]["GroupName"].ToString();
                            Sheet.Copy(Sheet);

                            // clrear txt
                            Array_Record = new object[record_mod, dt.Columns.Count];
                            for (int l = 0; l < record_mod; l++)
                            {
                                for (int m = 0; m < dt.Columns.Count; m++)
                                {
                                    Array_Record[l, m] = "";
                                }
                            }
                            Sheet.get_Range(Cell_Begin, Cell_End).Value2            = Array_Record;
                            Sheet.get_Range(Cell_Begin, Cell_End).Borders.LineStyle = 0;
                            //Sheet.get_Range(Cell_Begin, Cell_End).Interior.Color = System.Drawing.Color.Transparent.ToArgb();
                            // end cler text in excel
                        }
                        #endregion
                        // xu ly voi du lieu chi phong ban
                        dtView.RowFilter = "DepartmentName = '" + dtpb.Rows[ii]["DepartmentName"].ToString() + "' And GroupName is null";
                        if (dtView.Count > 0)
                        {
                            //Tinh so luong cot xuat ra
                            string _MaxColumn = ((String)(Convert.ToChar(dt.Columns.Count / 26 + 64).ToString() + Convert.ToChar(dt.Columns.Count % 26 + 64))).Replace('@', ' ').Trim();
                            if (dt.Columns.Count % 26 == 0)
                            {
                                _MaxColumn = ((String)(Convert.ToChar((dt.Columns.Count) / 26 + 64).ToString() + Convert.ToChar((dt.Columns.Count + 1) % 26 + 64))).Replace('@', ' ').Trim();
                            }
                            //cac tham so
                            int _record_count = dtView.Count;
                            int _record_mod   = _record_count;

                            string _pos_begin1 = pos_column;
                            int    _pos_begin2 = pos_row;

                            object[,] _Array_Record;
                            string _Cell_Begin, _Cell_End;

                            _Cell_Begin = _pos_begin1 + (_pos_begin2).ToString();
                            _Cell_End   = _MaxColumn + (_record_count + pos_row - 1).ToString();

                            _Array_Record = new object[_record_mod, dt.Columns.Count];
                            for (int l = 0; l < _record_mod; l++)
                            {
                                for (int m = 0; m < dt.Columns.Count; m++)
                                {
                                    _Array_Record[l, m] = dtView[l][m];
                                }
                                // SET MAU KHONG CHAM CONG
                                //if (_Array_Record[l, 9].ToString() == "")
                                //{
                                //    chartRange = Sheet.get_Range("J" + l + pos_row, Type.Missing);
                                //    chartRange.Interior.Color = System.Drawing.Color.Blue.ToArgb();
                                //}
                                //if (_Array_Record[l, 10].ToString() == "")
                                //{
                                //    chartRange = Sheet.get_Range("K" + l + pos_row, Type.Missing);
                                //    chartRange.Interior.Color = System.Drawing.Color.Blue.ToArgb();
                                //}
                            }
                            Sheet.get_Range(_Cell_Begin, _Cell_End).Value2            = _Array_Record;
                            Sheet.get_Range(_Cell_Begin, _Cell_End).Borders.LineStyle = 1;

                            Sheet.Name = dtpb.Rows[ii]["DepartmentName"].ToString();
                            Sheet.Copy(Sheet);

                            // clrear txt
                            _Array_Record = new object[_record_mod, dt.Columns.Count];
                            for (int l = 0; l < _record_mod; l++)
                            {
                                for (int m = 0; m < dt.Columns.Count; m++)
                                {
                                    _Array_Record[l, m] = "";
                                }
                            }
                            Sheet.get_Range(_Cell_Begin, _Cell_End).Value2            = _Array_Record;
                            Sheet.get_Range(_Cell_Begin, _Cell_End).Borders.LineStyle = 0;
                            //Sheet.get_Range(_Cell_Begin, _Cell_End).Interior.Color = System.Drawing.Color.Transparent.ToArgb();
                            // end cler text in excel
                        }
                        // Het xu ly voi du lieu chi phong ban
                    }
                    else
                    {
                        //Tinh so luong cot xuat ra
                        string MaxColumn = ((String)(Convert.ToChar(dt.Columns.Count / 26 + 64).ToString() + Convert.ToChar(dt.Columns.Count % 26 + 64))).Replace('@', ' ').Trim();
                        if (dt.Columns.Count % 26 == 0)
                        {
                            MaxColumn = ((String)(Convert.ToChar((dt.Columns.Count) / 26 + 64).ToString() + Convert.ToChar((dt.Columns.Count + 1) % 26 + 64))).Replace('@', ' ').Trim();
                        }
                        //cac tham so
                        int record_count = dtView.Count;
                        int record_mod   = record_count;

                        string pos_begin1 = pos_column;
                        int    pos_begin2 = pos_row;

                        object[,] Array_Record;
                        string Cell_Begin, Cell_End;

                        Cell_Begin = pos_begin1 + (pos_begin2).ToString();
                        Cell_End   = MaxColumn + (record_count + pos_row - 1).ToString();
                        if (Show_Column == true)
                        {
                            Cell_End = MaxColumn + (record_mod + pos_row).ToString();
                        }
                        Array_Record = new object[record_mod, dt.Columns.Count];
                        for (int l = 0; l < record_mod; l++)
                        {
                            for (int m = 0; m < dt.Columns.Count; m++)
                            {
                                Array_Record[l, m] = dtView[l][m];
                            }
                            // SET MAU KHONG CHAM CONG
                            //if (Array_Record[l, 9].ToString()== "")
                            //{
                            //    chartRange = Sheet.get_Range("J" + l+pos_row, Type.Missing);
                            //    chartRange.Interior.Color = System.Drawing.Color.Blue.ToArgb();
                            //}
                            //if (Array_Record[l, 10].ToString() == "")
                            //{
                            //    chartRange = Sheet.get_Range("K" + l + pos_row, Type.Missing);
                            //    chartRange.Interior.Color = System.Drawing.Color.Blue.ToArgb();
                            //}
                        }
                        Sheet.get_Range(Cell_Begin, Cell_End).Value2            = Array_Record;
                        Sheet.get_Range(Cell_Begin, Cell_End).Borders.LineStyle = 1;

                        Sheet.Name = dtpb.Rows[ii]["DepartmentName"].ToString();
                        Sheet.Copy(Sheet);

                        // clrear txt
                        Array_Record = new object[record_mod, dt.Columns.Count];
                        for (int l = 0; l < record_mod; l++)
                        {
                            for (int m = 0; m < dt.Columns.Count; m++)
                            {
                                Array_Record[l, m] = "";
                            }
                        }
                        Sheet.get_Range(Cell_Begin, Cell_End).Value2            = Array_Record;
                        Sheet.get_Range(Cell_Begin, Cell_End).Borders.LineStyle = 0;
                        //Sheet.get_Range(Cell_Begin, Cell_End).Interior.Color = System.Drawing.Color.Transparent.ToArgb();
                        // end cler text in excel
                    }
                }
                // end for phong ban
                for (int iii = 1; iii < WorkBook.Worksheets.Count; iii++) // xóa số (2) o sheet name
                {
                    Microsoft.Office.Interop.Excel._Worksheet SheetName = (Microsoft.Office.Interop.Excel._Worksheet)WorkBook.Worksheets[iii];
                    string _sheetname = SheetName.Name;
                    _sheetname     = _sheetname.Replace("(2)", "");
                    SheetName.Name = _sheetname;
                }
                //save file
                if (sfilename == "")
                {
                    ReportFile.AlertBeforeOverwriting = false;
                    ReportFile.DisplayAlerts          = false;
                    ReportFile.Save(Type.Missing);
                    MessageBox.Show("Export Excel Successful !!!");
                }
                else
                {
                    //if (IsOpen)
                    //{
                    //    IsOpen = false;
                    //}
                    WorkBook.Save();
                    //  MessageBox.Show("Export Excel Successful !!!");
                }

                if (IsOpen)
                {
                    ReportFile.Visible     = true;
                    ReportFile.UserControl = true;
                }
                //ReportFile.Quit();
                releaseObject(Sheet);
                releaseObject(WorkBook);
                releaseObject(ReportFile);
                //foreach (Process process in Process.GetProcessesByName("EXCEL"))
                //{
                //    process.Kill();
                //}
            }
            catch (Exception exx)
            {
                string Error = exx.ToString();
            }
        }
Пример #4
0
        private void BTN_DoExport_Click(object sender, RoutedEventArgs e)
        {
            string _saveFolder     = string.Empty;
            string _subFolder      = string.Empty;
            string _bookName       = string.Empty; // 工作簿名称,不包含文件后缀名
            string _fileNamePrefix = string.Empty;
            string _fileNameExt    = string.Empty;

            Int32 i = 0;

            Microsoft.Office.Interop.Excel._Workbook  _xlActiveBook  = null;
            Microsoft.Office.Interop.Excel._Worksheet _xlSheet       = null;
            Microsoft.Office.Interop.Excel._Workbook  _xlTargetBook  = null;
            Microsoft.Office.Interop.Excel._Worksheet _xlTargetSheet = null;


            try
            {
                // 设置按钮状态
                ZS_BTN_SelectFolder.IsEnabled = false;
                ZS_BTN_Exit.IsEnabled         = false;
                ZS_BTN_DoExport.IsEnabled     = false;

                _xlActiveBook = Globals.ThisAddIn.Application.ActiveWorkbook;

                // 文件后缀名
                if (_xlActiveBook.Name.Length > 0 && _xlActiveBook.Name.Contains(".x"))
                {
                    _fileNameExt = _xlActiveBook.Name.Substring(_xlActiveBook.Name.LastIndexOf('.'));
                }
                else
                {
                    _fileNameExt = string.Empty;
                }

                _saveFolder = ZS_LBL_SaveFolder.Content.ToString();
                // 检查目录是否存在
                if (!System.IO.Directory.Exists(_saveFolder))
                {
                    MessageBox.Show("未设置保存目录!");
                    return;
                }

                // 检查是否创建子目录,如果是,创建目录并且取得新的存储路径
                if ((bool)ZS_CHK_IsCreateSubFolder.IsChecked)
                {
                    _subFolder = _xlActiveBook.Name;
                    if (_subFolder.Trim().Length == 0)
                    {
                        _subFolder = System.Guid.NewGuid().ToString("N");
                    }
                    else
                    {
                        // 去掉后缀
                        if (_subFolder.IndexOf('.') > 0)
                        {
                            _subFolder = _subFolder.Substring(0, _subFolder.LastIndexOf('.'));
                        }
                    }
                    _saveFolder = _saveFolder + "\\" + _subFolder;
                    if (!System.IO.Directory.Exists(_saveFolder))
                    {
                        System.IO.Directory.CreateDirectory(_saveFolder);
                    }
                }

                // 检查文件名前缀设置
                if ((bool)ZS_CHK_IsAddWorkBookName.IsChecked)
                {
                    if (ZS_Text_FileNamePrefix.Text.Length == 0)
                    {
                        MessageBox.Show("请添加文件名前缀");
                        return;
                    }
                    _fileNamePrefix = ZS_Text_FileNamePrefix.Text + "_";
                }
                else
                {
                    _fileNamePrefix = string.Empty;
                }


                // 检查工作表个数


                Globals.ThisAddIn.Application.DisplayAlerts = false;
                Globals.ThisAddIn.Application.Visible       = false;

                // 遍历工作表,执行导出
                for (i = 1; i <= _xlActiveBook.Worksheets.Count; ++i)
                {
                    _xlSheet = (Microsoft.Office.Interop.Excel._Worksheet)_xlActiveBook.Worksheets[i];
                    string saveFullName = _saveFolder + "\\" + _fileNamePrefix + _xlSheet.Name + ".xlsx";


                    // 创建新工作簿
                    _xlTargetBook = Globals.ThisAddIn.Application.Workbooks.Add();

                    // 复制工作表
                    _xlTargetSheet = (Microsoft.Office.Interop.Excel._Worksheet)_xlTargetBook.Sheets[1];
                    _xlSheet.Copy(_xlTargetSheet);

                    // 删除多余的工作表
                    _xlTargetSheet.Delete();

                    // 删除已存在的工作簿
                    if (System.IO.File.Exists(saveFullName))
                    {
                        if ((bool)ZS_CHK_IsOverWrite.IsChecked)
                        {
                            System.IO.File.Delete(saveFullName);
                        }
                        else
                        {
                            saveFullName = saveFullName.Replace(_xlSheet.Name + ".xlsx", _xlSheet.Name + "_" + System.DateTime.Now.Millisecond.ToString() + ".xlsx");
                        }
                    }

                    // 保存工作簿
                    _xlTargetBook.SaveAs(saveFullName, Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook,
                                         Type.Missing,
                                         Type.Missing,
                                         Type.Missing,
                                         Type.Missing,
                                         Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
                                         Type.Missing,
                                         Type.Missing,
                                         Type.Missing,
                                         Type.Missing, Type.Missing
                                         );

                    _xlTargetBook.Close(false);
                }


                MessageBox.Show("保存完成!");

                if ((bool)ZS_CHK_OpenFolderAfterFinish.IsChecked)
                {
                    System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
                    psi.FileName  = "Explorer.exe";
                    psi.Arguments = "/e,/select," + _saveFolder;
                    System.Diagnostics.Process.Start(psi);
                }

                this.Close();
            }
            catch (Exception ex)
            {
                WriteRuntimeInfo(ex.Message + ex.StackTrace);
                System.Windows.Forms.MessageBox.Show(ex.Message + ex.StackTrace);
            }
            finally
            {
                ZS_BTN_SelectFolder.IsEnabled = true;
                ZS_BTN_Exit.IsEnabled         = true;
                ZS_BTN_DoExport.IsEnabled     = true;

                Globals.ThisAddIn.Application.DisplayAlerts = true;
                Globals.ThisAddIn.Application.Visible       = true;

                if (_xlTargetBook != null)
                {
                    _xlTargetBook = null;
                }
            }
        }