Apache POIのHSSFWorkbookを使用して、Excelスプレッドシートにデータを書き込みます。
行全体を太字にしたい。誰かがそれを行う方法を提案できますか?
このようなものはあなたが持っているもので動作しますか?
public static void makeRowBold(Workbook wb, Row row){
CellStyle style = wb.createCellStyle();//Create style
Font font = wb.createFont();//Create font
font.setBold(true);//Make font bold
style.setFont(font);//set it to bold
for(int i = 0; i < row.getLastCellNum(); i++){//For each cell in the row
row.getCell(i).setCellStyle(style);//Set the style
}
}
基本的に、渡された行の各セルを調べ、スタイルを太字に設定します。結果として、行全体が目的のスタイルに設定されます。
幸運を!
編集
より完全な例:
public static void main(String[] args) {
Path myFile = Paths.get(System.getProperty("user.home"), "Desktop", "tester.xlsx");
try {
XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(myFile.toFile()));
XSSFSheet sheet = wb.getSheetAt(0);
makeRowBold(wb, sheet.getRow(0));
wb.write(new FileOutputStream(myFile.toFile()));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void makeRowBold(Workbook wb, Row row){
CellStyle style = wb.createCellStyle();//Create style
Font font = wb.createFont();//Create font
font.setBold(true);//Make font bold
style.setFont(font);//set it to bold
for(int i = 0; i < row.getLastCellNum(); i++){//For each cell in the row
row.getCell(i).setCellStyle(style);//Set the sty;e
}
}
これは、行1のデータを含むxlsxファイルでテストされました。結果のファイルには、後で太字のデータが含まれていました。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加