springboot 数据pdf下载

需要的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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值