在实习开发中,生成PDF或Excel表格是很常见的功能,但是有时往往在短时间之内又不能很好的写出功能齐全的代码,所以简单的列一个模板。
首先,需要几个相关夹包的支持,将其加入到类路径地下方可使用:

这个包在maven中央仓库是找不到,只有在Spring4.x企业应用开发实战这本书的网盘上有,版本有点旧,在依赖itext-jar时千万要注意,不要依赖最新的jar,不然版本不兼容,无法解析 。
<!-- https://mvnrepository.com/artifact/com.lowagie/itext -->
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
基本条件配置完成后,就可以实现代码的编写了。
具体代码实现
1、AttendancePdfView - 考勤报表的PDF视图
package com.qfedu.sail.controller.lgj;
import java.awt.Color;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.document.AbstractPdfView;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
import com.qfedu.sail.entity.Attendance;
@Component
public class AttendancePdfView extends AbstractPdfView {
@Override
protected void buildPdfDocument(Map<String, Object> model,
Document document, PdfWriter writer, HttpServletRequest req,
HttpServletResponse resp) throws Exception {
resp.setHeader("Content-Disposition", "inline;filename="+
new String("考勤报表".getBytes(),"iso-8859-1"));
List<Attendance> attendanceList = (List<Attendance>) model.get("aList");
for (Attendance attendance : attendanceList) {
System.out.println(attendance.getAid());
}
Table table = new Table(3);
table.setWidth(80);
table.setBorder(1);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
BaseFont cnBaseFont = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",false);
Font cnFont = new Font(cnBaseFont,10,Font.NORMAL,Color.BLUE);
table.addCell(buildFontCell("编号",cnFont));
table.addCell(buildFontCell("姓名",cnFont));
table.addCell(buildFontCell("打卡时间",cnFont));
for(Attendance attendance:attendanceList) {
table.addCell(buildFontCell(attendance.getUser().getUid().toString(), cnFont));
table.addCell(buildFontCell(attendance.getUser().getUserName(), cnFont));
table.addCell(buildFontCell(attendance.getClockInTime().toString(),cnFont));
}
document.add(table);
}
private Cell buildFontCell(String content,Font font) throws RuntimeException{
try {
Phrase phrase = new Phrase(content,font);
return new Cell(phrase);
} catch (BadElementException e) {
throw new RuntimeException(e);
}
}
}
2、showAttendanceListByPdf-controller
@RequestMapping("/showAttendanceListByPdf")
public String showAttendanceListByPdf(ModelMap mm) {
List<Attendance> aList = new ArrayList<>();
aList = attendanceService.findAll();
mm.addAttribute("aList",aList);
return "attendanceListPdf";
}
3、想要生成PDF还需要在springMVC的配置文件中将视图配置成bean,同时还需要添加一个视图解析器,具体如下:
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="10" />
</bean>
<bean id="attendanceListPdf" class="com.qfedu.sail.controller.lgj.AttendancePdfView" />
网友评论