//读文件
public static<T> T getFile(String filename, Class<T> cla) throws Exception{
ClassPathResource classPathResource = new ClassPathResource(“template\\ + filename) ;
InputStream in = classPathResource.getInputStream();
Constructor<T> c = cla.getDeclaredConstructor(InputStream.class);
XWPFDocument doc = c.newInstance;
changeText(doc, map);
createTable(generateaaa(code),doc,list1,list2);
map.put(code,"");
byte[] image;
changePic(doc, code, image, map);
}
private String genarateaaa(String code){
return "${" + code + "}";
}
//文字
public static void changeText(XWPFDocument doc, Map<String, String> textMap){
List<XSPFParagrahph> paragraphs = doc.getParagraphs();
for(XSPFParagrahph paragraph : paragrahphs ){
String text = paragraph.getText();
refactorParagraph(paragraph);
if(checkText(text)){
List<XWPFRun > runs = paragraph.getRuns();
for(XWPFRun run : runs){
run.setText(changeValue(run.tostring(), textMap),0);
}
}
}
}
private static void refactorParagraph(XWPFParagrahph p){
int start = -1;
int end = -1;
List<XWPFRUN> runs = p.getRuns();
for(int i = 0;i < run.size(); i ++){
String runText = runs.get(i).tostring();
if(StringUtils.isEmpty(runText)){
continue;
}
if('$' == runText.charAt(0) && '}' == runText.charAt(runText.length() - 1)){
continue;
}
if('$' == runText.charAt(0)){
start = i;
}
if('}' == runText.charAt(runText.length() - 1)){
end = i;
break;
}
}
if(start != -1){
mergeRun(p, start, end );
refactorParagraph(p);
}
}
private static void mergeRun(XWPFParagraph p,int start,int end ){
int removeCount = end-start;
int removeIndex = start + 1;
List<XWPFRun> runs = p.getRuns();
StringBuilder sb = new StringBuilder();
sb.append(runs.get(start).tostring());
for(int i = 0; i < removeCount; i ++){
sb.append(runs.get(removeIndex).tstring());
p.removeRun(removeIndex);
}
runs.get(start).setText(sb.tostring(), 0)
}
private static boolean checkText(String t){
boolean check = false;
if(null!=text&&text.contails("$")){
check = true;
}
return check;
}
private static String changeValue(String value, Map<String, String> textMap){
Set<Map.Entry<String, String>> textSets = textMap.entrySet();
for(Map.Entry<String, String> textSet : textSets){
String key = "${" + textSet.getKey() = "}";
if(null!=value && value.contains(key)){
String s = textSet.getValue();
VALUE = S;
}
}
rerurn value;
}
private static XWPFDocument createTable(String key, XWPFDocument doc,List<String> headers, List<List<String>> dates){
List<XWPFParagraph> paragraphList = doc.getParagraphs();
for(XWPFParagraph paragraph:paragraphList){
List<XWPFRun> runs = paragraph.getRuns();
for(XWPFRun run:runs){
String text = run.getText(0);
if(StringUtil.isNotEmpty(text)&&text.contains(key)){
run.setText(text.replace(key,""),0);
XmlCursor cursor = paragraph.getCTP().newCursor();
XWPFTable table = doc.insertNewTbl(cursor);
int colNum = dates.get(0).size();
int rowNum = dates.size();
for(int i = 0; i < colNum; i++){
XWPFTableRow tabRow = table.getRow(I)==null?table.createRow():table.getRow(i);
for(int k = 0; k < colNum; ++k){
if(tabRow.getCell(k)==null){
tabRow.createCell();
}
}
}
List<XWPFTableRow> rows = table.getRows();
CTTbPr tablePr = table.getCTTbl().addNewTblPr();
CTTblWidth width = tablePr.addNewTblW();
width.setW(BigInteger.valueof(8000));
createSimpleTableNormal(table,dates,headers);
break;
}
}
}
return doc;
}
private static void createSimpleTableNormal(XWPFTable table,List<List<String>> dates,List<String> headers){
CTTbl ttbl = table.getCTTbl();
CTTblPr tblPr = ttbl.getTblPr() = null?ttbl.addNewTblPr():ttbl.getTblPr();
CTTblWidth tblWidth = tblPr.isSetTblW()? tblPr.getTblW():tblPr.addNewTblW();
CTJc cTJc = tblPr.addNewJc();
cTJc.setVal(STJc.Enum.forString("center");
tblWidth.setW(new BigInteger("8000"));
tblWidth.setType(STTblWidth.DXA);
XWPFTableRow firstRow = null;
XWPFTableRow secondRow = null;
XWPFTableCell firstCell=null;
XWPFTableCell secondCell = null;
for(int i = 0; i<dates.size();i++){
firstRow = table.getRow(i);
List<String> date = dates.get(i);
firstRow.setHeight(380);
for(int j=0;j<date.size();j++){
firstCell = firstRow.getCell(j);
int width = j ==1 ? 4500: j ==2 ? 1300 : 950;
setCellTest(firstCell, date.get(j),"FFFFFF",width);
}
}
firstRow = table.insertNewTableRow(0);
secondRow = table.insertNewTableRow(1);
firstRow.setHeight(380);
secondRow.setHeight(380);
for(int j = 0; j < headers.size(); j ++){
String str = headers.get(i);
int width = j ==1 ? 4500: j ==2 ? 1300 : 950;
if(!str.contains("|")){
firstCell = firstRow.addNewTableCell();
secondCell = secondRow.addNewTableCell();
createVSpanCell(firstCell, str, "4682B4", width, STMerge.RESTART);
createVSpanCell(firstCell, "", "4682B4", width, null);
}else{
String[] strArr = str.split("\\|");
firstCell = firstRow.addNewTableCell();
createHSpanCell(firstCell, strArr[0], "4682B4", width, STMerge.RESTART);
for(int i = 0; i < strArr.length-1; i++){
firstCell = firstRow.addNewTableCell();
createHSpanCell(firstCell, "", "4682B4", width, null);
}
for(int i=1; i<strArr.length;i++){
secondCell = secondRow.addNewTableCell();
setCellText(secondCell, strArr[i], "4682B4", width);
}
}
}
}
private static void setCellTest(XWPFTableCell cell, String text,String bgcolor,int width){
CTTc cttc = cell.getCTTc();
CTTcPr cellPr = cttc.addNewTcPr();
cellPr.addNewTcW().setW(BigInteger.valueof(width));
CTTcPr ctpr = cttc.addNewTcPr();
CTShd ctshd = ctPr.addNewShd();
ctshd.setFill(bgcolor);
ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
cttc.addPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
cell.setText(text);
}
private static void createVSpanCell(XWPFTableCell cell, String value, String bgcolor, int width, STMerge.Enum stMerge){
CTTc cttc = cell.getCTTc();
CTTcPr cellPr = cttc.addNewTcPr();
cellPr.addNewTcW().setW(BigInteger.valueof(width));
cell.setColor(bgcolor);
cell.addNewVMerge().setVal(stMerge);
ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
cttc.addPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
cttc.getPList().get(0).addNewR().addNewT().setStringValue(value);
}
private static void createHSpanCell(XWPFTableCell cell, String value, String bgcolor, int width, STMerge.Enum stMerge){
CTTc cttc = cell.getCTTc();
CTTcPr cellPr = cttc.addNewTcPr();
cellPr.addNewTcW().setW(BigInteger.valueof(width));
cell.setColor(bgcolor);
cell.addNewVMerge().setVal(stMerge);
ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
cttc.addPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
cttc.getPList().get(0).addNewR().addNewT().setStringValue(value);
}
//图片
private static void changePic(XWPFDocument doc, byte[] image, Map<String, String> map){
Lisy<XWPFParagraph> ps = doc.getParagraph();
for(XWPFParagraph p : ps){
String text = p.getText();
refactorParagraph(p);
if(text.contails(code)){
XWPFParagraph xwpfParagraph = p;
XWPFRun run = xwpfParagraph.createRun();
changeText(doc, map);
run.addPicture(new ByteArrayInputStream(image), XWPFDocument.PICTURE_TYPE_PNG, "", Units.toEMU(500),Units.toEMU(400));
}
}
}