POI 动态生成ECXEL

package com.adc.da.util.utils;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author: xiaoyu
 * @Date: 11:33 2017/11/20
 * @Description:
 * @ModifyBy:
 */
public class ExcelUtils {
    private static final String PATH_FGF = "\\";
    private static final String NAME_SUFFIX = ".xls";

    /**
     * @Author: xiaoyu
     * @Date: 16:05 2017/11/20
     * @Description:
     * @param: [excelPath, tableEOName, fileEOMap]
     * @return: void
     * @ModifyBy:
     */
    public static void createExcel(String excelPath, String tableEOName, Map<List<String>, List<String>> fileEOMap) throws IOException {

        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();

        //创建HSSFSheet对象
        HSSFSheet sheet = hssfWorkbook.createSheet(tableEOName);
        //设置sheet 的默认宽度
        sheet.setDefaultColumnWidth(35);
        //创建HSSFRow对象  设置行
        HSSFRow row = sheet.createRow(0);
        HSSFCellStyle style = hssfWorkbook.createCellStyle();
        //设置样式
        //背景颜色
//       style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
        //底纹样式
//       style.setFillPattern(FillPatternType.BRICKS);
        //初始的excel 没有boder 需要自己设置
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        //设置行内居中
        style.setAlignment(HorizontalAlignment.CENTER_SELECTION);
        //
        style.setShrinkToFit(true);
//        style.setWrapText(true);
//        style.setDataFormat(DateFormat.);
        HSSFFont font = hssfWorkbook.createFont();
        //设置字体颜色 大小 格式
        font.setFontName("微软雅黑");
//      font.setColor(HSSFColor.VIOLET.index);
        font.setFontHeightInPoints((short) 16);
        // 把字体应用到当前的样式
        style.setFont(font);
        Set<Map.Entry<List<String>, List<String>>> entrySet = fileEOMap.entrySet();
        Iterator<Map.Entry<List<String>, List<String>>> iterator = entrySet.iterator();
        for (int j = 0; iterator.hasNext(); j++) {
            Map.Entry<List<String>, List<String>> next = iterator.next();
            List<String> key = next.getKey();
            List<String> value = next.getValue();
            for (int i = 0; i < value.size(); i++) {
                //创建HSSFCell对象
                HSSFCell cell = row.createCell(i);
                //设置单元格的值
                cell.setCellValue(key.get(i) + "(" + value.get(i) + ")\t");
                cell.setCellStyle(style);
            }
        }
        File file = new File(excelPath + PATH_FGF + tableEOName + NAME_SUFFIX);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }

        file.createNewFile();
        //输出Excel文件
        FileOutputStream output = new FileOutputStream(file);
        hssfWorkbook.write(output);
        //注意 不关out 会显示个 0kb 的文件,打开会显示什么只读乱七八糟的东西。
        output.close();
        hssfWorkbook.close();
    }


}

猜你喜欢

转载自blog.csdn.net/qq_36324685/article/details/78582958
poi
今日推荐