カンマやパーセントが無いと、見づらいよ~。
Apache POIを使って、Excelでカンマ、パーセントの書式を設定するサンプル
Apache POIを使ってExcelファイルを作成します。
 また、2つのセルに対して、それぞれカンマ、パーセントの書式を適用します。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.DataFormat; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelDataFormatTest {     public static void main(String[] args) {         //ワークブック作成         try (Workbook workbook = new XSSFWorkbook()) {             //シート作成             Sheet sheet = workbook.createSheet("しーと1");             //セルにデータ書き込み             Row row = sheet.createRow(0);             //カンマとパーセントの書式を作成             DataFormat dataFormat = workbook.createDataFormat();             short commaFormat = dataFormat.getFormat("#,##0");             short percentFormat = dataFormat.getFormat("0.0%");             //1つめのセルはカンマ。             CellStyle cellStyle = workbook.createCellStyle();             cellStyle.setDataFormat(commaFormat);             Cell cell = row.createCell(0);             cell.setCellValue(1234567);             cell.setCellStyle(cellStyle);             //2つめのセルはパーセント。             cellStyle = workbook.createCellStyle();             cellStyle.setDataFormat(percentFormat);             cell = row.createCell(1);             cell.setCellValue(0.123);             cell.setCellStyle(cellStyle);             //ファイル出力             try (FileOutputStream outputStream = new FileOutputStream("c:\\work\\test.xlsx")) {                 workbook.write(outputStream);             }         } catch (IOException e) {             e.printStackTrace();         }     } } | 
実行結果
Excelファイルが作成されます。
 また、2つのセルに対して、それぞれカンマ、パーセントの書式が適用されます。
 
サンプルの解説
Workbook#createDataFormat()で、データフォーマットを作成。
 作成したデータフォーマットから、DataFormat#getFormat(String)で書式を作成します。
 あとは、セルスタイルに書式を適用するだけですね。
※このコードを使用するには、別途Apache POIの入手が必要です。
 入手方法などはこちらの記事に書いてあります。















