You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Pari Gandhi <pa...@essential.com.au> on 2006/03/22 05:07:31 UTC
HSSFListener - parse dates
Hi I have written a program to convert an xls file to csv format & and Iam
having problems with parsing date values.
I have uploaded my test xls file at
<http://rapidshare.de/files/16111340/x.xls.html>
Expected Output:
ABC,10/10/2005,12/10/2007,10/10/2005,123.12451,1234.125235,BDS
XXX,01/01/2012,24/11/1984,21/02/2005,214.234,124.234,SSS
Actual Output:
ABC,38635.0,12/10/2007,10/10/2005,02/05/1900,1234.125235,BDS
XXX,40909.0,24/11/1984,21/02/2005,01/08/1900,124.234,SSS
Can someone help me to solve this problem?
Thanks
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import org.apache.poi.hssf.eventusermodel.*;
import org.apache.poi.hssf.record.*;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class EventExample implements HSSFListener {
private SSTRecord sstrec;
private static String s = "";
public void processRecord(Record record) {
switch (record.getSid()) {
case NumberRecord.sid:
NumberRecord numrec = (NumberRecord) record;
if (numrec.getColumn() == 0 && numrec.getRow() > 0) {
s += "\n";
} else {
s += (numrec.getColumn() == 0 && numrec.getRow() == 0 ? "" :
",");
}
s += (HSSFDateUtil.isInternalDateFormat(numrec.getXFIndex()) ?
new SimpleDateFormat("dd/MM/yyyy").
format(HSSFDateUtil.getJavaDate(numrec.getValue())) : numrec.getValue());
break;
case SSTRecord.sid:
sstrec = (SSTRecord) record;
break;
case LabelSSTRecord.sid:
LabelSSTRecord lrec = (LabelSSTRecord) record;
if (lrec.getColumn() == 0 && lrec.getRow() > 0) {
s += "\n";
} else {
s += (lrec.getColumn() == 0 && lrec.getRow() == 0 ? "" :
",");
}
s += sstrec.getString(lrec.getSSTIndex());
break;
}
}
public static void main(String[] args) throws IOException {
FileInputStream fin = new FileInputStream(args[0]);
POIFSFileSystem poifs = new POIFSFileSystem(fin);
InputStream din = poifs.createDocumentInputStream("Workbook");
HSSFRequest req = new HSSFRequest();
req.addListenerForAllRecords(new EventExample());
HSSFEventFactory factory = new HSSFEventFactory();
factory.processEvents(req, din);
fin.close();
din.close();
System.out.println(s);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List: http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project: http://jakarta.apache.org/poi/