!以下是修复后的代码可以保留时间格式:import javaio;import javautilIterator;import orgapachepoissusermodel;import orgapachepoixssfusermodel;import orgapachepoihssfusermodel;import orgapachepoissutil
修复后的代码如下,可以保留时间格式并输出纯数字格式:
import java.io.*; import java.util.Iterator;
import org.apache.poi.ss.usermodel.; import org.apache.poi.xssf.usermodel.; import org.apache.poi.hssf.usermodel.; import org.apache.poi.ss.util.; import org.apache.poi.hssf.util.*; import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
public class test12 { public static void main(String[] args) throws Exception { String excelFilePath = "input.xlsx"; File inputFile = new File(excelFilePath);
//Get the workbook instance for XLSX file
Workbook workbook = null;
if (excelFilePath.endsWith("xlsx")) {
workbook = new XSSFWorkbook(new FileInputStream(inputFile));
} else if (excelFilePath.endsWith("xls")) {
workbook = new HSSFWorkbook(new FileInputStream(inputFile));
}
//Get the first sheet from the workbook
Sheet sheet = workbook.getSheetAt(0);
//Create a new HTML table
StringBuffer htmlTable = new StringBuffer("<table>");
//Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
//Create a new HTML table row
htmlTable.append("<tr>");
//Iterate through each columns from the row
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
//Create a new HTML table cell
htmlTable.append("<td>");
//Get the cell value and append to HTML table cell
switch (cell.getCellType()) {
case STRING:
htmlTable.append(cell.getStringCellValue());
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
htmlTable.append(cell.getDateCellValue().getTime());
} else {
htmlTable.append(cell.getNumericCellValue());
}
break;
case BOOLEAN:
htmlTable.append(cell.getBooleanCellValue());
break;
case FORMULA:
htmlTable.append(cell.getCellFormula());
break;
case BLANK:
htmlTable.append("");
break;
default:
htmlTable.append(cell);
}
//Close the HTML table cell
htmlTable.append("</td>");
}
//Close the HTML table row
htmlTable.append("</tr>");
}
//Close the HTML table
htmlTable.append("</table>");
//Print the HTML table
System.out.println(htmlTable.toString());
//Close the workbook
workbook.close();
}
原文地址: https://www.cveoy.top/t/topic/hltX 著作权归作者所有。请勿转载和采集!