poi 导出excel 简单示例

 

 

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellUtil;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @Auyher: zjk
 */
@RestController
@RequestMapping("/weather")
public class Weather {

    /**
     * @return 天气查询-返回历史30天的天气信息
     * @throws IOException
     */
    @GetMapping("export")
    public void export(@RequestParam("site") String site, HttpServletResponse response) throws Exception {

        ArrayList arrayList = new ArrayList();

        Document doc = null;
        doc = Jsoup.connect("https://www.tianqishi.com/lishi/" + site + ".html").get();

        Element body = doc.getElementsByClass("yuBaoTable").get(0);
        Elements div = body.select("tr");

        for (Element v : div) {
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            Element when = v.select("td").get(0);
            String whenStr = when.select("a").get(0).text();
            String s = whenStr.substring(0, 4) + "-" + whenStr.substring(4, 6) + "-" + whenStr.substring(6, 8);
            map.put("when", s);
            map.put("temp", v.select("td").get(1).text());
            map.put("wind", v.select("td").get(2).text());
            arrayList.add(map);
        }
        List list = arrayList.subList(0, 30);


        // 创建工作簿类
        XSSFWorkbook workbook = new XSSFWorkbook();


        CellStyle style = workbook.createCellStyle();
        //下边框
        style.setBorderBottom(BorderStyle.THIN);
        //左边框
        style.setBorderLeft(BorderStyle.THIN);
        //上边框
        style.setBorderTop(BorderStyle.THIN);
        //右边框
        style.setBorderRight(BorderStyle.THIN);
        //居中
        style.setAlignment(HorizontalAlignment.CENTER);
        // 设置垂直对齐的样式为居中对齐;
        style.setVerticalAlignment(VerticalAlignment.CENTER);

        Font headerFont = workbook.createFont();
        // 设置字体
        headerFont.setFontName("宋体");
        // 设置字体大小
        headerFont.setFontHeightInPoints((short) 12);
        // 字体加粗
        headerFont.setBold(true);
        // 斜体
        headerFont.setItalic(false);
        // 字体颜色
        headerFont.setColor(IndexedColors.BLACK.getIndex());
        //设置字体高度
        headerFont.setFontHeightInPoints((short) 22);

        // 创建工作表并设置表名
        XSSFSheet sheet = workbook.createSheet("历史天气信息");

        //设置列宽和行高
        sheet.setDefaultRowHeight((short) 700);
        sheet.setDefaultColumnWidth(25);

        //创建表头
        XSSFRow row = sheet.createRow(0);

        CellRangeAddress region = new CellRangeAddress(0, 0, 0, 3);
        sheet.addMergedRegion(region);

        Cell cell = row.createCell(0);
        cell.setCellStyle(style);
        cell.setCellValue("近一个月天气情况");

        CellUtil.setAlignment(cell, HorizontalAlignment.CENTER);
        CellUtil.setVerticalAlignment(cell, VerticalAlignment.CENTER);
        CellUtil.setFont(cell, headerFont);

        // 3.设置合并单元格边框
        setBorderStyle(sheet, region);
        XSSFRow row2 = sheet.createRow(1);
        XSSFCell cell0 = row2.createCell(0);
        XSSFCell cell1 = row2.createCell(1);
        XSSFCell cell2 = row2.createCell(2);
        XSSFCell cell3 = row2.createCell(3);
        cell0.setCellValue("序号");
        cell1.setCellValue("日期");
        cell2.setCellValue("天气");
        cell3.setCellValue("温度");

        cell0.setCellStyle(style);
        cell1.setCellStyle(style);
        cell2.setCellStyle(style);
        cell3.setCellStyle(style);

        for (int i = 0; i < list.size(); i++) {
            LinkedHashMap<String, String> map = (LinkedHashMap<String, String>) list.get(i);
            XSSFRow row1 = sheet.createRow(i + 2);
            XSSFCell cell4 = row1.createCell(0);
            XSSFCell cell5 = row1.createCell(1);
            XSSFCell cell6 = row1.createCell(2);
            XSSFCell cell7 = row1.createCell(3);
            cell4.setCellValue(i + 1);
            cell5.setCellValue(map.get("when"));
            cell6.setCellValue(map.get("wind"));
            cell7.setCellValue(map.get("temp"));

            cell4.setCellStyle(style);
            cell5.setCellStyle(style);
            cell6.setCellStyle(style);
            cell7.setCellStyle(style);
        }

        try{
            String fileName = "历史天气信息.xlsx";
            ServletOutputStream out = response.getOutputStream();
            response.setHeader("content-type", "text/plain");
            response.setHeader("content-type", "application/x-msdownload;");
            response.setContentType("text/plain; charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename="  +  URLEncoder.encode(fileName, "UTF-8"));
            out = response.getOutputStream();
            workbook.write(out);
            out.flush();
            out.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vegetari

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值