private void addNewFileToolStripMenuItem_Click(object sender, EventArgs e) { AddFile1 newFile = new AddFile1(); //newFile.labelX3.Text = newFile.labelX3.Text + "-" + treeViewproject.SelectedNode.Text; newFile.ShowDialog(); main main = new main(); Excel.Application xlApp = default(Excel.Application); Excel.Workbook xlWorkBook = default(Excel.Workbook); Excel.Worksheet xlWorkSheet = default(Excel.Worksheet); FileTreeNode treeNode = new FileTreeNode(); treeNode.Text = newFile.txtName.Text + "." + newFile.cboType.Text; treeNode.Tag = CurrentProjectPath + @"\" + treeViewproject.SelectedNode.Text + @"\" + treeNode.Text; treeNode.Name = "File"; treeNode.ImageIndex = newFile.cboType.SelectedIndex; newFile.Close(); if (newFile.txtName.Text != "") { treeViewproject.SelectedNode.Nodes.Add(treeNode); treeViewproject.SelectedNode.ExpandAll(); //@new new1 = new @new(); //string path = new1.ProjectPath; //string fName = path + @"\" + treeViewproject.SelectedNode.Tag + @"\" + treeNode.Tag; string fName = treeViewproject.Nodes[0].Tag + @"\" + treeViewproject.SelectedNode.Text + @"\" + treeNode.Text; //const string fName =@"F:\abc.xls"; 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); // //} // //catch (Exception ex) // //{ // // System.Windows.Forms.MessageBox.Show("Error opening the excel file." + Environment.NewLine + // // ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); // //} // //} //} //CompoundDocument _doc = CompoundDocument.Open(fName); } }
private void addNewFileToolStripMenuItem_Click(object sender, EventArgs e) { AddFile1 newFile = new AddFile1(); //newFile.labelX3.Text = newFile.labelX3.Text + "-" + treeViewproject.SelectedNode.Text; newFile.ShowDialog(); main main = new main(); Excel.Application xlApp = default(Excel.Application); Excel.Workbook xlWorkBook = default(Excel.Workbook); Excel.Worksheet xlWorkSheet = default(Excel.Worksheet); FileTreeNode treeNode = new FileTreeNode(); treeNode.Text = newFile.txtName.Text + "." + newFile.cboType.Text; treeNode.Tag = CurrentProjectPath + @"\" + treeViewproject.SelectedNode.Text + @"\" + treeNode.Text; treeNode.Name = "File"; treeNode.ImageIndex = newFile.cboType.SelectedIndex; newFile.Close(); if (newFile.txtName.Text != "") { treeViewproject.SelectedNode.Nodes.Add(treeNode); treeViewproject.SelectedNode.ExpandAll(); //@new new1 = new @new(); //string path = new1.ProjectPath; //string fName = path + @"\" + treeViewproject.SelectedNode.Tag + @"\" + treeNode.Tag; string fName = treeViewproject.Nodes[0].Tag + @"\" + treeViewproject.SelectedNode.Text + @"\" + treeNode.Text; //const string fName =@"F:\abc.xls"; 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); // //} // //catch (Exception ex) // //{ // // System.Windows.Forms.MessageBox.Show("Error opening the excel file." + Environment.NewLine + // // ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); // //} // //} //} //CompoundDocument _doc = CompoundDocument.Open(fName); } }