//程序单输出 public bool WritePgmList(string fileName, NCListData exceldata) { Microsoft.Office.Interop.Excel.Application xls = new Microsoft.Office.Interop.Excel.Application(); _Workbook book = null; _Worksheet sheet = null; try { book = xls.Workbooks.Open(fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); xls.Visible = false; xls.DisplayAlerts = true; sheet = (_Worksheet)book.Worksheets.get_Item(1); Microsoft.Office.Interop.Excel.Range cellRange = sheet.Cells; NCDoc doc = exceldata.NcDoc; VWConf vwConf = VWConf.Instance; int timeTotle = 0; //Jobs明细输出 List <NCJob> jobs = doc.Jobs; for (int i = 0; i < jobs.Count; i++) { NCJob job = jobs[i]; //程序名 cellRange[i + 23, 1] = job.NC_Job; //加工内容 cellRange[i + 23, 3] = vwConf.GetProgramDispString(job.NC_Job_Code); //刀具-直径 cellRange[i + 23, 5] = "D" + job.Tool_D; //刀具-半径 cellRange[i + 23, 6] = "R" + job.Tool_R; //刀具-悬长 cellRange[i + 23, 7] = job.TotalToolLength; //加工角度 cellRange[i + 23, 8] = job.MachineAxis; //余量 cellRange[i + 23, 11] = job.Stockallow; //料厚 cellRange[i + 23, 12] = job.Wallthick; //步距 cellRange[i + 23, 13] = job.Stepover; //F值 cellRange[i + 23, 14] = job.F; //刀补 cellRange[i + 23, 15] = job.IsR ? "有" : ""; //理论用时 cellRange[i + 23, 16] = job.NCTime; timeTotle += job.Time_Seconds; } //2-19行基本信息 cellRange[2, 2] = exceldata.ProjectName; cellRange[2, 5] = exceldata.ModuleName; cellRange[4, 2] = exceldata.PartName; cellRange[4, 5] = exceldata.SingleName; cellRange[6, 2] = exceldata.Procedure; cellRange[6, 5] = exceldata.LeftRight; cellRange[8, 2] = DateTime.Now.ToString("yyyy-MM-dd"); cellRange[8, 5] = exceldata.BenchMark; cellRange[11, 2] = VWUtil.FormatHMS(timeTotle); cellRange[11, 5] = exceldata.Side; cellRange[16, 2] = doc.NC_Documentation; cellRange[18, 2] = doc.PUTNC_Datei; //图片 Microsoft.Office.Interop.Excel.Range picRange = null; picRange = sheet.get_Range("R2", Missing.Value); picRange.Select(); Microsoft.Office.Interop.Excel.Pictures pics = (Microsoft.Office.Interop.Excel.Pictures)sheet.Pictures(Missing.Value); pics.Delete(); pics.Insert(exceldata.PicturePath, Missing.Value); book.Save(); book.Close(false, Missing.Value, Missing.Value); return(true); } catch (Exception ex) { return(false); } finally { xls.Quit(); xls = null; sheet = null; book = null; GC.Collect(); } }
private void buttonShengCheng_Click(object sender, EventArgs e) { NCListData exceldata = new NCListData(); exceldata.ProjectName = textBoxProjectName.Text; exceldata.ModuleName = comboBoxModuleName.Text; exceldata.PartName = comboBoxPartName.Text; exceldata.BenchMark = comboBoxBenchMark.Text; exceldata.Procedure = comboBoxProcedure.Text; exceldata.LeftRight = comboBoxLeftRight.Text; exceldata.SingleName = comboBoxSingleName.Text; exceldata.Side = comboBoxSide.Text; exceldata.CADPath = textBoxCADPath.Text; exceldata.PicturePath = textBoxPicturePath.Text; exceldata.ProgTime = textBoxTime.Text; exceldata.Memo = textBoxMemo.Text; if (!exceldata.Valid()) { return; } NCDoc doc = new NCDoc(); if (!doc.Load(exceldata.CADPath)) { MessageBox.Show("文件分析失败!\n" + "CAD路径:" + exceldata.CADPath, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } exceldata.NcDoc = doc; SaveFileDialog saveExcelPath = new SaveFileDialog(); saveExcelPath.InitialDirectory = Path.GetDirectoryName(exceldata.PicturePath); saveExcelPath.Title = "选择程序加工单保存路径"; saveExcelPath.Filter = "加工程序单(*.xlsx)|*.xlsx"; saveExcelPath.FileName = "加工程序单.xlsx"; if (saveExcelPath.ShowDialog() != DialogResult.OK) { return; } string pgmListPath = saveExcelPath.FileName; string pgmTimeListPath = Path.Combine(VWConf.Instance.TimeFolder, "加工时间统计单.xlsx"); if (!File.Exists(pgmTimeListPath)) { //不存在则拷贝一份过去 if (!VWTemplates.CopyPgmTimeList(pgmTimeListPath)) { MessageBox.Show("不能拷贝加工程序统计单模板!\n" + "到:" + pgmTimeListPath, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } if (!VWTemplates.CopyPgmList(pgmListPath)) { MessageBox.Show("不能拷贝加工程序单模板!\n" + "到:" + pgmListPath, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } EnableCtrl(false); NCExcelWriter writer = new NCExcelWriter(); if (!writer.WritePgmList(pgmListPath, exceldata)) { MessageBox.Show("文件生成失败!\n" + "加工程序单:" + pgmListPath, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); EnableCtrl(true); return; } if (!writer.WritePgmTimeList(pgmTimeListPath, exceldata)) { MessageBox.Show("文件生成失败!\n" + "加工时间统计单:" + pgmTimeListPath, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); EnableCtrl(true); return; } EnableCtrl(true); if (VWConf.Instance.OpenpgmList || VWConf.Instance.OpenpgmTimeList) { DialogResult dr = MessageBox.Show("下列文件生成成功!" + "\n1) 加工程序单:" + pgmListPath + "\n2) 加工时间统计单:" + pgmTimeListPath + "\n\n是否打开程序单/统计单文件查看?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (dr == DialogResult.Yes) { try { if (VWConf.Instance.OpenpgmList) { Process.Start(pgmListPath); } if (VWConf.Instance.OpenpgmTimeList) { Process.Start(pgmTimeListPath); } //Process.Start("Explorer.exe", "/select, " + pgmListPath); } catch { } } } }