导出数据(本地)

第一种 : 导出csv文件

依赖

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.16</version>
</dependency>

工具类ExportFileUtils

import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

public class ExportFileUtils {

    private static final Logger logger = Logger.getLogger(ExportFileUtils.class);

    /**
     * 导出csv文件
     * @param head 字段名称
     * @param maps 数据源
     * @param os 输出流
     * @throws Exception
     */
    public static void exportCsv(String[] head, List<Map<String, Object>> maps, OutputStream os)
            throws Exception {
        try {
            StringBuffer headLine = new StringBuffer();
            for (String key : head){
                headLine.append("\"").append(key).append("\"");
                headLine.append(",");
            }
            headLine.append("\r\n");
            os.write(headLine.toString().getBytes("GBK"));

            for (int i = 0; i < maps.size(); i++) {
                Map<String, Object> map = maps.get(i);
                StringBuffer line = new StringBuffer();
                for (String key : head) {
                    Object obj = map.get(key);
                    line.append("\"").append(obj).append("\"");
                    line.append(",");
                }
                line.append("\r\n");
                os.write(line.toString().getBytes("GBK"));
            }
            os.flush();
        }
        catch (Exception e) {
            
            throw e;
        }
        finally {
            if (os != null) {
                try {
                    os.close();
                }
                catch (IOException e) {
                    logger.error(e);
                }
            }
        }
    }
}

main方法测试

    public static void main(String[] args) {
       //标题
        String[] head = {"一", "二", "三", "四"};
        //测试数据
        List<Map<String, Object>> maps = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("一", 1);
        map1.put("二", 2);
        map1.put("三", 3);
        map1.put("四", 4);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("一", 11);
        map2.put("二", 22);
        map2.put("三", 33);
        map2.put("四", 44);
        Map<String, Object> map3 = new HashMap<>();
        map3.put("一", 111);
        map3.put("二", 222);
        map3.put("三", 333);
        map3.put("四", 444);
        maps.add(map1);
        maps.add(map2);
        maps.add(map3);
        //获取当前路径
        String webAppRootPath = System.getProperty("user.dir");
        String fileName = "信息导出_" + DateHelper.getToday() + ".csv";
        String path = webAppRootPath +"\\" + fileName;
        logger.info("# 导出文件路径 " + path + "   ########");
        File f = new File(path);
        try {
            OutputStream out = new FileOutputStream(f);
            ExportFileUtils.exportCsv(head, maps, out);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

第二种 : 导出excle文件

依赖

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.16</version>
</dependency>

工具类

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.*;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * @author wyc
 * @date 2019/7/30 20:17
 */
public class ExportUtil {

    private static final Logger logger = Logger.getLogger(ExportUtil.class);
    public static void export(String[] head,List<Map<String,Object>> maps,OutputStream os){
        HSSFWorkbook workBook = new HSSFWorkbook();// 创建一个Excel工作薄
        HSSFSheet sheet = workBook.createSheet("sheet1");

        HSSFRow headerRow = sheet.createRow(0);// 创建首行,并赋值
        HSSFFont headFont = workBook.createFont();
        headFont.setFontName("仿宋_GB2312");
        headFont.setFontHeightInPoints((short) 12);
        HSSFCellStyle style = workBook.createCellStyle();
        style.setFont(headFont);
        headerRow.setRowStyle(style);
        for (int i = 0; i < head.length; i++) {//给首行赋值(设置标题)
            headerRow.createCell(i).setCellValue(head[i]);
            sheet.setColumnWidth(i, 6000);
        }
        for (int j = 0; j < maps.size(); j++) {//给数据行赋值
            HSSFRow row = sheet.createRow(j+1);
            row.createCell(0).setCellValue(maps.get(j).get("一").toString());
            row.createCell(1).setCellValue(maps.get(j).get("二").toString());
            row.createCell(2).setCellValue(maps.get(j).get("三").toString());
            row.createCell(3).setCellValue(maps.get(j).get("四").toString());
        }
        try {
            workBook.write(os);
        } catch (IOException e) {
            logger.error("",e);
        }finally {
            try {
                if (os != null){
                    os.close();
                }
            } catch (IOException e) {
                logger.error("",e);
            }
        }

    }
}

main方法测试

    public static void main(String[] args) {
        String[] head = {"一", "二", "三", "四"};
        List<Map<String, Object>> maps = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("一", 1);
        map1.put("二", 2);
        map1.put("三", 3);
        map1.put("四", 4);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("一", 11);
        map2.put("二", 22);
        map2.put("三", 33);
        map2.put("四", 44);
        Map<String, Object> map3 = new HashMap<>();
        map3.put("一", 111);
        map3.put("二", 222);
        map3.put("三", 333);
        map3.put("四", 444);
        maps.add(map1);
        maps.add(map2);
        maps.add(map3);
        String webAppRootPath = System.getProperty("user.dir");
        String fileName = "信息导出_" + DateHelper.getToday() + ".csv";
        String path = webAppRootPath +"\\" + fileName;
        logger.info("# 导出文件路径 " + path + "   ########");
        File f = new File(path);
        try {
            OutputStream out = new FileOutputStream(f);
            ExportUtil.export(head,maps,out);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值