本文简略创建了两种格式的文件,部分代码需要根据实际业务进行调整,代码中如有错误,望各位大佬多多体谅并提出纠正
maven 依赖
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext-rtf</artifactId>
<version>2.1.7</version>
</dependency>
创建word
public File toWord(List<beanVo> list, String fileName, String title, HttpServletResponse res, HttpServletRequest request) {
Document document = new Document(PageSize.A4);
try {
File file = new File(fileName);
if (file.exists() && file.isFile()) {
file.delete();
}
file.createNewFile();
RtfWriter2.getInstance(document, new FileOutputStream(fileName));
document.open();
Paragraph ph = new Paragraph();
Font f = new Font();
Paragraph p = new Paragraph(title, new Font(Font.NORMAL, 24, Font.BOLDITALIC, new Color(0, 0, 0)));
p.setAlignment(1);
document.add(p);
ph.setFont(f);
Table table = new Table(5);
document.add(new Paragraph(""));
table.setBorderWidth(1);
table.setPadding(0);
table.setSpacing(0);
Color chade = new Color(176, 196, 222);
Cell cell = new Cell("序号");
addWordCell(table, cell, chade);
cell = new Cell("第一列");
addWordCell(table, cell, chade);
cell = new Cell("第二列");
addWordCell(table, cell, chade);
cell = new Cell("第三列");
addWordCell(table, cell, chade);
cell = new Cell("第四列");
addWordCell(table, cell, chade);
table.endHeaders();
for (int i = 0; i < list.size(); i++) {
addContent(table, cell, (i + 1) + "");
addContent(table, cell,list.get(i).getTaskDesc());
addContent(table, cell, list.get(i).getDepartName());
addContent(table, cell, list.get(i).getTaskMeasures());
addContent(table, cell, list.get(i).getContactPerson());
}
document.add(table);
document.close();
return file;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private void addWordCell(Table table, Cell cell, Color chade) {
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBackgroundColor(chade);
table.addCell(cell);
}
private void addContent(Table table, Cell cell, String content) {
cell = new Cell(content);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
}
创建pdf
private static com.itextpdf.text.Font titlefont;
private static com.itextpdf.text.Font headfont;
private static com.itextpdf.text.Font keyfont;
private static com.itextpdf.text.Font textfont;
private static int maxWidth = 520;
static {
try {
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
titlefont = new com.itextpdf.text.Font(bfChinese, 16, com.itextpdf.text.Font.BOLD);
headfont = new com.itextpdf.text.Font(bfChinese, 14, com.itextpdf.text.Font.BOLD);
keyfont = new com.itextpdf.text.Font(bfChinese, 10, com.itextpdf.text.Font.BOLD);
textfont = new com.itextpdf.text.Font(bfChinese, 10, com.itextpdf.text.Font.NORMAL);
} catch (Exception e) {
e.printStackTrace();
}
}
public File toPDF(List<AccountTaskVo> list, String fileName, String title,HttpServletResponse res, HttpServletRequest request){
try {
Document document = new Document(PageSize.A4);
File file = new File(fileName);
file.createNewFile();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
com.itextpdf.text.Paragraph paragraph = new com.itextpdf.text.Paragraph(title, titlefont);
paragraph.setAlignment(1);
paragraph.setIndentationLeft(12);
paragraph.setIndentationRight(12);
paragraph.setFirstLineIndent(24);
paragraph.setLeading(20f);
paragraph.setSpacingBefore(5f);
paragraph.setSpacingAfter(10f);
com.itextpdf.text.Paragraph p1 = new com.itextpdf.text.Paragraph();
p1.add(new com.itextpdf.text.Chunk(new LineSeparator()));
PdfPTable table = new PdfPTable(5);
table.setTotalWidth(maxWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
table.addCell(createCell("序号", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("任务描述", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("责任单位", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("落实举措", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("完成期限", keyfont, Element.ALIGN_CENTER));
for (int i = 0; i < list.size(); i++) {
table.addCell(createCell((i+1)+"", textfont));
table.addCell(createCell(list.get(i).getTaskDesc(), textfont));
table.addCell(createCell(list.get(i).getDepartName(), textfont));
table.addCell(createCell(list.get(i).getTaskMeasures(), textfont));
table.addCell(createCell(list.get(i).getContactPerson(), textfont));
}
document.add(paragraph);
document.add(p1);
document.add(table);
document.close();
return file;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public PdfPCell createCell(String value, Font font) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setPhrase(new Phrase(value, font));
return cell;
}
public PdfPCell createCell(String value, Font font, int align) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setPhrase(new Phrase(value, font));
return cell;
}
public PdfPTable createTable(float[] widths) {
PdfPTable table = new PdfPTable(widths);
try {
table.setTotalWidth(maxWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
} catch (Exception e) {
e.printStackTrace();
}
return table;
}