Exemplo n.º 1
0
        private void addFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _frmNewFile newFile = new _frmNewFile();

            newFile.Text = _treeViewProjectExplore.SelectedNode.Text + " - " + newFile.Text;
            newFile.ShowDialog();

            Excel.Application xlApp      = default(Excel.Application);
            Excel.Workbook    xlWorkBook = default(Excel.Workbook);
            //TabPage sheetPage = new TabPage(_treeViewProjectExplore.SelectedNode.Text);
            //Excel.Worksheet xlWorkSheet = default(Excel.Worksheet);
            Excel.Worksheet xlWorkSheet = new Excel.Worksheet();

            TreeNode treeNode = new TreeNode();

            treeNode.Text       = newFile.textBoxName.Text + "." + newFile.comboBoxType.Text;
            treeNode.Tag        = treeNode.Text;
            treeNode.Name       = "File";
            treeNode.ImageIndex = newFile.comboBoxType.SelectedIndex;
            newFile.Close();
            if (newFile.textBoxName.Text != "")
            {
                _treeViewProjectExplore.SelectedNode.Nodes.Add(treeNode);
                _treeViewProjectExplore.SelectedNode.ExpandAll();

                string fName = _treeViewProjectExplore.Nodes[0].Tag + @"\" + _treeViewProjectExplore.SelectedNode.Tag + @"\" + treeNode.Tag;
                try
                {
                    object misValue = System.Reflection.Missing.Value;
                    xlApp       = new Excel.Application();
                    xlWorkBook  = xlApp.Workbooks.Add(misValue);
                    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                    xlWorkBook.SaveAs(fName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                    xlWorkBook.Close(true, misValue, misValue);
                    xlApp.Quit();
                    releaseObject(xlWorkSheet);
                    releaseObject(xlWorkBook);
                    releaseObject(xlApp);
                }
                finally
                {
                    if (xlApp != null)
                    {
                        releaseObject(xlApp);
                    }
                    if (xlWorkBook != null)
                    {
                        releaseObject(xlWorkBook);
                    }
                    if (xlWorkSheet != null)
                    {
                        releaseObject(xlWorkSheet);
                    }
                }
                if (System.IO.File.Exists(fName))
                {
                    doc = CompoundDocument.Open(fName);
                }
            }
        }
Exemplo n.º 2
0
        private void newNToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //test create xls file
            string    file      = "C:\\newdoc.xls";
            Workbook  workbook  = new Workbook();
            Worksheet worksheet = new Worksheet("First Sheet");

            worksheet.Cells[0, 1]             = new Cell((short)1);
            worksheet.Cells[2, 0]             = new Cell(2.8);
            worksheet.Cells[3, 3]             = new Cell((decimal)3.45);
            worksheet.Cells[2, 2]             = new Cell("Text string");
            worksheet.Cells[2, 4]             = new Cell("Second string");
            worksheet.Cells[4, 0]             = new Cell(32764.5, "#,##0.00");
            worksheet.Cells[5, 1]             = new Cell(DateTime.Now, @"YYYY\-MM\-DD");
            worksheet.Cells.ColumnWidth[0, 1] = 3000;
            //Picture pic = new Picture();
            //pic.Image = Excel.Image.FromFile("C:\\DelBreakpoint.png");
            //pic.TopLeftCorner = new CellAnchor(5, 1, 0, 0);
            //pic.BottomRightCorner = new CellAnchor(12, 5, 592, 243);
            //worksheet.AddPicture(pic);
            workbook.Worksheets.Add(worksheet);
            workbook.Save(file);
            //open created file
            doc      = CompoundDocument.Open(file);
            IsOpened = true;
            PopulateTreeview(file);
        }
Exemplo n.º 3
0
        public void Test()
        {
            string filename = Path.GetTempFileName();

            try
            {
                // create invalid file for read
                using (var writer = new StreamWriter(filename))
                    writer.WriteLine("test");

                try
                {
                    // triggers exception
                    CompoundDocument.Open(filename);
                }
                catch
                {
                    Assert.IsTrue(true, "Expection trigger.");
                }
            }
            finally
            {
                // should not trigger any IO Exception
                File.Delete(filename);
            }
        }
