導出數據(本地)

第一種 : 導出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();
        }

    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章