工具类总结(8)- poi导出工具类

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class PoiUtil {
//文件名 准备数据 数据Class 文件保存路径
    public static String getUrl(String excelName, List listInfo, Class c, String savePath) throws IllegalAccessException {

        // 创建Excel文件
        Workbook workbook = new HSSFWorkbook();
        // 创建sheet
        Sheet sheet = workbook.createSheet(excelName);
        //设置默认列宽
        sheet.setDefaultColumnWidth(30);
        // style  为单元格设置样式
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setColor(Font.COLOR_RED);
        // 加粗
        font.setBold(true);
        font.setFontName("宋体");
        // 设置居中
        style.setAlignment(CellStyle.ALIGN_CENTER);
        style.setFont(font);
        // 准备数据
//        listInfo
        /**
         * 获得所有属性名作为标题
         */
        Field[] fields = c.getDeclaredFields();

        // 创建行
        Row titleRow = sheet.createRow(0);

        for (int i = 0; i < fields.length; i++) {
            com.manage.model.Comment comment = fields[i].getAnnotation( com.manage.model.Comment.class);
            String value = comment.value();
            // 创建单元格
            Cell cell = titleRow.createCell(i);
            cell.setCellValue(value);
            //设置样式
            cell.setCellStyle(style);
        }

        /**
         * 设置内容
         */
        for (int i = 0; i < listInfo.size(); i++) {
            Row row = sheet.createRow(i + 1);
            for (int j = 0; j < fields.length; j++) {
                // 打破封装
                fields[j].setAccessible(true);
                Object val = fields[j].get(listInfo.get(i));
                // System.out.println(val);
                Cell cell = row.createCell(j);
                // 创建单元格
                if (fields[j].getType() == Date.class) {
                    String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(val);
                    cell.setCellValue(format);
                } else {
                    cell.setCellValue(val.toString());
                }
            }
        }
        // 通过流输出
//        HttpServletRequest request;
//        String imgPath = request.getSession().getServletContext().getRealPath("/admin/page/productImages");
        String title = excelName + "表.xls";
        File file = new File(savePath, title);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            workbook.write(fos);
            System.out.println("信息导出成功!");
        } catch (FileNotFoundException e) { // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) { // TODO Auto-generated
            e.printStackTrace();
        }
        return savePath;
    }


}

使用:

 @RequestMapping("/getPrint")
    @ResponseBody
    @Log(operationName = "打印订单",operationType = "print")
    public Result getPrint(HttpServletRequest request) {
        Result result = new Result();
        List<OrderPoi> orderPoiList = orderService.printOrder();
        //获取存放路径
        String savePath = request.getSession().getServletContext().getRealPath("/admin/page");
        String path = null;

        try {
            path = PoiUtil.getUrl("订单信息",orderPoiList,new OrderPoi().getClass(),savePath);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        System.out.println(path);

        if (path != null) {
            result.setCode(1);
            result.setError(false);
            result.setMessage("订单打印成功");
            result.setData("/manage/admin/page/订单信息表.xls");
        } else {
            result.setCode(0);
            result.setError(true);
            result.setMessage("订单打印失败");
            result.setData(null);
        }
        return result;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

孟林洁

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

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

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

打赏作者

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

抵扣说明:

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

余额充值