您的位置:首页 > 编程语言 > Java开发

springboot 数据pdf下载

2017-12-14 11:11 435 查看
需要的jar

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: