天天看點

POI 建立單元格 并給值

Workbook wb = new HSSFWorkbook()//.xls

    //Workbook wb = new XSSFWorkbook();//.xlsx

    CreationHelper createHelper = wb.getCreationHelper();

    Sheet sheet = wb.createSheet("new sheet");



    // Create a row and put some cells in it. Rows are 0 based.

    //建立行

    Row row = sheet.createRow((short)0);

    // Create a cell and put a value in it.

    //建立單元格

    Cell cell = row.createCell(0);

    //設定單元格的值為1

    cell.setCellValue(1);



    // 另外一種設定方法

    row.createCell(1).setCellValue(1.2);

    row.createCell(2).setCellValue(

         createHelper.createRichTextString("This is a string"));

    row.createCell(3).setCellValue(true);



    // Write the output to a file

    FileOutputStream fileOut = new FileOutputStream("workbook.xls");

    wb.write(fileOut);

    fileOut.close();