public void PrintForTable(string userId, DataTable _datatable, string machineId, string filepath) {//传进来的datatable需要进过排序 ClsAllExcel excel = new ClsAllExcel(); if (System.IO.Directory.Exists(filepath)) { if (!System.IO.File.Exists(string.Format(@"{0}\print.xlt", filepath))) { mFrm.ShowPrgMsg("模板没有找到,正在从Ftp下载..", MainParent.MsgType.Warning); FrmBLL.Ftp_MyFtp ftp = new FrmBLL.Ftp_MyFtp(); ftp.DownloadFile("print.xlt", System.AppDomain.CurrentDomain.BaseDirectory + "excel", "print.xlt"); mFrm.ShowPrgMsg("下载完成", MainParent.MsgType.Outgoing); //throw new Exception("备料表打印模板文件不存在"); } } else { throw new Exception("打印模板文件路错误(请在程序运行目录下建立\"Excel\"文件夹"); } excel.OpenFileName = string.Format(@"{0}\print.xlt", filepath);// :"D:\BOM\New\print.xlt"; excel.OpenExcelFile(); int rowindex = 10; excel.setOneCellValue(2, 6, string.Format("*{0} {1}*", _datatable.Rows[0]["masterId"].ToString(), _datatable.Rows[0]["woId"].ToString())); excel.setOneCellValue(3, 6, string.Format("{0} {1}", _datatable.Rows[0]["masterId"].ToString(), _datatable.Rows[0]["woId"].ToString())); excel.setOneCellValue(3, 2, _datatable.Rows[0]["woId"].ToString()); excel.setOneCellValue(3, 3, _datatable.Rows[0]["modelname"].ToString()); excel.setOneCellValue(4, 2, _datatable.Rows[0]["partnumber"].ToString()); excel.setOneCellValue(4, 4, _datatable.Rows.Count.ToString()); excel.setOneCellValue(5, 4, _datatable.Rows[0]["side"].ToString()); excel.setOneCellValue(6, 4, _datatable.Rows[0]["bomver"].ToString()); excel.setOneCellValue(5, 9, userId); excel.setOneCellValue(6, 9, FrmBLL.ReleaseData.arrByteToDataTable(refWebtUserInfo.Instance.GetUserInfo(userId, null, null)).Rows[0]["username"].ToString()); excel.setOneCellValue(5, 6, this.sEditUser); if (!string.IsNullOrEmpty(this.sEditUser)) { excel.setOneCellValue(6, 6, FrmBLL.ReleaseData.arrByteToDataTable(refWebtUserInfo.Instance.GetUserInfo(this.sEditUser, null, null)).Rows[0]["username"].ToString()); } excel.setOneCellValue(5, 7, this.sCheckedUser); if (!string.IsNullOrEmpty(this.sCheckedUser)) { excel.setOneCellValue(6, 7, FrmBLL.ReleaseData.arrByteToDataTable(refWebtUserInfo.Instance.GetUserInfo(this.sCheckedUser, null, null)).Rows[0]["username"].ToString()); } excel.setOneCellValue(6, 2, machineId); for (int z = 0; z < _datatable.Rows.Count; z++)// (DataRow dr in _datatable.Rows) { excel.setOneCellValue(rowindex, 1, _datatable.Rows[z]["stationno"].ToString()); excel.setOneCellValue(rowindex, 2, _datatable.Rows[z]["kpnumber"].ToString()); excel.setOneCellValue(rowindex, 3, _datatable.Rows[z]["kpdesc"].ToString()); excel.setOneCellValue(rowindex, 10, _datatable.Rows[z]["feedertype"].ToString()); string[] arrStr = getstring(_datatable.Rows[z]["localtion"].ToString().Split(','), 4).Split('|'); for (int y = 0; y < arrStr.Length; y++) { if (!string.IsNullOrEmpty(arrStr[y])) { excel.setOneCellValue(rowindex, 6, arrStr[y]); rowindex++; } } if (!(z + 1 == _datatable.Rows.Count)) { //料站相同但是物料组不同的 if ((_datatable.Rows[z]["stationno"].ToString().ToUpper() == _datatable.Rows[z + 1]["stationno"].ToString().ToUpper()) && ((string.IsNullOrEmpty(_datatable.Rows[z]["replacegroup"].ToString().ToUpper()) ? "A" : _datatable.Rows[z]["replacegroup"].ToString().ToUpper()) != (string.IsNullOrEmpty(_datatable.Rows[z + 1]["replacegroup"].ToString().ToUpper()) ? "B" : _datatable.Rows[z + 1]["replacegroup"].ToString().ToUpper()))) { throw new Exception(string.Format("物料替代关系错误:{0}与{1}不存在替代关系,或料站表制作有误.请检查", _datatable.Rows[z]["kpnumber"].ToString(), _datatable.Rows[z + 1]["kpnumber"].ToString())); } if (_datatable.Rows[z]["stationno"].ToString().ToUpper() == _datatable.Rows[z + 1]["stationno"].ToString().ToUpper() && _datatable.Rows[z]["replacegroup"].ToString().ToUpper() == _datatable.Rows[z + 1]["replacegroup"].ToString().ToUpper()) { excel.setOneCellValue(rowindex, 2, _datatable.Rows[z + 1]["kpnumber"].ToString()); excel.setOneCellValue(rowindex, 3, _datatable.Rows[z + 1]["kpdesc"].ToString()); rowindex++; z++; } } excel.setOneCellValue(rowindex, 1, "----------------------------------------------------------------------------------------------------------------------------------------------"); rowindex++; } excel.SaveFileName = string.Format(@"{2}\{0}_{1}.xls", _datatable.Rows[0]["woId"].ToString(), machineId, filepath); excel.SaveAsExcel(true); excel.CloseExcelApplication(); }
private void mbt_OpenExcel_Click(object sender, EventArgs e) { try { dicnames = new Dictionary <string, string>(); OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "选择料站表"; ofd.Filter = "(*.xls Excel 2003)|*.xls"; //ofd.InitialDirectory = "c:\\"; DialogResult dlr = ofd.ShowDialog(); if (dlr == DialogResult.Yes || dlr == DialogResult.OK) { this.mExcleFile = ofd.FileName; //在打开Excel文档时就去判断文档内容是否符合规则要求 List <string> lsnames = ClsReadExcel.GetTableNames(ClsReadExcel.FileExists(this.mExcleFile)); DataTable mdatatable = new DataTable(); string side = string.Empty; foreach (string item in lsnames) { side = "T";// item.Split('-')[1].Substring(0, 1); if (item.Split(' ').Length < 2) { throw new Exception("料站表的命名规则不符,请重新修正.."); } if (string.IsNullOrEmpty(side)) { throw new Exception("文档命名不符合规则(\"产品名称-PCB版面+F\")"); } //if ("T" != side && "B" != side) //throw new Exception("文档命名不符合规则(\"产品名称-PCB版面+F\")"); //添加Feeder类型 string sql = string.Format("select 产品料号,产品描述,机器编号,BOM版本,料站总数,count(*) as 料站数 from [{0}] group by 产品料号,产品描述,机器编号,BOM版本,料站总数 having 机器编号 is not null", item); //string sql = string.Format("select 产品料号,产品描述,机器编号,BOM版本,料站总数,count(*) as 料站数 from [{0}] group by 产品料号,产品描述,机器编号,BOM版本,料站总数 having 机器编号 is not null", // item); mdatatable = ClsReadExcel.getTableForSql(ClsReadExcel.FileExists(this.mExcleFile), sql); this.dicnames.Add(item.Replace('\'', ' ').Replace('$', ' ').Trim(), item); if (mdatatable.Rows.Count > 2) { if (this.ShowMsg("一条生产线的参数设置过多\n默认只支持左边、右边,请重新设置..")) { throw new Exception("一条生产线的参数设置过多,默认只支持左边、右边,请检查料表设置.."); } } } //this.dgv_had.DataSource = mdatatable; // this.dgv_had.Columns["料站数"].Visible = false; this.ip_linenamelist.Items.Clear(); foreach (string item in this.dicnames.Keys) { this.ip_linenamelist.Items.Add(new DevComponents.DotNetBar.CheckBoxItem(item, item)); this.ip_linenamelist.Refresh(); } //将料站表上传FTP try { FrmBLL.Ftp_MyFtp ftp = new FrmBLL.Ftp_MyFtp(); ftp.PutImage(ofd.FileName); } catch (Exception ex) { this.mainfrm.ShowPrgMsg(ex.Message, MainParent.MsgType.Warning); } } } catch (Exception ex) { this.mainfrm.ShowPrgMsg(ex.Message, MainParent.MsgType.Error); } }