poi create excel file

package com.mozq.sb.file01.test;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

public class Excel_01 {
    public static void main(String[] args) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("创建sheet页");

        //默认的列宽度8字符
        System.out.println(sheet.getDefaultColumnWidth());

        //设置默认列宽和默认行高
        sheet.setDefaultColumnWidth(20);
        sheet.setDefaultRowHeightInPoints((short) 20);

        //存储最大列宽
        Map<Integer,Integer> maxWidth = new HashMap<>();
        maxWidth.put(0, getLength("错误行"));
        maxWidth.put(1,getLength("Excel表格导入错误消息"));
        for (Integer key : maxWidth.keySet()) {
            System.out.println(key + ":" + maxWidth.get(key));
            sheet.setColumnWidth(key, maxWidth.get(key));
        }

        HSSFRow sheetTitle = sheet.createRow(1);
        //创建

        HSSFRow row = sheet.createRow(2);//创建行
        //设置行高
        row.setHeightInPoints(30);

        HSSFCell cell = row.createCell(0);
        cell.setCellValue("错误行");
        cell.setCellStyle(getTitleCellStyle(workbook));//设置样式

        HSSFCell cell1 = row.createCell(1);
        cell1.setCellValue("Excel表格导入错误消息");
        cell1.setCellStyle(getTitleCellStyle(workbook));//设置样式

        //-------------打印内容
        List<String[]> content = new ArrayList<>();
        content.add(new String[]{"第1行", "电话号码格式错误"});
        content.add(new String[]{"第2行", "用户信息不存在"});

        for (int i = 0; i < content.size(); i++) {
            HSSFRow dataRow = sheet.createRow(i + 3);
            String[] strs = content.get(i);
            for (int j = 0; j < strs.length; j++) {
                HSSFCell posCell = dataRow.createCell(j);
                posCell.setCellValue(strs[j]);
                posCell.setCellStyle(getTextCellStyle(workbook));
                System.out.println(strs[j]);
            }
        }

        FileOutputStream fileOutputStream=new FileOutputStream("E:\\00\\3.xls");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
    }

    public static Integer getLength(String content){
        Integer length =  0;
        if(Objects.nonNull(content)){
           length =  content.getBytes().length * 2 * 230;
        }
        return length;
    }

    //创建标题栏通用样式
    public static HSSFCellStyle getTitleCellStyle(HSSFWorkbook workbook){
        /* 对齐方式 */
        HSSFCellStyle cellStyle= workbook.createCellStyle();  //设置样式
        //水平居中
        cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
        //垂直居中
        cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

        /* 边框设置 */
        cellStyle.setBorderTop(CellStyle.BORDER_THIN);
        cellStyle.setBorderRight(CellStyle.BORDER_THIN);
        cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
        cellStyle.setBorderLeft(CellStyle.BORDER_THIN);

        /* 字体样式 */
        HSSFFont font = workbook.createFont();
        //字体颜色
        font.setColor(HSSFColor.DARK_BLUE.index);
        //字体高度
        font.setFontHeightInPoints((short) 14);
        //字体
        font.setFontName("宋体");
        //加粗
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);

        cellStyle.setFont(font);
        return cellStyle;
    }

    //创建标题栏通用样式
    public static HSSFCellStyle getTextCellStyle(HSSFWorkbook workbook){
        /* 对齐方式 */
        HSSFCellStyle cellStyle= workbook.createCellStyle();  //设置样式
        //水平居中
        cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
        //垂直居中
        cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

        /* 边框设置 */
        cellStyle.setBorderTop(CellStyle.BORDER_THIN);
        cellStyle.setBorderRight(CellStyle.BORDER_THIN);
        cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
        cellStyle.setBorderLeft(CellStyle.BORDER_THIN);

        /* 字体样式 */
        HSSFFont font = workbook.createFont();
        //字体颜色
        font.setColor(HSSFColor.BLACK.index);
        //字体高度
        font.setFontHeightInPoints((short) 14);
        //字体
        font.setFontName("宋体");
        //字体加粗
        //font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        cellStyle.setFont(font);
        return cellStyle;
    }
}

Guess you like

Origin www.cnblogs.com/mozq/p/12069467.html