Exemple #1
0
        void MakeExcelFile(string shab_fname)
        {
            Excel._Application oExcel = new Excel.Application();

            string        dir           = comboBox1.SelectedItem.ToString() + "_" + dateTimePicker1.Value.Year + "-" + dateTimePicker1.Value.Month + "-" + dateTimePicker1.Value.Day;
            DirectoryInfo directoryinfo = Directory.CreateDirectory(Environment.CurrentDirectory + "\\" + dir);

            string WorkId = dataGridView1.CurrentRow.Cells["WorkId"].Value.ToString();

            string fname = Environment.CurrentDirectory + "\\" + dir + "\\" + WorkId + "_" + shab_fname;

            Excel::Application excelAplication = new Excel.Application();

            excelAplication.Visible = false;
            Excel::Workbook excelBook = excelAplication.Workbooks.Open(Environment.CurrentDirectory + "\\" + shab_fname, 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);
            // сохранение шаблона под другим именем, чтобы не испортить исходный файл

            Excel::Sheets    excelSheet     = excelBook.Worksheets;
            Excel::Worksheet excelWorksheet = (Excel::Worksheet)excelSheet.get_Item("f112ep"); // заполнение рабочего листа ланными из dataGridView

            excelWorksheet.Range["P37"].Value  = textBox_otkogo.Text;
            excelWorksheet.Range["AB39"].Value = textBox_adrotravitelya.Text;
            excelWorksheet.Range["T28"].Value  = dataGridView1.CurrentRow.Cells[0].Value;
            excelAplication.ActiveWorkbook.SaveAs(fname, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            excelAplication.Quit();

            if (checkBox2.Checked)
            {
                System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
                myProcess.StartInfo.FileName       = fname;
                myProcess.StartInfo.Verb           = "Open";
                myProcess.StartInfo.CreateNoWindow = false;
                myProcess.Start();
            }
        }
Exemple #2
0
        protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state)
        {
            PropertyDescriptor property = context.DataContext.GetProperties()[ExcelCreate.GetExcelAppTag];
            Excel::Application excelApp = property.GetValue(context.DataContext) as Excel::Application;

            try
            {
                string       cellName_Begin = CellName_Begin.Get(context);
                string       cellName_End = CellName_End.Get(context);
                int          cellRow_Begin = CellRow_Begin.Get(context);
                int          cellColumn_Begin = CellColumn_Begin.Get(context);
                int          cellRow_End = CellRow_End.Get(context);
                int          cellColumn_End = CellColumn_End.Get(context);
                int          destCellRow = DestCellRow.Get(context);
                int          destCellColumn = DestCellColumn.Get(context);
                string       copySheet = CopySheet.Get(context);
                string       destSheet = DestSheet.Get(context);
                string       destCell = DestCell.Get(context);
                string       destDestFilePath = DestFilePath.Get(context);
                Excel::Range range1, range2;

                Excel::_Worksheet sheet;
                if (copySheet != null)
                {
                    sheet = excelApp.ActiveWorkbook.Sheets[copySheet];
                }
                else
                {
                    sheet = excelApp.ActiveSheet;
                }
                range1 = cellName_Begin == null ? sheet.Cells[cellRow_Begin, cellColumn_Begin] : sheet.Range[cellName_Begin];
                range2 = cellName_End == null ? sheet.Cells[cellRow_End, cellColumn_End] : sheet.Range[cellName_End];
                sheet.Range[range1, range2].Copy(Type.Missing);

                Excel::_Worksheet pasteSheet;
                if (destDestFilePath != null && destDestFilePath != "")
                {
                    if (!File.Exists(destDestFilePath))
                    {
                        SharedObject.Instance.Output(SharedObject.enOutputType.Error, "文件不存在,请检查路径有效性", destDestFilePath);
                        new CommonVariable().realaseProcessExit(excelApp);
                    }
                    else
                    {
                        Excel::Workbook workbook2 = excelApp.Workbooks._Open(destDestFilePath);
                        if (destSheet != null)
                        {
                            pasteSheet = workbook2.Sheets[destSheet];
                        }
                        else
                        {
                            pasteSheet = workbook2.ActiveSheet;
                        }

                        Excel::Range pasteRange = destCell == null ? pasteSheet.Cells[destCellRow, destCellColumn] : pasteSheet.Range[destCell];
                        pasteSheet.Paste(pasteRange);
                        workbook2.Save();
                        workbook2.Close();
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(pasteSheet);
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(pasteRange);
                        pasteRange = null;
                    }
                }
                else
                {
                    if (destSheet != null)
                    {
                        pasteSheet = excelApp.ActiveWorkbook.Sheets[destSheet];
                    }
                    else
                    {
                        pasteSheet = excelApp.ActiveSheet;
                    }

                    Excel::Range pasteRange = destCell == null ? sheet.Cells[destCellRow, destCellColumn] : sheet.Range[destCell];
                    pasteSheet.Paste(pasteRange);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(pasteSheet);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(pasteRange);
                    pasteRange = null;
                }

                System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(range1);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(range2);
                range1 = null; range2 = null;
                sheet  = null; pasteSheet = null;
                GC.Collect();
            }
            catch (Exception e)
            {
                SharedObject.Instance.Output(SharedObject.enOutputType.Error, "EXCEL复制粘贴过程出错", e.Message);
                new CommonVariable().realaseProcessExit(excelApp);
            }
            m_Delegate = new runDelegate(Run);
            return(m_Delegate.BeginInvoke(callback, state));
        }