| | |
| | | */ |
| | | private static void insertData(XWPFTable table, List<String[]> addList) { |
| | | for (int i = 1, c = addList.size(); i < c; i++) { |
| | | XWPFTableRow row = table.createRow(); |
| | | table.createRow(); |
| | | } |
| | | |
| | | List<XWPFTableCell> oldCells = table.getRow(1).getTableCells(); |
| | |
| | | XWPFTableCell newCell = tableCells.get(j); |
| | | |
| | | if (0 == i) { |
| | | newCell.setText(addList.get(i)[j]); |
| | | // newCell.setText(addList.get(i)[j]) |
| | | setCellText(newCell, addList.get(i)[j]); |
| | | } else { |
| | | setCellText(oldCell, newCell, addList.get(i)[j]); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设置单元格文本 |
| | | */ |
| | | private static void setCellText(XWPFTableCell cell, String text) { |
| | | List<XWPFParagraph> paragraphs = cell.getParagraphs(); |
| | | for (XWPFParagraph paragraph : paragraphs) { |
| | | List<XWPFRun> runs = paragraph.getRuns(); |
| | | for (XWPFRun run : runs) { |
| | | run.setText(text, 0); |
| | | } |
| | | } |
| | | } |
| | |
| | | cellR.setText(text); |
| | | |
| | | // 复制字体信息 |
| | | if (tmpR != null) { |
| | | if (!cellR.isBold()) { |
| | | cellR.setBold(tmpR.isBold()); |
| | | } |
| | | cellR.setItalic(tmpR.isItalic()); |
| | | cellR.setUnderline(tmpR.getUnderline()); |
| | | cellR.setColor(tmpR.getColor()); |
| | | cellR.setTextPosition(tmpR.getTextPosition()); |
| | | if (tmpR.getFontSize() != -1) { |
| | | cellR.setFontSize(tmpR.getFontSize()); |
| | | } |
| | | if (tmpR.getFontFamily() != null) { |
| | | cellR.setFontFamily(tmpR.getFontFamily()); |
| | | } |
| | | if (tmpR.getCTR() != null) { |
| | | if (tmpR.getCTR().isSetRPr()) { |
| | | CTRPr tmpRpr = tmpR.getCTR().getRPr(); |
| | | if (tmpRpr.isSetRFonts()) { |
| | | CTFonts tmpFonts = tmpRpr.getRFonts(); |
| | | CTRPr cellRpr = cellR.getCTR().isSetRPr() ? cellR |
| | | .getCTR().getRPr() : cellR.getCTR().addNewRPr(); |
| | | CTFonts cellFonts = cellRpr.isSetRFonts() ? cellRpr |
| | | .getRFonts() : cellRpr.addNewRFonts(); |
| | | cellFonts.setAscii(tmpFonts.getAscii()); |
| | | cellFonts.setAsciiTheme(tmpFonts.getAsciiTheme()); |
| | | cellFonts.setCs(tmpFonts.getCs()); |
| | | cellFonts.setCstheme(tmpFonts.getCstheme()); |
| | | cellFonts.setEastAsia(tmpFonts.getEastAsia()); |
| | | cellFonts.setEastAsiaTheme(tmpFonts.getEastAsiaTheme()); |
| | | cellFonts.setHAnsi(tmpFonts.getHAnsi()); |
| | | cellFonts.setHAnsiTheme(tmpFonts.getHAnsiTheme()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | copyFontInfo(cellR, tmpR); |
| | | |
| | | // 复制段落信息 |
| | | cellP.setAlignment(tmpP.getAlignment()); |
| | |
| | | cellP.setBorderRight(tmpP.getBorderRight()); |
| | | cellP.setBorderTop(tmpP.getBorderTop()); |
| | | cellP.setPageBreak(tmpP.isPageBreak()); |
| | | if (tmpP.getCTP() != null) { |
| | | if (tmpP.getCTP().getPPr() != null) { |
| | | CTPPr tmpPpr = tmpP.getCTP().getPPr(); |
| | | CTPPr cellPpr = cellP.getCTP().getPPr() != null ? cellP.getCTP().getPPr() : cellP.getCTP().addNewPPr(); |
| | | |
| | | // 复制段落间距信息 |
| | | CTSpacing tmpSpacing = tmpPpr.getSpacing(); |
| | | if (tmpSpacing != null) { |
| | | CTSpacing cellSpacing = cellPpr.getSpacing() != null ? cellPpr |
| | | .getSpacing() : cellPpr.addNewSpacing(); |
| | | if (tmpSpacing.getAfter() != null) { |
| | | cellSpacing.setAfter(tmpSpacing.getAfter()); |
| | | } |
| | | if (tmpSpacing.getAfterAutospacing() != null) { |
| | | cellSpacing.setAfterAutospacing(tmpSpacing |
| | | .getAfterAutospacing()); |
| | | } |
| | | if (tmpSpacing.getAfterLines() != null) { |
| | | cellSpacing.setAfterLines(tmpSpacing.getAfterLines()); |
| | | } |
| | | if (tmpSpacing.getBefore() != null) { |
| | | cellSpacing.setBefore(tmpSpacing.getBefore()); |
| | | } |
| | | if (tmpSpacing.getBeforeAutospacing() != null) { |
| | | cellSpacing.setBeforeAutospacing(tmpSpacing |
| | | .getBeforeAutospacing()); |
| | | } |
| | | if (tmpSpacing.getBeforeLines() != null) { |
| | | cellSpacing.setBeforeLines(tmpSpacing.getBeforeLines()); |
| | | } |
| | | if (tmpSpacing.getLine() != null) { |
| | | cellSpacing.setLine(tmpSpacing.getLine()); |
| | | } |
| | | if (tmpSpacing.getLineRule() != null) { |
| | | cellSpacing.setLineRule(tmpSpacing.getLineRule()); |
| | | } |
| | | } |
| | | if (tmpP.getCTP() != null&&tmpP.getCTP().getPPr() != null) { |
| | | CTPPr tmpPpr = tmpP.getCTP().getPPr(); |
| | | CTPPr cellPpr = cellP.getCTP().getPPr() != null ? cellP.getCTP().getPPr() : cellP.getCTP().addNewPPr(); |
| | | |
| | | // 复制段落缩进信息 |
| | | CTInd tmpInd = tmpPpr.getInd(); |
| | | if (tmpInd != null) { |
| | | CTInd cellInd = cellPpr.getInd() != null ? cellPpr.getInd() |
| | | : cellPpr.addNewInd(); |
| | | if (tmpInd.getFirstLine() != null) { |
| | | cellInd.setFirstLine(tmpInd.getFirstLine()); |
| | | } |
| | | if (tmpInd.getFirstLineChars() != null) { |
| | | cellInd.setFirstLineChars(tmpInd.getFirstLineChars()); |
| | | } |
| | | if (tmpInd.getHanging() != null) { |
| | | cellInd.setHanging(tmpInd.getHanging()); |
| | | } |
| | | if (tmpInd.getHangingChars() != null) { |
| | | cellInd.setHangingChars(tmpInd.getHangingChars()); |
| | | } |
| | | if (tmpInd.getLeft() != null) { |
| | | cellInd.setLeft(tmpInd.getLeft()); |
| | | } |
| | | if (tmpInd.getLeftChars() != null) { |
| | | cellInd.setLeftChars(tmpInd.getLeftChars()); |
| | | } |
| | | if (tmpInd.getRight() != null) { |
| | | cellInd.setRight(tmpInd.getRight()); |
| | | } |
| | | if (tmpInd.getRightChars() != null) { |
| | | cellInd.setRightChars(tmpInd.getRightChars()); |
| | | } |
| | | // 复制段落间距信息 |
| | | copySpacing(tmpPpr, cellPpr); |
| | | |
| | | // 复制段落缩进信息 |
| | | copyParagraph(tmpPpr, cellPpr); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 复制字体信息 |
| | | */ |
| | | private static void copyFontInfo(XWPFRun cellR, XWPFRun tmpR) { |
| | | if (tmpR == null) { |
| | | return; |
| | | } |
| | | if (!cellR.isBold()) { |
| | | cellR.setBold(tmpR.isBold()); |
| | | } |
| | | cellR.setItalic(tmpR.isItalic()); |
| | | cellR.setUnderline(tmpR.getUnderline()); |
| | | cellR.setColor(tmpR.getColor()); |
| | | cellR.setTextPosition(tmpR.getTextPosition()); |
| | | if (tmpR.getFontSize() != -1) { |
| | | cellR.setFontSize(tmpR.getFontSize()); |
| | | } |
| | | if (tmpR.getFontFamily() != null) { |
| | | cellR.setFontFamily(tmpR.getFontFamily()); |
| | | } |
| | | if (tmpR.getCTR() != null) { |
| | | if (tmpR.getCTR().isSetRPr()) { |
| | | CTRPr tmpRpr = tmpR.getCTR().getRPr(); |
| | | if (tmpRpr.isSetRFonts()) { |
| | | CTFonts tmpFonts = tmpRpr.getRFonts(); |
| | | CTRPr cellRpr = cellR.getCTR().isSetRPr() ? cellR |
| | | .getCTR().getRPr() : cellR.getCTR().addNewRPr(); |
| | | CTFonts cellFonts = cellRpr.isSetRFonts() ? cellRpr |
| | | .getRFonts() : cellRpr.addNewRFonts(); |
| | | cellFonts.setAscii(tmpFonts.getAscii()); |
| | | cellFonts.setAsciiTheme(tmpFonts.getAsciiTheme()); |
| | | cellFonts.setCs(tmpFonts.getCs()); |
| | | cellFonts.setCstheme(tmpFonts.getCstheme()); |
| | | cellFonts.setEastAsia(tmpFonts.getEastAsia()); |
| | | cellFonts.setEastAsiaTheme(tmpFonts.getEastAsiaTheme()); |
| | | cellFonts.setHAnsi(tmpFonts.getHAnsi()); |
| | | cellFonts.setHAnsiTheme(tmpFonts.getHAnsiTheme()); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 复制段落间距信息 |
| | | */ |
| | | private static void copySpacing(CTPPr tmpPpr, CTPPr cellPpr) { |
| | | CTSpacing tmpSpacing = tmpPpr.getSpacing(); |
| | | if (tmpSpacing != null) { |
| | | CTSpacing cellSpacing = cellPpr.getSpacing() != null ? cellPpr.getSpacing() : cellPpr.addNewSpacing(); |
| | | if (tmpSpacing.getAfter() != null) { |
| | | cellSpacing.setAfter(tmpSpacing.getAfter()); |
| | | } |
| | | if (tmpSpacing.getAfterAutospacing() != null) { |
| | | cellSpacing.setAfterAutospacing(tmpSpacing |
| | | .getAfterAutospacing()); |
| | | } |
| | | if (tmpSpacing.getAfterLines() != null) { |
| | | cellSpacing.setAfterLines(tmpSpacing.getAfterLines()); |
| | | } |
| | | if (tmpSpacing.getBefore() != null) { |
| | | cellSpacing.setBefore(tmpSpacing.getBefore()); |
| | | } |
| | | if (tmpSpacing.getBeforeAutospacing() != null) { |
| | | cellSpacing.setBeforeAutospacing(tmpSpacing |
| | | .getBeforeAutospacing()); |
| | | } |
| | | if (tmpSpacing.getBeforeLines() != null) { |
| | | cellSpacing.setBeforeLines(tmpSpacing.getBeforeLines()); |
| | | } |
| | | if (tmpSpacing.getLine() != null) { |
| | | cellSpacing.setLine(tmpSpacing.getLine()); |
| | | } |
| | | if (tmpSpacing.getLineRule() != null) { |
| | | cellSpacing.setLineRule(tmpSpacing.getLineRule()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 复制段落缩进信息 |
| | | */ |
| | | private static void copyParagraph(CTPPr tmpPpr, CTPPr cellPpr) { |
| | | CTInd tmpInd = tmpPpr.getInd(); |
| | | if (tmpInd != null) { |
| | | CTInd cellInd = cellPpr.getInd() != null ? cellPpr.getInd() : cellPpr.addNewInd(); |
| | | if (tmpInd.getFirstLine() != null) { |
| | | cellInd.setFirstLine(tmpInd.getFirstLine()); |
| | | } |
| | | if (tmpInd.getFirstLineChars() != null) { |
| | | cellInd.setFirstLineChars(tmpInd.getFirstLineChars()); |
| | | } |
| | | if (tmpInd.getHanging() != null) { |
| | | cellInd.setHanging(tmpInd.getHanging()); |
| | | } |
| | | if (tmpInd.getHangingChars() != null) { |
| | | cellInd.setHangingChars(tmpInd.getHangingChars()); |
| | | } |
| | | if (tmpInd.getLeft() != null) { |
| | | cellInd.setLeft(tmpInd.getLeft()); |
| | | } |
| | | if (tmpInd.getLeftChars() != null) { |
| | | cellInd.setLeftChars(tmpInd.getLeftChars()); |
| | | } |
| | | if (tmpInd.getRight() != null) { |
| | | cellInd.setRight(tmpInd.getRight()); |
| | | } |
| | | if (tmpInd.getRightChars() != null) { |
| | | cellInd.setRightChars(tmpInd.getRightChars()); |
| | | } |
| | | } |
| | | } |
| | | } |