31、使用spire.doc for java 操作文档记录
public static void createText(Section section,List<ApplyObject> wordList){
Paragraph paragraph = section.addParagraph();
TextRange tr = paragraph.appendText("专线一键搜查询内容");
tr.getCharacterFormat().setFontSize(20);
String name = "";
String sfzhm = "";
String queryContent = "";
for (ApplyObject applyObject : wordList){
name+=applyObject.getSelName()+",";
sfzhm+=applyObject.getIdNumber()+",";
queryContent = applyObject.getSelTableStr();
}
String nameNew = name.substring(0,name.length()-1);
String sfzhmNew = sfzhm.substring(0,sfzhm.length()-1);
Paragraph paragraph3 = section.addParagraph();
TextRange tr3 = paragraph3.appendText("外网专线查询对象为:"+nameNew+"("+sfzhmNew+")"+","+"查询数据项包括:"+queryContent);
Paragraph paragraph2 = section.addParagraph();
TextRange tr2 = paragraph2.appendText("专线一键搜查询结果");
tr2.getCharacterFormat().setFontSize(20);
}
找到段落,给每一个段落添加样式
ParagraphStyle style1 = new ParagraphStyle(document);
style1.setName("style");
style1.getCharacterFormat().setFontName("仿宋_GB2312");
document.getStyles().add(style1);
for(int i = 0; i < doc.getSections().getCount(); i++) {
Section section = doc.getSections().get(i);
for (int j = 0; j < section.getParagraphs().getCount(); j++) {
Paragraph paragraph = section.getParagraphs().get(j);
paragraph.applyStyle(style1.getName());
}
}
public static void createDoc(Section section, List<String> header, List<List<Object>> data, String title, List<ApplyObject> wordList) {
Table table = section.addTable(true);
table.resetCells(data.size() + 2, header.size());
TableRow row = table.getRows().get(1);
row.isHeader(true);
row.setHeight(40);
row.setHeightType(TableRowHeightType.Exactly);
TableRow row3 = table.getRows().get(0);
row3.isHeader(true);
row3.setHeight(60);
row3.setHeightType(TableRowHeightType.Exactly);
row3.getCells().get(0).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
table.applyHorizontalMerge(0, 0, header.size() - 1);
Paragraph p1 = row3.getCells().get(0).addParagraph();
p1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
TextRange range3 = p1.appendText(title);
range3.getCharacterFormat().setFontName("仿宋_GB2312");
range3.getCharacterFormat().setFontSize(12f);
range3.getCharacterFormat().setTextColor(Color.black);
range3.getCharacterFormat().setBold(true);
for (int i = 0; i < header.size(); i++) {
row.getCells().get(i).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
Paragraph p = row.getCells().get(i).addParagraph();
p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
TextRange range1 = p.appendText(header.get(i));
range1.getCharacterFormat().setTextColor(Color.black);
range1.getCharacterFormat().setFontName("仿宋_GB2312");
range1.getCharacterFormat().setFontSize(12f);
range1.getCharacterFormat().setBold(true);
}
try{
for (int r = 0; r < data.size(); r++) {
TableRow dataRow = table.getRows().get(r + 2);
dataRow.setHeight(25);
dataRow.setHeightType(TableRowHeightType.Exactly);
dataRow.getRowFormat().setBackColor(Color.white);
for (int c = 0; c < data.get(r).size(); c++) {
dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
String s = "";
if (data.get(r).get(c) instanceof Timestamp) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
s = df.format((Timestamp) (data.get(r).get(c)));
} else if (data.get(r).get(c) instanceof Date) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
s = df.format((Date) (data.get(r).get(c)));
} else if (data.get(r).get(c) == null) {
s = "";
} else {
s = data.get(r).get(c).toString();
}
TextRange range2 = dataRow.getCells().get(c).addParagraph().appendText(s);
range2.getCharacterFormat().setFontName("仿宋_GB2312");
range2.getCharacterFormat().setFontSize(10f);
}
}
}catch (Exception e){
e.getMessage();
log.info("插入数据有异常");
}
section.addParagraph();
}
public static void insertHeaderAndFooter(Section section) {
HeaderFooter header = section.getHeadersFooters().getHeader();
HeaderFooter footer = section.getHeadersFooters().getFooter();
Paragraph headerParagraph = header.addParagraph();
TextRange text = headerParagraph.appendText("页眉测试");
text.getCharacterFormat().setFontName("仿宋_GB2312");
text.getCharacterFormat().setFontSize(10);
text.getCharacterFormat().setItalic(true);
headerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);
headerParagraph.getFormat().getBorders().getBottom().setBorderType(BorderStyle.Single);
headerParagraph.getFormat().getBorders().getBottom().setLineWidth(1f);
Paragraph footerParagraph = footer.addParagraph();
footerParagraph.appendField("page number", FieldType.Field_Page);
footerParagraph.appendText("/");
footerParagraph.appendField("number of pages", FieldType.Field_Num_Pages);
footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);
footerParagraph.getFormat().getBorders().getTop().setBorderType(BorderStyle.Single);
footerParagraph.getFormat().getBorders().getTop().setLineWidth(1f);
}
|