转载自:http://kinkding.iteye.com/blog/789600
JXL对于日期类型的处理,有些特殊需要注意:
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.JXLException;
import jxl.Sheet;
import jxl.Workbook;
public class Test {
public static void main(String[] args) {
Test t = new Test();
try {
t.read();
} catch (Exception e) {
e.printStackTrace();
}
}
public void read() throws JXLException, IOException {
Workbook wb = Workbook.getWorkbook(new File("input2.xls"));
Sheet sheet = wb.getSheet(0);
for (int i = 0, rowSize = sheet.getRows(); i < rowSize; i++) {
for (Cell c : sheet.getRow(i)) {
CellType type = c.getType();
if (type.equals(CellType.DATE)) {
// 日期 类型的处理
DateCell dc = (DateCell) c;
Date jxlDate = dc.getDate();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(sdf.format(jxlDate));
}
}
}
wb.close();
}
}
比较特别就是format的时候,必须采用标准的GMT时区,否则就会出错了。