Introduction
FastExcel is an excel file read and write component developed using pure Java. Supports Excel'97 (-2003) (BIFF8) file format. FastExcel mainly focuses on the processing of excel content, so FastExcel can only read character information of cells, while other attributes such as colors, fonts, etc. are not supported. Since this extra information is not read, parsed and stored, FastExcel requires only a small amount of memory.
Sample code:
public void testDump() throws ParserException, ReadException { Workbook workBook; workBook = (new File("")); (); Sheet s; s = (0); ("SHEET:"+s); for (int i = (); i <= (); i++) { (i+"#"); for (int j = (); j <=(); j++) { (","+(i, j)); } (); } (); }
EasyExcel => FastExcel, import supports multiple time formats
InfoExcelDTO
/** * Cooperation start date* */ @ExcelProperty(index = 22,converter = ) private Date cooperationDate;
ExcelDateConverter
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import org.; import org.; /** * Date format converter */ public class ExcelDateConverter implements Converter<Date> { private static final Logger log = (); // Define all date formats to try SimpleDateFormat[] formats = { new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"), new SimpleDateFormat("yyyy/MM/dd"), new SimpleDateFormat("yyyy-MM-dd"), new SimpleDateFormat("yyyy/MM"), new SimpleDateFormat("yyyy/MM"), new SimpleDateFormat("yyyyMMdd") }; @Override public Class<Date> supportJavaTypeKey() { return ; } @Override public CellDataTypeEnum supportExcelTypeKey() { return ; } @Override public Date convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception { String cellValue = ""; if (().equals()) { long cellIntValue = ().longValue(); if (cellIntValue > 19900100) { try { // 1. The first analysis is passed on to a date in the form of a number, which is like yyyyMMdd SimpleDateFormat originalFormat = new SimpleDateFormat("yyyyMMdd"); return ((cellIntValue)); } catch (Exception e) { ("exception when parse numerical time with format yyyyMMdd"); cellValue=(cellIntValue); } } // 2. The second analysis, excel starts from 1900, and finally calculates the target date by calculating the number of days between the 1900 years LocalDate localDate = (1900, 1, 1); //Excel has some strange bug, causing a 2-date difference in dates localDate = (cellIntValue - 2); // Convert to ZonedDateTime (if time zone information is required) ZonedDateTime zonedDateTime = (()); return (()); } else if (().equals()) { // 3. The third analysis Date date = null; cellValue = (); for (SimpleDateFormat format : formats) { try { date = (cellValue); if (date != null) { // This step is to format the date into the format that Java expects return date; } } catch (Exception e) { // If there is an exception, continue to parse after catching the exception ((), e); } } } // If there is an exception, continue to parse after catching the exception throw new UnsupportedOperationException("The current operation is not supported by the current converter." + cellValue); } @Override public WriteCellData<?> convertToExcelData(Date value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String dateValue = (value); return new WriteCellData<>(dateValue); } }
This is the end of this article about Java importing using FastExcel to support multiple time formats. For more related Java FastExcel time format content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!