Untitled

mail@pastecode.io avatar
unknown
java
a year ago
2.0 kB
2
Indexable
Never
public static void main(String[] args) throws Exception {
    

    Files.deleteIfExists(Paths.get("D:/1/exceltest.xlsx"));

    SXSSFWorkbook wb = new SXSSFWorkbook();
    SXSSFSheet sheet = wb.createSheet("sheet");
    SXSSFRow row = sheet.createRow(0);

    SXSSFCell cell = row.createCell(0);
    cell.setCellValue(12.12568);
    cell = row.createCell(1);
    cell.setCellValue(true);
    cell = row.createCell(2);
    cell.setCellValue(new GregorianCalendar());
    cell = row.createCell(3);
    cell.setCellValue(new Date());
    cell = row.createCell(4);
    cell.setCellValue("String");


    wb.write(Files.newOutputStream(Paths.get("D:/1/exceltest.xlsx"), StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW));
    wb.close();

    XSSFWorkbook in = new XSSFWorkbook("D:/1/exceltest.xlsx");
    XSSFSheet s = in.getSheet("sheet");
    XSSFRow r = s.getRow(0);
    XSSFCell c;

    for ( int i = 0; i <= 4; i++ ) {
      c = r.getCell(i);
      System.out.println(c.getCellType());
      switch ( c.getCellType() ) {
        case NUMERIC:
          if ( DateUtil.isCellDateFormatted(c) ) {
            System.out.println("DATE");
            System.out.println(c.getCellStyle().getDataFormat());
            System.out.println(c.getCellStyle().getDataFormatString());
            Date dateCellValue = c.getDateCellValue();
            System.out.println(dateCellValue.toString());
          } else {
            System.out.println(c.getCellStyle().getDataFormat());
            System.out.println(c.getNumericCellValue());
          }
          break;
        case BOOLEAN:
          System.out.println(c.getBooleanCellValue());
          break;
        case FORMULA:
          System.out.println(c.getCachedFormulaResultType());
          break;
        case STRING:
        default:
          System.out.println(c.getStringCellValue());
          break;
      }

    }



  }