| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- package UIB.UIB03.ZBS;
- import java.io.File;
- import java.net.URLDecoder;
- import java.util.Calendar;
- import java.util.HashMap;
- import UIB.COM.QRCodeHandler;
- import jxl.SheetSettings;
- import jxl.write.Label;
- import jxl.write.WritableImage;
- import jxl.write.WritableSheet;
- /**
- *
- * @desc B类模板,元素占据4个单元格
- * @author meiguiping
- * @date 2010 1:51:12 PM
- */
- public class QltyGuaranteeBuilderB implements IQltyGuaranteeBuilder
- {
- private int columns; //总行数
- private int rows; //总列数
- private WritableSheet wSheet;
- // private int fixedChemRow;
- // private int fixedQltyRow;
-
-
- public QltyGuaranteeBuilderB()
- {
- }
-
- public QltyGuaranteeBuilderB(WritableSheet sheet)
- {
- wSheet = sheet;
- columns = wSheet.getColumns();
- rows = wSheet.getRows();
- }
-
- /**
- * 1、填充模板行
- * 2、填充实际值(表头、成分、材质)
- * 3、合并单元格
- * 4、填充备注
- * @author meiguiping
- * @date Sep 20, 2010
- */
- public void buildQltyGuarantee(String tmplNO , HashMap mapHead ,DataRowList[] drlChem , DataRowList[] drlQlty) throws Exception
- {
- //获取配置参数
- QltyTmplParser qtp = new QltyTmplParser();
- HashMap hm = qtp.getParamer();
- if(hm.size() <= 0)
- {
- System.out.println("参数配置出错,请检查!");
- return;
- }
- HashMap proMap = (HashMap)hm.get(tmplNO+"ATT");
- HashMap picMap = (HashMap)hm.get(tmplNO+"PIC");
- HashMap codeMap = (HashMap)hm.get(tmplNO+"CODE");
- //System.out.println("图片========》》"+picMap+"\n"+proMap);
- int maxCoilNo = Integer.parseInt(proMap.get("maxCoilNo").toString()); //质保书最多容纳卷数
- int chemTmplRow = StringFormat.getNumber(proMap.get("chemTmplRow").toString());//成分模板行位置
- int qltyTmplRow = StringFormat.getNumber(proMap.get("qltyTmplRow").toString());//性能模板行位置
-
- int headStartRow = StringFormat.getNumber(proMap.get("headStartRow").toString());//表头开始行
- int headEndRow = StringFormat.getNumber(proMap.get("headEndRow").toString());//表头结束行
-
- int chemCDRow = StringFormat.getNumber(proMap.get("chemCDRow").toString());//元素名填充的行
- int chemValStartRow = StringFormat.getNumber(proMap.get("chemValStartRow").toString());//成分值开始填充行
- int chemValStartCol = StringFormat.getNumber(proMap.get("chemValStartCol").toString());//成分开始列
-
- int qltyValStartRow = StringFormat.getNumber(proMap.get("qltyValStartRow").toString());//性能开始行
- int qltyValStartCol = StringFormat.getNumber(proMap.get("qltyValStartCol").toString());//性能开始列
- int qltyGigNameRow = StringFormat.getNumber(proMap.get("qltyGigNameRow").toString());//性能大名称开始行
- int qltySmallNameRow = StringFormat.getNumber(proMap.get("qltySmallNameRow").toString());//性能小名称开始行
- int qltyExtCodeRow = StringFormat.getNumber(proMap.get("qltyExtCodeRow").toString());//性能附加信息填充行
-
- int rmkStartRow = StringFormat.getNumber(proMap.get("rmkStartRow").toString());//备注开始行
- int rmkStartCol = StringFormat.getNumber(proMap.get("rmkStartCol").toString());//备注开始列
-
- int tabulatorRow = StringFormat.getNumber(proMap.get("tabulatorRow").toString());//质表人开始行
- int tabulatorCol = StringFormat.getNumber(proMap.get("tabulatorCol").toString());//质表人开始列
-
- int cretNoDescRow = StringFormat.getNumber(proMap.get("cretNoDescRow").toString());//描述行
- int cretNoDescCol = StringFormat.getNumber(proMap.get("cretNoDescCol").toString());//描述列
-
- if(drlChem == null) throw new Exception("质保书异常,缺少成分记录。");
-
- int coilCount = drlChem.length; //SQL查询出来的实际卷数
- if(maxCoilNo < coilCount) throw new Exception("质保书最大容纳卷数小于实际质保书卷数");
- if(maxCoilNo >8 || maxCoilNo < 4) throw new Exception("一张质保书卷数应在4~8卷之间");
- ///补充拉力项目
- if(drlQlty.length != 0)
- {
- RowSetConverter.replenishPullItem(drlQlty[0]);//补充拉力项目
- }
- ///填充模板行
- setChemTmpl( drlChem[0] , chemTmplRow , chemValStartCol);//增加行操作对于元素代码填充行无影响,元素代码填充
- if(drlQlty.length != 0)
- {
- setQltyTmpl( drlQlty[0] , qltyTmplRow , qltyValStartCol , qltySmallNameRow );//模板行(材质)填充
- }
- ///填充实绩值
- setHeader( mapHead ,picMap , codeMap,headStartRow , headEndRow );//质保书表头填充
- setChem(drlChem , chemValStartRow , chemTmplRow , chemCDRow , proMap);//成分填充
- if(drlQlty.length != 0)
- {
- setQlty(drlQlty , qltyValStartRow , qltyTmplRow , proMap);//材质填充
- }
- //合并单元格
- if(drlQlty.length != 0)//无性能不考虑合并问题
- {
- mergeQltyCells(qltySmallNameRow);//材质小名称行
- mergeQltyCells(qltyGigNameRow);//材质大名称行
- mergeQltyCellsColumn(qltyGigNameRow , columns);
- }
-
- ///填充备注,制表人
-
- wSheet.addCell( new Label(rmkStartCol, rmkStartRow, mapHead.get("CRET_NO_RMK")==null?"": mapHead.get("CRET_NO_RMK").toString(),wSheet.getCell(rmkStartCol, rmkStartRow).getCellFormat() ) );
- wSheet.addCell( new Label(tabulatorCol, tabulatorRow, mapHead.get("USERID")==null?"": mapHead.get("USERID").toString(),wSheet.getCell(tabulatorCol, tabulatorRow).getCellFormat() ) );
- //wSheet.addCell( new Label(qltyValStartRow+drlQlty.length, qltyValStartRow+drlQlty.length, mapHead.get("CRET_NO_RMK")==null?"": mapHead.get("CRET_NO_RMK").toString(),wSheet.getCell(rmkStartCol, rmkStartRow).getCellFormat() ) );
- //wSheet.addCell( new Label(tabulatorCol, tabulatorRow, mapHead.get("USERID")==null?"": mapHead.get("USERID").toString(),wSheet.getCell(tabulatorCol, tabulatorRow).getCellFormat() ) );
-
- //质保书最后描述的填充
- //wSheet.addCell(new Label(cretNoDescCol , cretNoDescRow , mapHead.get("CRET_DESC") == null?"":mapHead.get("CRET_DESC").toString() ,wSheet.getCell(cretNoDescCol, cretNoDescRow).getCellFormat() ));
-
- if("B00005".equals(tmplNO))//军工模板特殊处理
- {
- String gradetype = mapHead.get("SPEC_STL_GRD").toString();
- gradetype=gradetype.substring(3);
- //动态获取军工的标准(CCSA牌号则获取A,CCS36则获取36,以此类推)
- wSheet.addCell(new Label(20 , 33 ,gradetype,wSheet.getCell(20, 33).getCellFormat() ));
- wSheet.addCell(new Label(25 , 34 ,gradetype,wSheet.getCell(25, 34).getCellFormat() ));
- }else
- {
- //质保书最后描述的填充,军工质保书固定描述,其他质保书动态填充
- wSheet.addCell(new Label(cretNoDescCol , cretNoDescRow , mapHead.get("CRET_DESC") == null?"":mapHead.get("CRET_DESC").toString() ,wSheet.getCell(cretNoDescCol, cretNoDescRow).getCellFormat() ));
- }
-
-
- // setImage(picMap);
- ///打印设置
- SheetSettings sheetSettings = wSheet.getSettings();
- // sheetSettings.setFitHeight(fh);
- // sheetSettings.setFitWidth(fw);//
- sheetSettings.setScaleFactor(100);//百分比形式
-
- ///删除模板行
- wSheet.removeRow(chemTmplRow);
- wSheet.removeRow(qltyTmplRow-1);
- }
-
- /**
- * 模板行的成分信息填充
- * @param tmplRow 原模板上录入成分实绩的行在当前操作副本上的行数,也就是成分开始的行
- * @param chemValStartCol 成分开始列
- */
- public void setChemTmpl(DataRowList drl , int tmplRow , int chemValStartCol) throws Exception
- {
- String key = "";
- String value = "";
- String extCode = "";
- for(int index =0 ; index < drl.size(); index++)
- {
- key = drl.getKey(index).toString();
- value = drl.getValue(index).toString();
- extCode = drl.getExtCode(index).toString();
- if("".equals(extCode))continue;//只填充元素名称
-
- for(int col = chemValStartCol ; col < columns; col++)
- {
- if( "".equals(wSheet.getCell(col, tmplRow).getContents().trim()))//判断是否已填充,只有成分元素才填充
- {
- wSheet.addCell(new Label(col , tmplRow, key /*, ExcelStyleSeter.getChemValuetyle(),wSheet.getCell(col, tmplRow).getCellFormat()*/));//模板行
- break;//因为首先是按元素名循环,所以元素被填充后,需要进入大循环的下一个元素循环
- }
- }
- }
- }
-
- /**
- * 模板行的材质信息填充
- * @param tmplRow 材质模板行
- * @param qltyValStartCol 材质开始列
- * @param qltySmallNameRow材质小名称开始行
- */
- public void setQltyTmpl(DataRowList drl , int tmplRow , int qltyValStartCol , int qltySmallNameRow ) throws Exception
- {
- String key = "";
- String smallName = "";
- String bigName = "";
- String extCode = "";
- for(int index =0 ; index < drl.size(); index++)
- {
- key = drl.getKey(index).toString();
- // value = drl.getValue(index).toString();
- smallName = drl.getSmallName(index).toString().replaceAll("&", "\n");//小名称不可以为空,所以不判断,若为空则直接抛出异常
- bigName = drl.getBigName(index)==null?"":drl.getBigName(index).toString().replaceAll("&", "\n");
- extCode = drl.getExtCode(index)==null?"":drl.getExtCode(index).toString();
- for(int col = qltyValStartCol ; col < columns; col++)
- {
- if( "".equals(wSheet.getCell(col, tmplRow).getContents().trim())
- && !"".equals(smallName) )//判断是否已填充,只有材质、位置等才填充,对钢卷号等信息不填充
- {
- wSheet.addCell(new Label(col , qltySmallNameRow-1 , bigName , ExcelStyleSeter.getQltyNameStyle() ));//大名称
- wSheet.addCell(new Label(col , qltySmallNameRow , smallName , ExcelStyleSeter.getQltyNameStyle() ));//材质名称行
- wSheet.addCell(new Label(col , qltySmallNameRow+1 , extCode , ExcelStyleSeter.getQltyNameStyle() ));//补充码
- //wSheet.addCell(new Label(col , qltySmallNameRow+2 , extCode , ExcelStyleSeter.getQltyNameStyle() ));//补充码
- wSheet.addCell(new Label(col , tmplRow , key , ExcelStyleSeter.getChemValuetyle()/*,wSheet.getCell(col, 16).getCellFormat()*/));//模板行
- break;//某一列被填充后退出列的循环,进入下一个元素的循环。否则第一个元素将填满模板
- }
- }
- }
- }
-
- /**
- * @desc 表头填充
- * @param map 表头数据如合同号均在该map中
- * @param picMap 存储图片
- * @param startRow 表头数据开始填充行
- * @param endRow 表头数据结束填充行
- * @throws Exception
- */
- public void setHeader(HashMap map , HashMap picMap ,HashMap codeMap , int startRow , int endRow) throws Exception
- {
- String key = "";
- //对表头所有的单元格进行循环,获取需要的质保书数据要求
- for(int row = 0 ; row <= endRow; row++)
- {
- for(int col = 0 ; col <= columns; col++)
- {
- key = wSheet.getCell(col, row).getContents().trim();//获取EXCEL中的文本内容
- if(key.equals("")||!key.startsWith("["))//EXCEL中内容为空,或者不以[开头的,则进入下个循环处理
- continue;
- key = key.replaceFirst("\\[", "").replaceFirst("\\]", "").trim();//将EXCEL中用"[]"括起来的内容提取出来
- if(map.get(key) == null)//map中可能不包含所要求的内容,进入下个循环。
- {
- wSheet.addCell(new Label(col , row , "" ,wSheet.getCell(col, row).getCellFormat()));//若无内容,则填入空
- }
- else
- {
- wSheet.addCell(new Label(col , row , map.get(key).toString() ,wSheet.getCell(col, row).getCellFormat()));
- }
- }
- }
- //增加图片
- Object pic1 = map.get("PIC1");//20180323解决图片无法读取---wl
- // Object pic2 = map.get("PIC2");
- if(pic1 != null)
- {
- // if(map == null)return;
- pic1=pic1+".png";
- double col = Double.parseDouble(picMap.get("imgStartCol").toString());//StringFormat.getNumber(picMap.get("imgStartCol").toString());;
- double row = Double.parseDouble(picMap.get("imgStartrow").toString());//StringFormat.getNumber(picMap.get("imgStartrow").toString());;
- double width = Double.parseDouble(picMap.get("imgWidth").toString());
- double height = Double.parseDouble(picMap.get("imgHeight").toString());
- // String filePath= Thread.currentThread().getContextClassLoader().getResource("").getPath()+map.get("name").toString();
- // String filePath= Thread.currentThread().getContextClassLoader().getResource("").getPath()+"excelConfig/"+pic1;
- String filePath= this.getClass().getClassLoader().getResource("/").getPath().replaceFirst("classes/", "")+"excelConfig/"+pic1;
- filePath =URLDecoder.decode(filePath,"utf-8");
- File file = new File(filePath);
- WritableImage wi = new WritableImage(col,row,width,height , file);
- wSheet.addImage(wi);
- }
- //增加二维码图片
- Object pic3 = map.get("PIC3");
- if(pic3 != null)
- {
- // if(map == null)return;
- //获取当前日期
- Calendar c = Calendar.getInstance();
- String p = pic3.toString();
- p = p.substring(0, 2)+c.get(Calendar.YEAR)+"-"+c.get(Calendar.MONTH)+"-"+c.get(Calendar.DATE)+"_"+c.get(Calendar.HOUR_OF_DAY)+"-"+c.get(Calendar.MINUTE)+"-"+c.get(Calendar.SECOND)+p.substring(2, 6);
- double col = Double.parseDouble(codeMap.get("imgStartCol").toString());//StringFormat.getNumber(picMap.get("imgStartCol").toString());;
- double row = Double.parseDouble(codeMap.get("imgStartrow").toString());//StringFormat.getNumber(picMap.get("imgStartrow").toString());;
- double width = Double.parseDouble(codeMap.get("imgWidth").toString());
- double height = Double.parseDouble(codeMap.get("imgHeight").toString());
- // String filePath= Thread.currentThread().getContextClassLoader().getResource("").getPath()+map.get("name").toString();
- // String filePath= Thread.currentThread().getContextClassLoader().getResource("").getPath()+"excelConfig/"+pic1;
- String filePath= this.getClass().getClassLoader().getResource("/").getPath().replaceFirst("classes/", "")+"excelConfig/"+p;
- filePath =URLDecoder.decode(filePath,"utf-8");
- //截取标准号,二维码里面暂时不显示精度
- String abbsym = map.get("SPEC_ABBSYM").toString();
- if(abbsym.indexOf(";")>0){
- abbsym = abbsym.substring(0,abbsym.indexOf(";"));
- }
- StringBuffer context =new StringBuffer();
- context.append("制造商:新余钢铁股份有限公司\n");
- context.append("牌号:").append(map.get("SPEC_STL_GRD")).append("\n");
- context.append("标准号:").append(abbsym).append("\n");
- context.append("交货状态:").append(map.get("PRDNM_CD")).append("\n");
- context.append("发货日期:").append(map.get("SHIP_DIR_DTIME")).append("\n");
- context.append("合同号:").append(map.get("ORD_NO")).append("\n");
- context.append("证明书编号:").append(map.get("CRET_NO")).append("\n");
-
- //生成二维码
- QRCodeHandler.QRCodeEncoder(context.toString(), filePath);
- //将二维码添加到模板
- File file = new File(filePath);
- WritableImage wi = new WritableImage(col,row,width,height , file);
- wSheet.addImage(wi);
- //保存二维码图片地址以便后面删除.
- map.put("cdPath", filePath);
- }
- }
-
- /**
- * @param chemValStartRow 成分实绩值填充起始行
- * @param tmplRow 成分模板行
- * @param chemCDRow 成分代码填充行(C、Si等名称)
- */
- public void setChem(DataRowList[] drl , int chemValStartRow , int tmplRow , int chemCDRow , HashMap map) throws Exception
- {
- int currentRow = chemValStartRow;
- String labelContent = "";//模板中的内容
- String afterLabelContent = "";
-
- int chemFlag = 0;
- for(int i = 0; i < drl.length; i++)
- {
- chemFlag = 0;
- //元素前的钢卷号等信息填充
- for(int col = 0 ; col < columns; col++)//元素前的钢卷号等信息填充
- {
- labelContent = wSheet.getCell(col , tmplRow).getContents().trim();
- if("".equals(labelContent))//若模板中成分行的内容为空
- continue;
- if(drl[i].getValue(labelContent)==null)//为null则进入下次循环
- continue;
- if("".equals(drl[i].getExtCode(labelContent)))//填充钢卷信息
- {
- wSheet.addCell( new Label(col , currentRow+i*2 , drl[i].getValue(labelContent).toString() , wSheet.getCell(col, tmplRow).getCellFormat()/*ExcelStyleSeter.getCommStyle()*/) );
- }
- else//填充元素值信息
- {
- if(chemFlag%2==0)
- {
- afterLabelContent = wSheet.getCell(col+1 , tmplRow).getContents().trim();
- //元素名
- wSheet.addCell(new Label(col , chemCDRow , labelContent , ExcelStyleSeter.getChemNametyle()) );
- wSheet.addCell(new Label(col , chemCDRow+2 , afterLabelContent , ExcelStyleSeter.getChemNametyle()) );
- //元素值
- wSheet.addCell(new Label(col , currentRow+i*2 , drl[i].getValue(labelContent)==null?"":drl[i].getValue(labelContent).toString() , ExcelStyleSeter.getChemValuetyle()) );
- wSheet.addCell(new Label(col , currentRow+i*2+1 , drl[i].getValue(afterLabelContent)==null?"":drl[i].getValue(afterLabelContent).toString() , ExcelStyleSeter.getChemValuetyle()) );
- }
- chemFlag++;
- }
- }
- }
-
- }
-
- /**
- *
- * @param drl
- * @param qltyValStartRow 材质实绩值填充起始行
- * @param tmplRow 材质模板行
- * @throws Exception
- */
- public void setQlty(DataRowList[] drl , int qltyValStartRow , int tmplRow , HashMap map) throws Exception
- {
- Label label = null;
- String labelContent = "";//模板中的内容
- for(int i = 0; i < drl.length; i++)
- {
- for(int col = 0 ; col < columns ; col++)
- {
- labelContent = wSheet.getCell(col, tmplRow).getContents().trim();
- if("".equals(labelContent))//为空则进行下一次循环
- continue;
- if(drl[i].getValue(labelContent) == null)//DataRowList中可能不包含所要求的内容,进入下个循环。
- continue;
- label = new Label( col,qltyValStartRow+i ,drl[i].getValue(labelContent).toString() ,wSheet.getCell(col, tmplRow).getCellFormat()/*ExcelStyleSeter.getQltyValueStyle()*/);
- wSheet.addCell(label);
- }
- }
- }
- /**
- * @desc 插入图片
- * @param map
- * @throws Exception
- */
- public void setImage(HashMap map )throws Exception
- {
- if(map == null)return;
- int col = StringFormat.getNumber(map.get("imgStartCol").toString());;
- int row = StringFormat.getNumber(map.get("imgStartrow").toString());;
- double width = Double.parseDouble(map.get("imgWidth").toString());
- double height = Double.parseDouble(map.get("imgHeight").toString());
- String filePath= Thread.currentThread().getContextClassLoader().getResource("").getPath()+map.get("name").toString();
- File file = new File(filePath);
- WritableImage wi = new WritableImage(col,row,width,height , file);
- wSheet.addImage(wi);
- }
-
- /**
- * @param currentRow 需要合并的行
- * @throws Exception
- */
- public void mergeQltyCells(int currentRow) throws Exception
- {
- String labelContent = "";
- String labelContentBefore = "";
- int count = 0;
- for(int col = 0; col <= columns; col++)
- {
- labelContent = wSheet.getCell(col , currentRow).getContents().trim();//当前单元格的值
- labelContentBefore = wSheet.getCell(col-1 , currentRow).getContents().trim();//之前一个单元格的值
- if(!labelContentBefore.equals(labelContent) && !"".equals(labelContentBefore) )//
- {
- if(count == 0)
- continue;
- wSheet.mergeCells((col-1)-count, currentRow, col-1, currentRow );
- count = 0;//合并后,重新归0
-
- }
- else if(!"".equals(labelContent) && !"".equals(labelContentBefore) )
- {
- count++;
- }
- }
- }
-
- /**
- * @desc 合同列,主要针对材质大名称、小名称的合并
- * @param currentRow
- */
- private void mergeQltyCellsColumn(int currentRow , int cols) throws Exception
- {
- String shangValue;
- String xiaValue;
- for(int i = 0; i < cols; i++)
- {
- shangValue = wSheet.getCell(i , currentRow).getContents().toString();
- xiaValue = wSheet.getCell(i , currentRow+1).getContents().toString();
- if(shangValue.equals(xiaValue) && !"".equals(shangValue))
- {
- wSheet.mergeCells( i, currentRow , i , currentRow+1);
- }
- }
- }
-
- }
|