用java做出两行三列的表格_Java中,使用HSSFSheet创建excel模板如何创建一列两行的数据?...

本文展示了如何使用Java的HSSFSheet库创建一个包含两行三列数据的Excel模板。通过创建标题行和设置正文样式,然后从数据库中获取数据填充到Excel表格中,实现了数据导出的功能。
摘要由CSDN通过智能技术生成

不知道下面这段代码是不是你想要的 ,这是把数据库的表导出到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();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值