|
|
@@ -4,9 +4,10 @@ import java.io.IOException;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
|
|
|
import org.apache.poi.xwpf.usermodel.*;
|
|
|
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
|
|
|
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;
|
|
|
+import org.apache.xmlbeans.impl.xb.xmlschema.SpaceAttribute;
|
|
|
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
@@ -35,6 +36,16 @@ public class ExportWord {
|
|
|
XWPFDocument doc = new XWPFDocument();
|
|
|
createTitleParagraph(doc);
|
|
|
createTableParagraph(doc, row, cols);
|
|
|
+//测试页眉设置
|
|
|
+// CTP ctp = CTP.Factory.newInstance();
|
|
|
+// XWPFParagraph paragraph = new XWPFParagraph(ctp, doc);//段落对象
|
|
|
+// ctp.addNewR().addNewT().setStringValue("华丽的测试页眉2019051488888888");//设置页眉参数
|
|
|
+// ctp.addNewR().addNewT().setSpace(SpaceAttribute.Space.PRESERVE);
|
|
|
+// CTSectPr sectPr = doc.getDocument().getBody().isSetSectPr() ? doc.getDocument().getBody().getSectPr() : doc.getDocument().getBody().addNewSectPr();
|
|
|
+// XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(doc, sectPr);
|
|
|
+// XWPFHeader header = policy.createHeader(STHdrFtr.DEFAULT, new XWPFParagraph[] { paragraph });
|
|
|
+// header.setXWPFDocument(doc);
|
|
|
+
|
|
|
return doc;
|
|
|
}
|
|
|
|
|
|
@@ -48,9 +59,9 @@ public class ExportWord {
|
|
|
titleParagraph.setAlignment(ParagraphAlignment.CENTER);//样式居中
|
|
|
XWPFRun titleFun = titleParagraph.createRun(); //创建文本对象
|
|
|
// titleFun.setText(titleName); //设置标题的名字
|
|
|
-// titleFun.setBold(true); //加粗
|
|
|
+ titleFun.setBold(true); //加粗
|
|
|
// titleFun.setColor("000000");//设置颜色
|
|
|
- titleFun.setFontSize(14); //字体大小
|
|
|
+ titleFun.setFontSize(16); //字体大小
|
|
|
titleFun.setFontFamily("宋体");//设置字体
|
|
|
//...
|
|
|
// titleFun.addBreak(); //换行
|
|
|
@@ -65,6 +76,8 @@ public class ExportWord {
|
|
|
// xwpfHelperTable.createTable(xdoc, rowSize, cellSize, isSetColWidth, colWidths)
|
|
|
XWPFTable infoTable = document.createTable(rows, cols);
|
|
|
xwpfHelperTable.setTableWidthAndHAlign(infoTable, "8500", STJc.CENTER);
|
|
|
+ int[] COLUMN_WIDTHS = new int[]{2500, 6000};
|
|
|
+ xwpfHelperTable.setTableWidth(infoTable, COLUMN_WIDTHS);
|
|
|
//合并表格
|
|
|
// xwpfHelperTable.mergeCellsHorizontal(infoTable, 1, 1, 5);
|
|
|
// xwpfHelperTable.mergeCellsVertically(infoTable, 0, 4, 9);
|
|
|
@@ -75,17 +88,18 @@ public class ExportWord {
|
|
|
List<XWPFTableRow> rowList = infoTable.getRows();
|
|
|
for (int i = 0; i < rowList.size(); i++) {
|
|
|
XWPFTableRow infoTableRow = rowList.get(i);
|
|
|
+ infoTableRow.setHeight(200);
|
|
|
List<XWPFTableCell> cellList = infoTableRow.getTableCells();
|
|
|
for (int j = 0; j < cellList.size(); j++) {
|
|
|
XWPFParagraph cellParagraph = cellList.get(j).getParagraphArray(0);
|
|
|
cellParagraph.setAlignment(ParagraphAlignment.LEFT);
|
|
|
cellParagraph.setIndentationLeft(50);
|
|
|
XWPFRun cellParagraphRun = cellParagraph.createRun();
|
|
|
- cellParagraphRun.setFontSize(11);
|
|
|
+ cellParagraphRun.setFontSize(12);
|
|
|
cellParagraphRun.setFontFamily("宋体");
|
|
|
-// if (i % 2 != 0) {
|
|
|
-// cellParagraphRun.setBold(true);
|
|
|
-// }
|
|
|
+ if (j % 2 == 0) {
|
|
|
+ cellParagraphRun.setBold(true);
|
|
|
+ }
|
|
|
}
|
|
|
//倒数第二格加五个回车
|
|
|
if (i == rowList.size() - 2) {
|
|
|
@@ -94,7 +108,7 @@ public class ExportWord {
|
|
|
cellParagraph.setAlignment(ParagraphAlignment.LEFT);
|
|
|
cellParagraph.setIndentationLeft(50);
|
|
|
XWPFRun cellParagraphRun = cellParagraph.createRun();
|
|
|
- cellParagraphRun.setFontSize(11);
|
|
|
+ cellParagraphRun.setFontSize(12);
|
|
|
cellParagraphRun.setFontFamily("宋体");
|
|
|
cellParagraphRun.addBreak();
|
|
|
cellParagraphRun.addBreak();
|
|
|
@@ -104,16 +118,16 @@ public class ExportWord {
|
|
|
}
|
|
|
}
|
|
|
//倒数第一格右对齐
|
|
|
- if (i == rowList.size() - 1) {
|
|
|
- for (int j = 0; j < cellList.size(); j++) {
|
|
|
- XWPFParagraph cellParagraph = cellList.get(j).getParagraphArray(0);
|
|
|
- cellParagraph.setAlignment(ParagraphAlignment.RIGHT);
|
|
|
- cellParagraph.setIndentationRight(100);
|
|
|
- XWPFRun cellParagraphRun = cellParagraph.createRun();
|
|
|
- cellParagraphRun.setFontSize(11);
|
|
|
- cellParagraphRun.setFontFamily("宋体");
|
|
|
- }
|
|
|
- }
|
|
|
+// if (i == rowList.size() - 1) {
|
|
|
+//// for (int j = 0; j < cellList.size(); j++) {
|
|
|
+//// XWPFParagraph cellParagraph = cellList.get(j).getParagraphArray(0);
|
|
|
+//// cellParagraph.setAlignment(ParagraphAlignment.RIGHT);
|
|
|
+//// cellParagraph.setIndentationRight(100);
|
|
|
+//// XWPFRun cellParagraphRun = cellParagraph.createRun();
|
|
|
+//// cellParagraphRun.setFontSize(12);
|
|
|
+//// cellParagraphRun.setFontFamily("宋体");
|
|
|
+//// }
|
|
|
+//// }
|
|
|
}
|
|
|
// xwpfHelperTable.mergeCellsBordersVertically(infoTable, 0, 4, 9);
|
|
|
xwpfHelperTable.setTableHeight(infoTable, 560, STVerticalJc.CENTER);
|
|
|
@@ -147,6 +161,16 @@ public class ExportWord {
|
|
|
List<List<Object>> tableData = (List<List<Object>>) dataList.get("TABLEDATA");
|
|
|
XWPFTable table = document.getTableArray(0);
|
|
|
fillTableData(table, tableData);
|
|
|
+ //测试页眉设置
|
|
|
+ CTP ctp = CTP.Factory.newInstance();
|
|
|
+ XWPFParagraph paragraph_ = new XWPFParagraph(ctp, document);//段落对象
|
|
|
+ ctp.addNewR().addNewT().setStringValue("华丽的测试页眉2019051488888888");//设置页眉参数
|
|
|
+ ctp.addNewR().addNewT().setSpace(SpaceAttribute.Space.PRESERVE);
|
|
|
+ CTSectPr sectPr = document.getDocument().getBody().isSetSectPr() ? document.getDocument().getBody().getSectPr() : document.getDocument().getBody().addNewSectPr();
|
|
|
+ XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);
|
|
|
+ XWPFHeader header = policy.createHeader(STHdrFtr.DEFAULT, new XWPFParagraph[]{paragraph_});
|
|
|
+ header.setXWPFDocument(document);
|
|
|
+
|
|
|
xwpfHelper.saveDocument(document, response);
|
|
|
}
|
|
|
|
|
|
@@ -163,7 +187,9 @@ public class ExportWord {
|
|
|
for (int j = 0; j < list.size(); j++) {
|
|
|
XWPFParagraph cellParagraph = cellList.get(j).getParagraphArray(0);
|
|
|
XWPFRun cellParagraphRun = cellParagraph.getRuns().get(0);
|
|
|
- cellParagraphRun.setText(String.valueOf(list.get(j)));
|
|
|
+ String str = (String) list.get(j);
|
|
|
+ String newStr = str.replace("\n\n\n\t", "\r");
|
|
|
+ cellParagraphRun.setText(newStr);
|
|
|
}
|
|
|
}
|
|
|
}
|