I wrote some code to extract data from xls and save them into csv. Everything is working great except when I have a time value, it is changing it to a date. How can I avoid that?
For example, if in the xls file it is 119:22:00, it is getting saved to the csv file as 1/4/1900 23:23 with format type as date. How can I keep it the way it is with format as String?
Here is the code:
if (sheet != null)
{
for (currentRow = 0; currentRow < sheet.getRows(); currentRow++)
{
row = new ArrayList<String>();
Cell[] rowCells = sheet.getRow(currentRow);
for (Cell cell : rowCells)
{
System.out.println(cell.getContents());
}
}
}
You need to get the Excel.Cell.Text rather than the Excel.Cell.Value to see the formatted value. Clearly what you’re extracting is the value. Based on your code, it looks like the problem will be in
cell.getContents()orsheet.getRow()– whichever is used to extract the data from excel. You should post the content of those functions too.The underlying values for dates and times in Excel are very hard to deal with, and meaningless to the novice user in their raw format. You can see the underlying value Excel translates your dates and times to just by opening up your excel sheet and temporarily changing the format to
number. You’ll see that as soon as you type in a date or time, it’s translating it to a numeric representation of that date or time that will be meaningless to the user, and thus meaningless to you if you extract the underlying value rather than the formatted text.