JAVA POI创建多个sheet的Excel文件,及多个sheet也创建失败原因

最近写一个生成多个sheet页的表格,正好遇到了sheet只有一个的问题,拿出来跟大家分享下

解决问题直接下拉!

 

import java.io.IOException;
import java.util.List;
 
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
 
/**
 * 解析excel文件
 * @author guosk
 */
public class WriteExcelUtils {
 
    /**
     * 输出Excel文档
     * @param workbook 
     * @param resources 源数据
     * @param headerNames 表头
     * @param sheetName 表格名
     * @param columnNum 列数量
     * @param sheetNum 页码(sheet页码)
     * @throws IOException
     */
    public static void writeExcel(HSSFWorkbook workbook,
    					   List<String[]> resources, 
    					   String[] headerNames,
    					   String sheetName,
                                           Integer columnNum,
    					   Integer sheetNum) throws IOException {
        // 创建表格
    	HSSFSheet sheet = workbook.createSheet();
    	sheet.setDefaultRowHeightInPoints(13);//默认宽度
        workbook.setSheetName(sheetNum, sheetName);
        // 设置列宽,根据
        for(int i=0; i<=columnNum; i++){
            sheet.setColumnWidth(i, 6000);
        }
        /*
		 * 创建合并区域
		 * CellRangeAddress(int 首行, int 最后一行, int 首列, int 最后一列);
         */
        CellRangeAddress add = new CellRangeAddress(0, 0, 0, columnNum);
        // 将创建的合并区域设置到表格中.
        sheet.addMergedRegion(add);
        // 创建行
        Row header = sheet.createRow(0);
        // 创建单元格. 合并后的单元格,编号合并. 
        //设置样式
        CellStyle titleStyle = workbook.createCellStyle();
        Font titlefont = workbook.createFont();
        titlefont.setFontName("黑体");
        //titlefont.setColor(IndexedColors.VIOLET.index);
        titlefont.setFontHeightInPoints((short)20);
        titlefont.setBold(true);
        titleStyle.setFont(titlefont);
        titleStyle.setAlignment(HorizontalAlignment.CENTER);
        Cell c = header.createCell(0);
        c.setCellValue(sheetName);
        c.setCellStyle(titleStyle);
        c = header.createCell(columnNum);
        // 编写表头
        // 定义表头的样式
        CellStyle headerStyle = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setFontName("宋体");
        //font.setColor(IndexedColors.VIOLET.index);
        font.setFontHeightInPoints((short)16);
        headerStyle.setFont(font);
        headerStyle.setAlignment(HorizontalAlignment.CENTER);
        // 设置单元格样式
        Row headerRow = sheet.createRow(1);
        for (int i = 0; i < headerNames.length; i++) {
            Cell cell = headerRow.createCell(i);
            // 设置单元格样式
            cell.setCellStyle(headerStyle);
            cell.setCellValue(headerNames[i]);
        }
        // 设置表格数据的样式
        CellStyle bodyStyle = workbook.createCellStyle();
        Font bodyFont = workbook.createFont();
        bodyFont.setFontName("微软雅黑");
        //bodyFont.setColor(IndexedColors.BLUE.index);
        bodyFont.setFontHeightInPoints((short)12);
        bodyStyle.setFont(bodyFont);
 
        // 编辑表格体数据
        for (int i = 0; i < resources.size(); i++) {
            // 获取行数据
            String[] temp = resources.get(i);
            // 创建行
            Row bodyRow = sheet.createRow(i + 2);
            for (int cellNum = 0; cellNum < temp.length; cellNum++) {
                Cell bodyCell = bodyRow.createCell(cellNum);
                bodyCell.setCellStyle(bodyStyle);
                bodyCell.setCellValue(temp[cellNum]);
            }
        }
        sheet.getRow(0).setHeightInPoints(24);
        sheet.getRow(1).setHeightInPoints(20);
    }
}

下面说一下多个sheet页创建失败的原因:

当你在写完这个方法之后如果直接写入write(out)的话,就是永远只创建一个sheet

所以要在你执行多次之后再写出write(out)才可以

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值