Class ExcelWriteUtil

java.lang.Object
jp.ecuacion.util.poi.excel.util.ExcelWriteUtil

public class ExcelWriteUtil extends Object
Provides excel writing related apache POI utility methods.
  • Method Summary

    Modifier and Type
    Method
    Description
    static org.apache.poi.ss.usermodel.Workbook
    Creates new workbook with adding sheet of name sheetName.
    static void
    evaluateFormula(org.apache.poi.ss.usermodel.Cell cell, String fileInfo)
    Catches Exceptions which are thrown when workbook.getCreationHelper().createFormulaEvaluator().evaluateAll() is called and changes it to a ExcelAppException with an appropriate message.
    static void
    evaluateFormula(org.apache.poi.ss.usermodel.Workbook workbook, String fileInfo, boolean breaksOnError)
    Catches Exceptions which are thrown when workbook.getCreationHelper().createFormulaEvaluator().evaluateFormulaCell() is called and changes it to a ExcelAppException with an appropriate message.
    static void
    evaluateFormula(org.apache.poi.ss.usermodel.Workbook workbook, String fileInfo, boolean breaksOnError, String... sheetNames)
    Catches Exceptions which are thrown when workbook.getCreationHelper().createFormulaEvaluator().evaluateFormulaCell() is called and changes it to a ExcelAppException with an appropriate message.
    static void
    getReadyToEvaluateFormula(org.apache.poi.ss.usermodel.Cell cell, boolean changesNumberString, boolean changesDateString, boolean changesCellsWithTextDataFormat, String[] dateFormats)
    There are patterns where excel can evaluate formulas but POI cannot.
    getReadyToWriteTableData(ExcelTableWriter<T> writer, org.apache.poi.ss.usermodel.Workbook workbook, String sheetName, int tableStartColumnNumber)
    Gets ready to write table data.
    Opens the excel file and returns Workbook object.
    static org.apache.poi.ss.usermodel.Workbook
    openForWrite(String filePath)
    Opens the excel file and returns Workbook object.
    static void
    saveToFile(org.apache.poi.ss.usermodel.Workbook workbook, FileOutputStream out)
    Opens the excel file and returns Workbook object.
    static <T> void
    writeTableLine(ExcelTableWriter<T> writer, ExcelTable.ContextContainer context, int rowNumber, List<T> columnList)
    Provides common procedure for write one line of a table.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • createWorkbookWithSheet

      public static org.apache.poi.ss.usermodel.Workbook createWorkbookWithSheet(String sheetName)
      Creates new workbook with adding sheet of name sheetName.
      Parameters:
      sheetName - sheetName
      Returns:
      Workbook
    • openForWrite

      public static org.apache.poi.ss.usermodel.Workbook openForWrite(String filePath) throws org.apache.poi.EncryptedDocumentException, IOException
      Opens the excel file and returns Workbook object.
      Parameters:
      filePath - filePath
      Returns:
      workbook
      Throws:
      org.apache.poi.EncryptedDocumentException - EncryptedDocumentException
      IOException - IOException
    • openForOutput

      public static FileOutputStream openForOutput(String filePath) throws org.apache.poi.EncryptedDocumentException, IOException
      Opens the excel file and returns Workbook object.
      Parameters:
      filePath - filePath
      Returns:
      workbook
      Throws:
      org.apache.poi.EncryptedDocumentException - EncryptedDocumentException
      IOException - IOException
    • saveToFile

      public static void saveToFile(org.apache.poi.ss.usermodel.Workbook workbook, FileOutputStream out) throws org.apache.poi.EncryptedDocumentException, IOException
      Opens the excel file and returns Workbook object.
      Throws:
      org.apache.poi.EncryptedDocumentException
      IOException
    • getReadyToWriteTableData

      public static <T> ExcelTable.ContextContainer getReadyToWriteTableData(ExcelTableWriter<T> writer, org.apache.poi.ss.usermodel.Workbook workbook, String sheetName, int tableStartColumnNumber) throws jp.ecuacion.util.poi.excel.exception.ExcelAppException
      Gets ready to write table data.
      Throws:
      jp.ecuacion.util.poi.excel.exception.ExcelAppException
    • writeTableLine

      public static <T> void writeTableLine(ExcelTableWriter<T> writer, ExcelTable.ContextContainer context, int rowNumber, List<T> columnList)
      Provides common procedure for write one line of a table.
    • getReadyToEvaluateFormula

      public static void getReadyToEvaluateFormula(org.apache.poi.ss.usermodel.Cell cell, boolean changesNumberString, boolean changesDateString, boolean changesCellsWithTextDataFormat, String[] dateFormats)
      There are patterns where excel can evaluate formulas but POI cannot. This method cares about it and let POI do the same evaluation as excel.

      Let's say A1 cell is CellType == STRING and value = "2025/01/01", and A2 cell is a formula cell with formula =A1+1.
      Excel can evaluate A2 and shows 45659 (Serial number which expresses "2025/1/2"), but POI cannot.
      So it change a date format string value (like "2025/01/01") to number (like 45658) when CellType is STRING and its value matches the date format.
      But it won't change the value when the dataFormat == 49 because 49 means format is "text" and that means a user clearly wants to treat the value as a string format.

      Number format is exactly the same case as the date case above. Data format allows comma.

      Parameters:
      cell - cell
      changesNumberString - whether it changes number format string
      changesDateString - whether it changes number format string
      changesCellsWithTextDataFormat - That Data format is "text" means that a user seems explicitly to set it because the user wants to treat it as "text" format. By setting this value true, the method applies the change even if the cell is "text" format.
      dateFormats - dateFormats like yyyy/MM/dd (java.time.format.DateTimeFormatter format), which can be null when changesDateString is false. When changesDateString is true it cannot be null and element length must be more than or equal to 1.
    • evaluateFormula

      public static void evaluateFormula(org.apache.poi.ss.usermodel.Workbook workbook, String fileInfo, boolean breaksOnError) throws jp.ecuacion.lib.core.exception.checked.AppException
      Catches Exceptions which are thrown when workbook.getCreationHelper().createFormulaEvaluator().evaluateFormulaCell() is called and changes it to a ExcelAppException with an appropriate message.

      When an excel file is created and uploaded by users, Exceptions according to the content of the file should be understandable to the users.

      Parameters:
      workbook - workbook
      fileInfo - filename or file path of the excel file to add to the message
      Throws:
      jp.ecuacion.lib.core.exception.checked.AppException - AppException
    • evaluateFormula

      public static void evaluateFormula(org.apache.poi.ss.usermodel.Workbook workbook, String fileInfo, boolean breaksOnError, String... sheetNames) throws jp.ecuacion.lib.core.exception.checked.AppException
      Catches Exceptions which are thrown when workbook.getCreationHelper().createFormulaEvaluator().evaluateFormulaCell() is called and changes it to a ExcelAppException with an appropriate message.

      When an excel file is created and uploaded by users, Exceptions according to the content of the file should be understandable to the users.

      Parameters:
      workbook - workbook
      fileInfo - filename or file path of the excel file to add to the message
      sheetNames - array of sheet names you want to evaluate
      Throws:
      jp.ecuacion.lib.core.exception.checked.AppException - AppException
    • evaluateFormula

      public static void evaluateFormula(org.apache.poi.ss.usermodel.Cell cell, String fileInfo) throws jp.ecuacion.util.poi.excel.exception.ExcelAppException
      Catches Exceptions which are thrown when workbook.getCreationHelper().createFormulaEvaluator().evaluateAll() is called and changes it to a ExcelAppException with an appropriate message.

      When an excel file is created and uploaded by users, Exceptions according to the content of the file should be understandable to the users.

      Parameters:
      cell - target cell you want to evaluate
      fileInfo - filename or file path of the excel file to add to the message
      Throws:
      jp.ecuacion.util.poi.excel.exception.ExcelAppException - ExcelAppException