0

Is there any functionality to auto fill the data in the cells with the correct type? Now I have to manually set the types, f.e. String or Integer.

Code now is:

for (String colName : headerNames) {
    dataRow.createCell(colIndex++).setCellValue(rs.getString(colName));
}

At this moment I have to declare for each cell separately what type to use with rs.getString(colName). As the data contains both Strings as Integers, it would be handy if they could me automatically typed.

1 Answer 1

1

There is no functionality inside POI and it would be hard to provide one for Dates as there are a myriad of different date formats in use, but it should be fairly easy to come up with a helper method that you can use in your project, e.g. something like

public static void setCellValue(Cell cell, String value) {
    try {
        cell.setCellValue(Double.parseDouble(value));
    } catch (NumberFormatException e) {
        // not a number, let's use String
        cell.setCellValue(value);
    }
}    

This can easily be extended to a date-format as well if you know the actual format that the date is coming in.

Sign up to request clarification or add additional context in comments.

2 Comments

Well, I wrote myself a solution. I created a new array which holds the fields which are Integers, and do a check if the column name is in that array. It would be handy if there was a functionality for automatically assigning the type, so I gave it a try and asked :-)
Please report an enhancement request at bz.apache.org/bugzilla if you have an idea how a fairly performant general functionality could look like!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.