Exemplo n.º 4
0
        public void LoadExcelSheets(string filepath)
        {
            CompoundDocument _doc = CompoundDocument.Open(filepath);

            byte[] bookdata = _doc.GetStreamData("Workbook"); // Doc Theo Byte
            if (bookdata == null)
            {
                return;
            }
            Workbook book = WorkbookDecoder.Decode(new MemoryStream(bookdata));


            foreach (Worksheet sheet in book.Worksheets)
            {
                ExcelTabPage sheetPage = new ExcelTabPage(sheet.Name); // Tên Sheet
                sheetPage.FileName = filepath;
                dgvCells           = new DataGridView();               // Tao Moi Datagridview
                dgvCells.Dock      = DockStyle.Fill;
                //dgvCells.RowCount = sheet.Cells.LastRowIndex + 1;
                //dgvCells.ColumnCount = sheet.Cells.LastColIndex + 1;
                dgvCells.RowCount    = 500;
                dgvCells.ColumnCount = 500;
                dgvCells.KeyUp      += dgvCells_KeyUp;

                // tranverse cells
                foreach (Pair <Pair <int, int>, Cell> cell in sheet.Cells)
                {
                    dgvCells[cell.Left.Right, cell.Left.Left].Value = cell.Right.Value;
                    if (cell.Right.Style.BackColor != Color.White)
                    {
                        dgvCells[cell.Left.Right, cell.Left.Left].Style.BackColor = cell.Right.Style.BackColor;
                    }
                }

                // tranvers rows by Index
                for (int rowIndex = sheet.Cells.FirstRowIndex; rowIndex <= sheet.Cells.LastRowIndex; rowIndex++)
                {
                    Row row = sheet.Cells.GetRow(rowIndex);
                    for (int colIndex = row.FirstColIndex; colIndex <= row.LastColIndex; colIndex++)
                    {
                        Cell cell = row.GetCell(colIndex);
                    }
                }
                // tranvers rows directly

                foreach (KeyValuePair <int, Row> row in sheet.Cells.Rows)
                {
                    foreach (KeyValuePair <int, Cell> cell in row.Value)
                    {
                    }
                }

                _doc.Close();
                sheetPage.Controls.Add(dgvCells);
                tabControl1.TabPages.Add(sheetPage);
                tabControl1.SelectedTab = sheetPage;
            }
        }
