I have a String array with values as below:
String[] myArray = new String[4];
myArray[0] = "one";
myArray[1] = "2012-02-25";
myArray[2] = "12345.58";
myArray[3] = "1245";
I want to write this array in to a excel sheet using POI where each value will go in a new column as below:
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("mySheet");
Row myRow = sheet.createRow(1);
Cell myCell;
for(int i=0;i < my.length;i++){
myCell = myRow.createCell(i);
myCell.setCellValue(myArray[i]);
sheet.autoSizeColumn(i);
}
FileOutputStream out;
try {
out = new FileOutputStream("myOutputFile");
wb.write(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
However, as obvious, everything will be dumped in the excel sheet as text. And I get a green colored notification on top left of every cell indicating "number stored as text", etc.
Assuming, that I will be unable to change the way I get the input because of source system limitations i.e. I will continue getting input only as Strings, how can I process the format of actual value inside the String and dump a date as date in excel, a number as number, a text as text and a double value as a properly decimal formatted double value ?
Please note that I do not have the information about the type of value I am getting in the String. I have to decide it at runtime whether the value is a text, number, float, date, etc and then put in appropriately in the excel sheet.
Please do not flag the question before reading properly.
Thanks for reading!