Java中,使用HSSFSheet创建excel模板如何创建一列两行的数据?

如图使用HSSFSheet想创建一行两列的excel,如何写?
3FA869B9-7071-4714-80A2-AC63734AD0BA.jpg
clipboard.png

阅读 4.5k
2 个回答

使用合并单元格的方法试试。

不知道下面这段代码是不是你想要的 ,这是把数据库的表导出到excel的创建excel的一部分代码

    
     // 创建标题
    HSSFRow titleRow = hssfSheet.createRow(0);
    for(int  i = 0 ; i < columnCount ; i++){
        HSSFCell headCell = titleRow.createCell(i);
        headCell.setCellStyle(headCellStyle);
        headCell.setCellValue(new HSSFRichTextString(columnNames.get(i)));
    }

    // 创建正文样式
    HSSFCellStyle bodyCellStyle = hssfWorkbook.createCellStyle();
    HSSFFont bodyFont = hssfWorkbook.createFont();
    bodyFont.setColor(Font.COLOR_NORMAL);
    bodyFont.setBold(false);
    bodyFont.setFontName("宋体");
    bodyFont.setFontHeight((short) 250);
    bodyCellStyle.setFont(bodyFont);

    // 创建正文
    try {
        // 在 excel 中所在的行数
        int columnRow = 1;
        while(resultSet.next()){
            HSSFRow bodyRow = hssfSheet.createRow(columnRow++); // 创建行对象
            for(int i = 0; i < columnCount; i++){   // 设置行对象中的每一个单元格的值
                HSSFCell bodyCell = bodyRow.createCell(i);
                bodyCell.setCellStyle(bodyCellStyle);
                bodyCell.setCellValue(new             
                HSSFRichTextString(resultSet.getString(columnNames.get(i))));
            }
        }

        OutputStream writer = new FileOutputStream(path);
        hssfWorkbook.write(writer);
    } catch (SQLException e) {
        isSuccess = false;
        e.printStackTrace();
    } catch (IOException e) {
        isSuccess = false;
        e.printStackTrace();
    }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题