Exemplo n.º 5
0
        private void _treeViewProjectExplore_DoubleClick(object sender, EventArgs e)
        {
            tabControl1X.SelectedIndex = 2;
            if (_treeViewProjectExplore.SelectedNode.Name == "File")
            {
                try
                {
                    LoadExcelSheets();
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show("Error opening the excel file." + Environment.NewLine +
                                                         ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            //Open file excel
            string fName  = _treeViewProjectExplore.Nodes[0].Tag + @"\" + _treeViewProjectExplore.Nodes[0].Nodes[0].Text + @"\" + _treeViewProjectExplore.SelectedNode.Text;
            string fName1 = _treeViewProjectExplore.Nodes[0].Tag + @"\" + _treeViewProjectExplore.Nodes[0].Nodes[1].Text + @"\" + _treeViewProjectExplore.SelectedNode.Text;
            string fName2 = _treeViewProjectExplore.Nodes[0].Tag + @"\" + _treeViewProjectExplore.Nodes[0].Nodes[2].Text + @"\" + _treeViewProjectExplore.SelectedNode.Text;

            if (_treeViewProjectExplore.SelectedNode.Text == _treeViewProjectExplore.SelectedNode.Text)
            {
                try
                {
                    doc = CompoundDocument.Open(fName);
                    LoadExcelSheets();
                }
                catch (Exception ex)
                {
                }
                if (_treeViewProjectExplore.SelectedNode.Text == _treeViewProjectExplore.SelectedNode.Text)
                {
                    try
                    {
                        doc = CompoundDocument.Open(fName1);
                        LoadExcelSheets();
                    }
                    catch (Exception ex)
                    {
                    }
                }
                if (_treeViewProjectExplore.SelectedNode.Text == _treeViewProjectExplore.SelectedNode.Text)
                {
                    try
                    {
                        doc = CompoundDocument.Open(fName2);
                        LoadExcelSheets();
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
        }
 //------------------ Get Open ----
 public string getopen(string path)
 {
     try
     {
         string file = path;
         doc = CompoundDocument.Open(file);
         return(file);
     }
     catch (Exception e)
     {
         return("");
     }
 }
        /// <summary>
        /// 初始化
        /// </summary>
        public void Intialize()
        {
            this.InitializeLog();
            this.ErrLock[0] = new object();
            this.ErrLock[1] = new object();
            this.ErrLock[2] = new object();

            WriteLog(MsgLevel.Info, "机器启动...");
            WriteLog(MsgLevel.Info, "正在载入相关字符定义");

            this.Start();

            if (!File.Exists("./Configure/机器字符定义.xls"))
            {
                WriteLog(MsgLevel.Info, "字符定义载入失败,没有找到定义文件");
                return;
            }
            // 读取Excel进行设置ErrorCode的定义
            CompoundDocument doc = CompoundDocument.Open("./Configure/机器字符定义.xls");

            byte[] bookdata = doc.GetStreamData("Workbook");
            if (bookdata == null)
            {
                return;
            }
            using (var ms = new MemoryStream(bookdata))
            {
                Workbook  book  = WorkbookDecoder.Decode(ms);
                Worksheet sheet = book.Worksheets[0];

                int rowCount = sheet.Cells.LastRowIndex + 1;
                int colCount = sheet.Cells.LastColIndex + 1;
                for (int i = 0; i < colCount; ++i)
                {
                    DataColumn dt = new DataColumn(sheet.Cells[0, i].StringValue);
                    stringDB.Columns.Add(dt);
                }

                string[] rowValue = new string[colCount];
                for (int row = 1; row < rowCount; row++)
                {
                    for (int col = 0; col < colCount; col++)
                    {
                        rowValue[col] = sheet.Cells[row, col].StringValue;
                    }

                    stringDB.Rows.Add(rowValue);
                }
            }
            WriteLog(MsgLevel.Info, "字符定义载入成功");
        }
Exemplo n.º 8
0
 private void openOToolStripMenuItem_Click(object sender, EventArgs e)
 {
     try
     {
         string xlsfilter = "Excel files(*.xls)|*.xls|All files (*.*)|*.*";
         string file      = FileSelector.BrowseFile(xlsfilter);
         if (file == null)
         {
             return;
         }
         doc      = CompoundDocument.Open(file);
         IsOpened = true;
         PopulateTreeview(file);
     }
     catch (Exception error)
     {
         MessageBox.Show(error.Message);
     }
 }
Exemplo n.º 9
0
        private void addFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _frmNewFile newFile = new _frmNewFile();

            newFile.ShowDialog();

            Excel.Application xlApp       = default(Excel.Application);
            Excel.Workbook    xlWorkBook  = default(Excel.Workbook);
            Excel.Worksheet   xlWorkSheet = default(Excel.Worksheet);

            TreeNode treeNode = new TreeNode();

            treeNode.Text       = newFile.textBoxName.Text + "." + newFile.comboBoxType.Text;
            treeNode.Tag        = treeNode.Text;
            treeNode.Name       = "File";
            treeNode.ImageIndex = newFile.comboBoxType.SelectedIndex;
            newFile.Close();
            if (newFile.textBoxName.Text != "")
            {
                _treeViewProjectExplore.SelectedNode.Nodes.Add(treeNode);
                _treeViewProjectExplore.SelectedNode.ExpandAll();

                string fName = _treeViewProjectExplore.Nodes[0].Tag + @"\" + _treeViewProjectExplore.SelectedNode.Tag + @"\" + treeNode.Tag;
                try
                {
                    object misValue = System.Reflection.Missing.Value;
                    xlApp       = new Excel.Application();
                    xlWorkBook  = xlApp.Workbooks.Add(misValue);
                    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                    xlWorkBook.SaveAs(fName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                    xlWorkBook.Close(true, misValue, misValue);
                    xlApp.Quit();
                    releaseObject(xlWorkSheet);
                    releaseObject(xlWorkBook);
                    releaseObject(xlApp);
                }
                finally
                {
                    if (xlApp != null)
                    {
                        releaseObject(xlApp);
                    }
                    if (xlWorkBook != null)
                    {
                        releaseObject(xlWorkBook);
                    }
                    if (xlWorkSheet != null)
                    {
                        releaseObject(xlWorkSheet);
                    }
                }
                if (System.IO.File.Exists(fName))
                {
                    //if (System.Windows.Forms.MessageBox.Show("Would you like to open the excel file?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    //{
                    //    try
                    //    {
                    //        //System.Diagnostics.Process.Start(fName);
                    doc = CompoundDocument.Open(fName);
                    //        LoadExcel();

                    //    }
                    //    catch (Exception ex)
                    //    {
                    //        System.Windows.Forms.MessageBox.Show("Error opening the excel file." + Environment.NewLine +
                    //          ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //    }
                    //} //doc1 = CompoundDocument.Open(fName);
                }
            }
        }