springboot 数据pdf下载
2017-12-14 11:11
435 查看
需要的jar
conreoller代码
List<Classmate> classmateList = classmateService.getAllClassmate(); for (Classmate classmate : classmateList) {
这里也有很多其他的。
http://rensanning.iteye.com/blog/1538689
compile("com.itextpdf:itextpdf:5.5.12") compile("com.itextpdf:itext-asian:5.2.0")
conreoller代码
package com.controller.downloadDataIsPdf; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Font; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import com.pojo.user.Classmate; import com.service.ClassmateService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; @RestController @RequestMapping("downloadPdfClassmate") public class DownloadPdfClassmate { private static final Logger logger = LoggerFactory.getLogger(DownloadPdfClassmate.class); private ClassmateService classmateService; @RequestMapping(value = "downloadAllClassmateData", method = RequestMethod.GET) public void downloadAllClassmate(HttpServletRequest request, HttpServletResponse response) throws IOException, DocumentException { // 告诉浏览器用什么软件可以打开此文件 response.setHeader("content-Type", "application/pdf"); // 下载文件的默认名称 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); String fileName = "Classmate-" + new Date().getTime() + ".pdf"; response.setHeader("Content-Disposition", "attachment;filename="+ fileName); //设置中文 BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font FontChinese = new Font(bfChinese, 12, Font.NORMAL); Document document = new Document(); PdfWriter.getInstance(document, response.getOutputStream()); document.open();
document.addTitle("同学信息表");
//表头 PdfPTable tableS = new PdfPTable(6); tableS.addCell(new Paragraph("姓名",FontChinese)); tableS.addCell(new Paragraph("性别",FontChinese)); tableS.addCell(new Paragraph("电话",FontChinese)); tableS.addCell(new Paragraph("家庭地址",FontChinese)); tableS.addCell(new Paragraph("出生年月",FontChinese)); tableS.addCell(new Paragraph("个人喜好",FontChinese)); document.add(tableS);
List<Classmate> classmateList = classmateService.getAllClassmate(); for (Classmate classmate : classmateList) {
PdfPTable table = new PdfPTable(6); table.addCell(new Paragraph(classmate.getUsername(),FontChinese)); table.addCell(new Paragraph(classmate.getGender(),FontChinese)); table.addCell(new Paragraph(classmate.getTel(),FontChinese)); table.addCell(new Paragraph(classmate.getHomeAddress(),FontChinese)); table.addCell(new Paragraph(String.valueOf(dateFormat.format(classmate.getBirthTime())),FontChinese)); table.addCell(new Paragraph(classmate.getHobby(),FontChinese)); document.add(table);} document.close(); } @Autowired public void setClassmateService(ClassmateService classmateService) { this.classmateService = classmateService; }}
这里也有很多其他的。
http://rensanning.iteye.com/blog/1538689
相关文章推荐
- 【资源】108个大数据文档PDF开放下载-整理后打包下载
- 经验分享 |【PDF下载】双11技术峰会之双11媒体大屏背后的数据技术
- 【PDF下载】大数据峰会之MaxCompute 2.0 性能优化揭秘
- 【PDF下载】大数据峰会之用大数据打造你的变美频道
- 【PDF下载】大数据峰会之地产大数据趋势与应用实践
- 【资源】108个大数据文档PDF开放下载-整理后打包下载
- 【PDF下载】双11技术峰会之阿里大规模数据计算与处理
- 【PDF下载】大数据峰会之云上大数据,零售新思维
- 大数据Spark企业级实战 PDF 下载 和目录
- 知识图谱数据管理浅讲(彩色标注经典收藏版,16k字;文末可下载本文PDF文档)
- 大数据文档PDF开放下载
- 《Oracle DBA工作笔记:运维、数据迁移与性能调优》 PDF 下载
- 【PDF下载】大数据峰会之阿里大规模流式增量计算应用
- 【PDF下载】大数据峰会之专利大数据的云上裂变之路
- 经验分享 | 【PDF下载】大数据峰会之MaxCompute数据上云与生态
- [.NET 4.0]从 Web浏览器存取 WCF数据服务 / WCF Data Services (附:PDF与范例下载)
- pdf插入数据并下载
- 经验分享 |【PDF下载】双11技术峰会之数据赋能商家背后的AI技术
- 经验分享 |【PDF下载】大数据峰会之高可用大数据计算平台持续发布
- 干货 | 36个大数据文档PDF开放下载