前言
最近项目中用到了导出PDF,所以在这里总结一下,便于以后的查阅.也在这里分享给看到博客的伙伴们
核心
代码展示:
public void exportpdf(HttpServletRequest request, HttpServletResponse response) {
String strAppRootPath = request.getServletContext().getRealPath("/")
String title = "LDAR维修工单"
// 导出文件名
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss")
String fileName = formatter.format(System.currentTimeMillis()) + ".pdf"
Map dicTitleUp = new HashMap<>()
dicTitleUp.put(0, "组件号")
dicTitleUp.put(1, "装置")
dicTitleUp.put(2, "区域")
dicTitleUp.put(3, "设备")
dicTitleUp.put(4, "组件类型")
dicTitleUp.put(5, "组件子类型")
dicTitleUp.put(6, "位置描述")
dicTitleUp.put(7, "尺寸(mm)")
dicTitleUp.put(8, "PID图号")
dicTitleUp.put(9, "检测日期")
dicTitleUp.put(10, "检测人")
dicTitleUp.put(11, "PPM读数")
dicTitleUp.put(12, "泄漏阈值")
dicTitleUp.put(13, "泄漏源")
dicTitleUp.put(14, "首次修复期限")
dicTitleUp.put(15, "最终修复期限")
dicTitleUp.put(16, "到期时间")
dicTitleUp.put(17, "延迟修复的预期日期")
Map dicUp = new HashMap<>()
dicUp.put(0, "COMPONENT_NUMBER")
dicUp.put(1, "LOCATIONA_NAME")
dicUp.put(2, "LOCATIONB_NAME")
dicUp.put(3, "LOCATIONC_NAME")
dicUp.put(4, "COMPONENT_TYPE_NAME")
dicUp.put(5, "SUB_TYPE_NAME")
dicUp.put(6, "LOCATION_DESCRIPTION")
dicUp.put(7, "SIZE_OF")
dicUp.put(8, "DRAWING")
dicUp.put(9, "DATETIMESTART")
dicUp.put(10, "realname")
dicUp.put(11, "READING")
dicUp.put(12, "FINAL_THRESHOLD")
dicUp.put(13, "leakage_source_name")
dicUp.put(14, "FIRST_TIME_LIMIT")
dicUp.put(15, "LAST_TIME_LIMIT")
dicUp.put(16, "due")
dicUp.put(17, "expeted")
// 下面的表格
Map dicTitle = new HashMap<>()
dicTitle.put(0, "维修日期")
dicTitle.put(1, "维修人")
dicTitle.put(2, "维修措施")
dicTitle.put(3, "复检日期")
dicTitle.put(4, "检测人")
dicTitle.put(5, "仪器")
dicTitle.put(6, "背景值")
dicTitle.put(7, "PPM读数")
Map dic = new HashMap<>()
dic.put(0, "maintenance_date")
dic.put(1, "maintenance_name")
dic.put(2, "maintenance_measures_name")
dic.put(3, "retest_start_date")
dic.put(4, "retest_name")
dic.put(5, "testing_instrument_name")
dic.put(6, "background_ppm_readings")
dic.put(7, "ppm_readings")
Rectangle rect = new Rectangle(PageSize.A4)
Document document = new Document(rect)
OutputStream os = null
try {
String type = "pdf"
this.setResponseHeader(response, fileName ,type)
os = response.getOutputStream()
PdfWriter writer = PdfWriter.getInstance(document, os)
document.open()
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false)
Font fontTitle = new Font(bfChinese, 22.0F, 0)
Font exportMan = new Font(bfChinese, 14.0F, 0)
Font cont = new Font(bfChinese, 10.0F, 0)
Font s = new Font(bfChinese, 10.0F, 0)
//查询的数据
List
总结
在项目中不断的成长.