Exemplo n.º 1
0
 ///// <summary>
 /// Clear CircleReference
 /// </summary>
 /// <param name="sheet">Worksheet object</param>
 private void ClearCircleReference(Microsoft.Office.Interop.Excel.Worksheet sheet)
 {
     Microsoft.Office.Interop.Excel.Range range = sheet.CircularReference;
     while (range != null)
     {
         range.Clear();
         range = sheet.CircularReference;
     }
 }
Exemplo n.º 2
0
        private void fOpenOKClick(Object sender, EventArgs e)
        {
            if (fopen.FileName == "")
            {
                lbFileName.Text = "Bạn chưa chọn File";
                return;
            }
            // tạo app excel, workbook từ filename và sheet đầu tiên của workbook đó
            lbFileName.Text = fopen.FileName;
            Microsoft.Office.Interop.Excel.Application app       = new Microsoft.Office.Interop.Excel.Application();;
            Microsoft.Office.Interop.Excel.Workbook    workBook  = app.Workbooks.Open(fopen.FileName);
            Microsoft.Office.Interop.Excel.Worksheet   workSheet = workBook.Worksheets[1];
            Microsoft.Office.Interop.Excel.Range       range     = workSheet.UsedRange;
            try
            {
                int rows = range.Rows.Count;
                int cols = range.Columns.Count;
                //get header
                for (int c = 1; c <= cols; c++)
                {
                    DataGridViewTextBoxColumn DGVColumn = new DataGridViewTextBoxColumn();
                    DGVColumn.Name       = "Col" + c;
                    DGVColumn.HeaderText = range.Cells[1, c].Value == null || range.Cells[1, c].Value.ToString().Trim() == "" ? "": range.Cells[1, c].Value.ToString();
                    DGVColumn.Visible    = true;
                    DGVColumn.Frozen     = false;
                    DGVColumn.ReadOnly   = false;
                    if (c == 1)
                    {
                        dgvQuestion.Rows.Clear();
                        dgvQuestion.Columns.Clear();
                    }
                    dgvQuestion.Columns.Add(DGVColumn);
                }
                //get row
                for (int r = 2; r <= rows; r++)
                {
                    DataGridViewRow dgvRow = new DataGridViewRow();
                    for (int c = 1; c <= cols; c++)
                    {
                        DataGridViewCell cell = new DataGridViewTextBoxCell();
                        cell.Value = range.Cells[r, c].Value == null || range.Cells[r, c].Value.ToString().Trim() == "" ? "" : range.Cells[r, c].Value.ToString();
                        dgvRow.Cells.Add(cell);
                    }

                    dgvQuestion.Rows.Add(dgvRow);
                }

                btnOK.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                range.Clear();
                workSheet = null;
                workBook.Close();
                app.Quit();
                workBook = null;
            }
        }
Exemplo n.º 3
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);
            }
        }