Ejemplo n.º 1
0
        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            int idx = dbFile.LastIndexOf(@"\");

            Microsoft.Win32.SaveFileDialog dlgSaveDiagram = new Microsoft.Win32.SaveFileDialog();
            dlgSaveDiagram.Filter = "Excel xlsx |*.xlsx;";
            dlgSaveDiagram.Title  = "Export Report";
            if (dlgSaveDiagram.ShowDialog() == true)
            {
                string filePath = dlgSaveDiagram.FileName;
                string vsd      = AppDomain.CurrentDomain.BaseDirectory.ToString() + "SimTech-PRV_DataSheet_Model.xlsx";
                System.IO.File.Copy(vsd, filePath);
                Microsoft.Office.Interop.Excel.Application xlApp      = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook    xlWorkBook = xlApp.Workbooks.Open(filePath, Type.Missing, false, Type.Missing,
                                                                                             Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                                                             Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Sheets.get_Item(1);
                xlWorkSheet.Cells[5][15] = "test";
                int count = 0;
                int row1  = 14;
                int note1 = 42;
                int row2  = 74;
                int note2 = 102;
                int row3  = 134;
                int note3 = 162;

                for (int i = 1; i < mainTab.Items.Count; i++)
                {
                    TabItem ti = (TabItem)mainTab.Items[i];
                    if (ti.Visibility == Visibility.Visible)
                    {
                        string       num = ti.Name.Remove(0, 2);
                        UC_CaseStudy uc  = (UC_CaseStudy)ti.FindName("uc" + num);
                        int          col = count % 5;
                        if (count <= 4)
                        {
                            xlWorkSheet.Cells[5 + col * 2][row1]     = ti.Header.ToString();
                            xlWorkSheet.Cells[5 + col * 2][row1 + 1] = 16;
                            if (ti.Header.ToString().Contains("Fire"))
                            {
                                xlWorkSheet.Cells[5 + col * 2][row1 + 1] = 21;
                            }
                            xlWorkSheet.Cells[5 + col * 2][row1 + 2] = uc.txtReliefPress.Text;
                            xlWorkSheet.Cells[5 + col * 2][row1 + 3] = uc.txtReliefTemp.Text;
                            xlWorkSheet.Cells[5 + col * 2][row1 + 4] = uc.txtReliefRate.Text;
                            xlWorkSheet.Cells[5 + col * 2][row1 + 5] = uc.txtReliefMW.Text;
                            xlWorkSheet.Cells[5 + col * 2][row1 + 6] = Compressibility;
                            xlWorkSheet.Cells[5 + col * 2][row1 + 7] = CpCv;
                            xlWorkSheet.Cells[3][note1 + count]      = uc.txtDescription.Text;
                            xlWorkSheet.Cells[3][note2 + count]      = uc.txtDescription.Text;
                            xlWorkSheet.Cells[3][note3 + count]      = uc.txtDescription.Text;
                        }
                        else if (count >= 5 && count <= 9)
                        {
                            xlWorkSheet.Cells[5 + col * 2][row2]     = ti.Header.ToString();
                            xlWorkSheet.Cells[5 + col * 2][row2 + 1] = 16;
                            if (ti.Header.ToString().Contains("Fire"))
                            {
                                xlWorkSheet.Cells[5 + col * 2][row2 + 1] = 21;
                            }
                            xlWorkSheet.Cells[5 + col * 2][row2 + 2] = uc.txtReliefPress.Text;
                            xlWorkSheet.Cells[5 + col * 2][row2 + 3] = uc.txtReliefTemp.Text;
                            xlWorkSheet.Cells[5 + col * 2][row2 + 4] = uc.txtReliefRate.Text;
                            xlWorkSheet.Cells[5 + col * 2][row2 + 5] = uc.txtReliefMW.Text;
                            xlWorkSheet.Cells[5 + col * 2][row2 + 6] = Compressibility;
                            xlWorkSheet.Cells[5 + col * 2][row2 + 7] = CpCv;
                            xlWorkSheet.Cells[3][note1 + count]      = uc.txtDescription.Text;
                            xlWorkSheet.Cells[3][note2 + count]      = uc.txtDescription.Text;
                            xlWorkSheet.Cells[3][note3 + count]      = uc.txtDescription.Text;
                        }
                        else
                        {
                            xlWorkSheet.Cells[5 + col * 2][row3]     = ti.Header.ToString();
                            xlWorkSheet.Cells[5 + col * 2][row3 + 1] = 16;
                            if (ti.Header.ToString().Contains("Fire"))
                            {
                                xlWorkSheet.Cells[5 + col * 2][row3 + 1] = 21;
                            }
                            xlWorkSheet.Cells[5 + col * 2][row3 + 2] = uc.txtReliefPress.Text;
                            xlWorkSheet.Cells[5 + col * 2][row3 + 3] = uc.txtReliefTemp.Text;
                            xlWorkSheet.Cells[5 + col * 2][row3 + 4] = uc.txtReliefRate.Text;
                            xlWorkSheet.Cells[5 + col * 2][row3 + 5] = uc.txtReliefMW.Text;
                            xlWorkSheet.Cells[5 + col * 2][row3 + 6] = Compressibility;
                            xlWorkSheet.Cells[5 + col * 2][row3 + 7] = CpCv;
                            xlWorkSheet.Cells[3][note1 + count]      = uc.txtDescription.Text;
                            xlWorkSheet.Cells[3][note2 + count]      = uc.txtDescription.Text;
                            xlWorkSheet.Cells[3][note3 + count]      = uc.txtDescription.Text;
                        }

                        count++;
                    }
                }
                if (count <= 5)
                {
                    Microsoft.Office.Interop.Excel.Range r = xlWorkSheet.Range[xlWorkSheet.Cells[2][122], xlWorkSheet.Cells[3][181]];
                    r.UnMerge();
                    r = xlWorkSheet.Range[xlWorkSheet.Cells[2][122], xlWorkSheet.Cells[14][181]];
                    r.Clear();

                    Microsoft.Office.Interop.Excel.Shape pic = xlWorkSheet.Shapes.Item(3) as Microsoft.Office.Interop.Excel.Shape;
                    pic.Delete();

                    r = xlWorkSheet.Range[xlWorkSheet.Cells[2][62], xlWorkSheet.Cells[3][121]];
                    r.UnMerge();
                    r = xlWorkSheet.Range[xlWorkSheet.Cells[2][62], xlWorkSheet.Cells[14][121]];
                    r.Clear();

                    pic = xlWorkSheet.Shapes.Item(2) as Microsoft.Office.Interop.Excel.Shape;
                    pic.Delete();
                }
                else if (count <= 10)
                {
                    Microsoft.Office.Interop.Excel.Range r = xlWorkSheet.Range[xlWorkSheet.Cells[2][122], xlWorkSheet.Cells[3][181]];
                    r.UnMerge();
                    r = xlWorkSheet.Range[xlWorkSheet.Cells[2][122], xlWorkSheet.Cells[14][181]];
                    r.Clear();

                    Microsoft.Office.Interop.Excel.Shape pic = xlWorkSheet.Shapes.Item(3) as Microsoft.Office.Interop.Excel.Shape;
                    pic.Delete();
                }

                xlWorkBook.Save();
                xlWorkBook.Close(true, Type.Missing, Type.Missing);
                xlApp.Quit();


                releaseObject(xlWorkSheet);
                releaseObject(xlWorkBook);
                releaseObject(xlApp);
            }
        }