鍍金池/ 問答/Java  HTML  Office/ Java中,使用HSSFSheet創(chuàng)建excel模板如何創(chuàng)建一列兩行的數(shù)據(jù)?

Java中,使用HSSFSheet創(chuàng)建excel模板如何創(chuàng)建一列兩行的數(shù)據(jù)?

如圖使用HSSFSheet想創(chuàng)建一行兩列的excel,如何寫?
3FA869B9-7071-4714-80A2-AC63734AD0BA.jpg
clipboard.png

回答
編輯回答
舊顏

不知道下面這段代碼是不是你想要的 ,這是把數(shù)據(jù)庫(kù)的表導(dǎo)出到excel的創(chuàng)建excel的一部分代碼

    
     // 創(chuàng)建標(biāo)題
    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)));
    }

    // 創(chuàng)建正文樣式
    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);

    // 創(chuàng)建正文
    try {
        // 在 excel 中所在的行數(shù)
        int columnRow = 1;
        while(resultSet.next()){
            HSSFRow bodyRow = hssfSheet.createRow(columnRow++); // 創(chuàng)建行對(duì)象
            for(int i = 0; i < columnCount; i++){   // 設(shè)置行對(duì)象中的每一個(gè)單元格的值
                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();
    }
2017年8月1日 08:29
編輯回答
晚風(fēng)眠

使用合并單元格的方法試試。

2018年6月8日 14:59