Elements trs = table.select("tr");
int firstRow = sheet.getFirstRowNum();
int lastRow = sheet.getLastRowNum();
for(int i=0;i<=lastRow;i++){
if(i<trs.size()){
Element tr = trs.get(i);
if(i<firstRow){//代表是空Cell,直接添加一个空的td
for(int j=0;j<totalColumns;j++){
tr.appendElement("td");
}
}else{
Row row = sheet.getRow(i);
if(row==null){
for(int j=0;j<totalColumns;j++){
tr.appendElement("td");
}
}else{
int firstColumn = row.getFirstCellNum();
int lastColumn = row.getLastCellNum();
for(int j=0;j<totalColumns;j++){
if(j<firstColumn){
tr.appendElement("td");
}else{
Cell cell = row.getCell(j);
Element td = tr.appendElement("td");
if(cell!=null){
if(cell.getCellType()==Cell.CELL_TYPE_FORMULA){
td.attr("formula","="+cell.getCellFormula());
}else if(cell.getCellType()==Cell.CELL_TYPE_BLANK){
}else if(cell.getCellType()==Cell.CELL_TYPE_BOOLEAN){
td.text(String.valueOf(cell.getBooleanCellValue()));
}else if(cell.getCellType()==Cell.CELL_TYPE_ERROR){
td.text("Error:"+String.valueOf(cell.getErrorCellValue()));
}else if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC){
String strValue = null;
if(cell instanceof HSSFCell){
HSSFCell hssfCell = (HSSFCell)cell;
if(HSSFDateUtil.isCellDateFormatted(hssfCell)){
// Date date = HSSFDateUtil.getJavaDate(dv);
HSSFCellStyle cellStyle = hssfCell.getCellStyle();
if(cellStyle!=null && cellStyle.getDataFormatString()!=null){
String dateFmt = cell.getCellStyle().getDataFormatString();
/* strValue = new SimpleDateFormat(dateFmt).format(date); - won't work as
Java fmt differs from Excel fmt. If Excel date format is mm/dd/yyyy, Java
will always be 00 for date since "m" is minutes of the hour.*/
strValue = new CellDateFormatter(dateFmt).format(hssfCell.getDateCellValue());
}else{
strValue = WebVariable.sdf.format(hssfCell.getDateCellValue());
}
// td.text(strValue);
}else{
HSSFCellStyle cellStyle = hssfCell.getCellStyle();
if(cellStyle!=null && cellStyle.getDataFormatString()!=null){
// String dataFmt = cellStyle.getDataFormatString();
strValue = new HSSFDataFormatter().formatRawCellContents(hssfCell.getNumericCellValue(), cellStyle.getDataFormat(), cellStyle.getDataFormatString());
}else{
strValue = String.valueOf(hssfCell.getNumericCellValue());
}
}
}else{
XSSFCell xssfCell = (XSSFCell)cell;
if(HSSFDateUtil.isCellDateFormatted(cell)){
String dateFmt = cell.getCellStyle().getDataFormatString();
/* strValue = new SimpleDateFormat(dateFmt).format(date); - won't work as
Java fmt differs from Excel fmt. If Excel date format is mm/dd/yyyy, Java
will always be 00 for date since "m" is minutes of the hour.*/
strValue = new CellDateFormatter(dateFmt).format(cell.getDateCellValue());
// td.text(strValue);
}else{
XSSFCellStyle cellStyle = xssfCell.getCellStyle();
if(cellStyle!=null && cellStyle.getDataFormatString()!=null){
// String dateFmt = cell.getCellStyle().getDataFormatString();
/* strValue = new SimpleDateFormat(dateFmt).format(date); - won't work as
Java fmt differs from Excel fmt. If Excel date format is mm/dd/yyyy, Java
will always be 00 for date since "m" is minutes of the hour.*/
strValue = new HSSFDataFormatter().formatRawCellContents(cell.getNumericCellValue(), cellStyle.getDataFormat(), cellStyle.getDataFormatString());
}else{
strValue = String.valueOf(xssfCell.getNumericCellValue());
}
// xssfCell.getCellStyle().getDataFormatString();
// td.text(String.valueOf(cell.getNumericCellValue()));
}
}
td.text(strValue);
}else if(cell.getCellType()==Cell.CELL_TYPE_STRING){
td.text(cell.getStringCellValue());
}
if(sheet instanceof HSSFSheet){
HSSFSheet hssfSheet = (HSSFSheet)sheet;
HSSFCell hssfCell = (HSSFCell)cell;
HSSFCellStyle style = hssfCell.getCellStyle();