diff --git a/src/main/java/com/alibaba/excel/ExcelReader.java b/src/main/java/com/alibaba/excel/ExcelReader.java index 07378d22..4fcb3cab 100644 --- a/src/main/java/com/alibaba/excel/ExcelReader.java +++ b/src/main/java/com/alibaba/excel/ExcelReader.java @@ -285,7 +285,7 @@ public class ExcelReader { } try { excelAnalyser.finish(); - } catch (Exception e) { + } catch (Throwable e) { LOGGER.warn("Destroy object failed", e); } } diff --git a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java index b24a182e..c0818187 100644 --- a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java +++ b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java @@ -1,7 +1,5 @@ package com.alibaba.excel.analysis; -import java.io.IOException; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,7 +32,7 @@ public class ExcelAnalyserImpl implements ExcelAnalyser { } catch (RuntimeException e) { finish(); throw e; - } catch (Exception e) { + } catch (Throwable e) { finish(); throw new ExcelAnalysisException(e); } @@ -72,7 +70,7 @@ public class ExcelAnalyserImpl implements ExcelAnalyser { } catch (RuntimeException e) { finish(); throw e; - } catch (Exception e) { + } catch (Throwable e) { finish(); throw new ExcelAnalysisException(e); } @@ -88,14 +86,22 @@ public class ExcelAnalyserImpl implements ExcelAnalyser { if (readWorkbookHolder.getReadCache() != null) { readWorkbookHolder.getReadCache().destroy(); } + } catch (Throwable e) { + throw new ExcelAnalysisException("Can not close IO", e); + } + try { if (analysisContext.readWorkbookHolder().getAutoCloseStream() && readWorkbookHolder.getInputStream() != null) { readWorkbookHolder.getInputStream().close(); } + } catch (Throwable e) { + throw new ExcelAnalysisException("Can not close IO", e); + } + try { if (readWorkbookHolder.getTempFile() != null) { FileUtils.delete(readWorkbookHolder.getTempFile()); } - } catch (IOException e) { + } catch (Throwable e) { throw new ExcelAnalysisException("Can not close IO", e); } } diff --git a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java index 92754360..8df8bfd8 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java +++ b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java @@ -14,7 +14,10 @@ import org.apache.poi.hssf.eventusermodel.HSSFListener; import org.apache.poi.hssf.eventusermodel.HSSFRequest; import org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener; import org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord; +import org.apache.poi.hssf.record.ExtendedFormatRecord; import org.apache.poi.hssf.record.Record; +import org.apache.poi.hssf.record.StyleRecord; +import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java index b18a89e2..23aff5a3 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java @@ -30,6 +30,8 @@ public class NumberRecordHandler extends AbstractXlsRecordHandler { this.row = numrec.getRow(); this.column = numrec.getColumn(); this.cellData = new CellData(numrec.getValue()); + this.cellData.setDataFormat(formatListener.getFormatIndex(numrec)); + this.cellData.setDataFormatString(formatListener.getFormatString(numrec)); } @Override diff --git a/src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java index de924c91..9d228d2a 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java @@ -1,5 +1,6 @@ package com.alibaba.excel.analysis.v07; +import org.xml.sax.Attributes; import org.xml.sax.helpers.DefaultHandler; import com.alibaba.excel.cache.ReadCache; @@ -11,22 +12,40 @@ import com.alibaba.excel.cache.ReadCache; */ public class SharedStringsTableHandler extends DefaultHandler { private static final String T_TAG = "t"; + private static final String SI_TAG = "si"; + /** + * The final piece of data + */ private String currentData; + /** + * Current element data + */ + private String currentElementData; + private ReadCache readCache; public SharedStringsTableHandler(ReadCache readCache) { this.readCache = readCache; } + @Override + public void startElement(String uri, String localName, String name, Attributes attributes) { + if (SI_TAG.equals(name)) { + currentData = ""; + } + } + @Override public void endElement(String uri, String localName, String name) { if (T_TAG.equals(name)) { + currentData += currentElementData; + } else if (SI_TAG.equals(name)) { readCache.put(currentData); } } @Override public void characters(char[] ch, int start, int length) { - currentData = new String(ch, start, length); + currentElementData = new String(ch, start, length); } } diff --git a/src/main/java/com/alibaba/excel/analysis/v07/XlsxHandlerFactory.java b/src/main/java/com/alibaba/excel/analysis/v07/XlsxHandlerFactory.java index 7fd1dc14..82f7d2c5 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/XlsxHandlerFactory.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/XlsxHandlerFactory.java @@ -3,6 +3,8 @@ package com.alibaba.excel.analysis.v07; import java.util.ArrayList; import java.util.List; +import org.apache.poi.xssf.model.StylesTable; + import com.alibaba.excel.analysis.v07.handlers.CountRowCellHandler; import com.alibaba.excel.analysis.v07.handlers.DefaultCellHandler; import com.alibaba.excel.analysis.v07.handlers.ProcessResultCellHandler; @@ -10,14 +12,14 @@ import com.alibaba.excel.context.AnalysisContext; /** * Build handler - * + * * @author Dan Zheng */ public class XlsxHandlerFactory { - public static List buildCellHandlers(AnalysisContext analysisContext) { + public static List buildCellHandlers(AnalysisContext analysisContext, StylesTable stylesTable) { List result = new ArrayList(); result.add(new CountRowCellHandler(analysisContext)); - DefaultCellHandler defaultCellHandler = new DefaultCellHandler(analysisContext); + DefaultCellHandler defaultCellHandler = new DefaultCellHandler(analysisContext, stylesTable); result.add(defaultCellHandler); result.add(new ProcessResultCellHandler(analysisContext, defaultCellHandler)); return result; diff --git a/src/main/java/com/alibaba/excel/analysis/v07/XlsxRowHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/XlsxRowHandler.java index 1a1bbeee..8306fc15 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/XlsxRowHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/XlsxRowHandler.java @@ -2,6 +2,7 @@ package com.alibaba.excel.analysis.v07; import java.util.List; +import org.apache.poi.xssf.model.StylesTable; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; @@ -17,8 +18,8 @@ public class XlsxRowHandler extends DefaultHandler { private List cellHandlers; private XlsxRowResultHolder rowResultHolder; - public XlsxRowHandler(AnalysisContext analysisContext) { - this.cellHandlers = XlsxHandlerFactory.buildCellHandlers(analysisContext); + public XlsxRowHandler(AnalysisContext analysisContext, StylesTable stylesTable) { + this.cellHandlers = XlsxHandlerFactory.buildCellHandlers(analysisContext, stylesTable); for (XlsxCellHandler cellHandler : cellHandlers) { if (cellHandler instanceof XlsxRowResultHolder) { this.rowResultHolder = (XlsxRowResultHolder)cellHandler; diff --git a/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java b/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java index d7aaea9e..bda0e763 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java @@ -15,6 +15,7 @@ import javax.xml.parsers.SAXParserFactory; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.xssf.eventusermodel.XSSFReader; +import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.usermodel.XSSFRelation; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr; @@ -47,6 +48,10 @@ public class XlsxSaxAnalyser implements ExcelExecutor { private AnalysisContext analysisContext; private List sheetList; private Map sheetMap; + /** + * Current style information + */ + private StylesTable stylesTable; public XlsxSaxAnalyser(AnalysisContext analysisContext) throws Exception { this.analysisContext = analysisContext; @@ -64,9 +69,9 @@ public class XlsxSaxAnalyser implements ExcelExecutor { analysisSharedStringsTable(sharedStringsTablePackagePart.getInputStream(), readWorkbookHolder); XSSFReader xssfReader = new XSSFReader(pkg); - analysisUse1904WindowDate(xssfReader, readWorkbookHolder); + stylesTable = xssfReader.getStylesTable(); sheetList = new ArrayList(); sheetMap = new HashMap(); XSSFReader.SheetIterator ite = (XSSFReader.SheetIterator)xssfReader.getSheetsData(); @@ -94,12 +99,12 @@ public class XlsxSaxAnalyser implements ExcelExecutor { } if (size < USE_MAP_CACHE_SIZE) { if (LOGGER.isDebugEnabled()) { - LOGGER.info("Use map cache.size:{}", size); + LOGGER.debug("Use map cache.size:{}", size); } readWorkbookHolder.setReadCache(new MapCache()); } else { if (LOGGER.isDebugEnabled()) { - LOGGER.info("Use ehcache.size:{}", size); + LOGGER.debug("Use ehcache.size:{}", size); } readWorkbookHolder.setReadCache(new Ehcache()); } @@ -178,7 +183,7 @@ public class XlsxSaxAnalyser implements ExcelExecutor { @Override public void execute() { parseXmlSource(sheetMap.get(analysisContext.readSheetHolder().getSheetNo()), - new XlsxRowHandler(analysisContext)); + new XlsxRowHandler(analysisContext, stylesTable)); } } diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java index ba890bd4..671693b4 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java @@ -1,5 +1,6 @@ package com.alibaba.excel.analysis.v07.handlers; +import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_DATA_FORMAT_TAG; import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_FORMULA_TAG; import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_INLINE_STRING_VALUE_TAG; import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_TAG; @@ -9,6 +10,9 @@ import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_VALUE_TYPE_TAG; import java.util.Map; import java.util.TreeMap; +import org.apache.poi.ss.usermodel.BuiltinFormats; +import org.apache.poi.xssf.model.StylesTable; +import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFRichTextString; import org.xml.sax.Attributes; @@ -36,9 +40,14 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder private int curCol; private Map curRowContent = new TreeMap(); private CellData currentCellData; + /** + * Current style information + */ + private StylesTable stylesTable; - public DefaultCellHandler(AnalysisContext analysisContext) { + public DefaultCellHandler(AnalysisContext analysisContext, StylesTable stylesTable) { this.analysisContext = analysisContext; + this.stylesTable = stylesTable; } @Override @@ -75,6 +84,21 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder // t is null ,it's means Empty or Number CellDataTypeEnum type = CellDataTypeEnum.buildFromCellType(attributes.getValue(CELL_VALUE_TYPE_TAG)); currentCellData = new CellData(type); + + // Put in data transformation information + String dateFormatIndex = attributes.getValue(CELL_DATA_FORMAT_TAG); + if (dateFormatIndex != null) { + int dateFormatIndexInteger = Integer.parseInt(dateFormatIndex); + XSSFCellStyle xssfCellStyle = stylesTable.getStyleAt(dateFormatIndexInteger); + int dataFormat = xssfCellStyle.getDataFormat(); + String dataFormatString = xssfCellStyle.getDataFormatString(); + currentCellData.setDataFormat(dataFormat); + if (dataFormatString == null) { + currentCellData.setDataFormatString(BuiltinFormats.getBuiltinFormat(dataFormat)); + } else { + currentCellData.setDataFormatString(dataFormatString); + } + } } // cell is formula if (CELL_FORMULA_TAG.equals(name)) { diff --git a/src/main/java/com/alibaba/excel/constant/ExcelXmlConstants.java b/src/main/java/com/alibaba/excel/constant/ExcelXmlConstants.java index 55d522fa..f01a33b3 100644 --- a/src/main/java/com/alibaba/excel/constant/ExcelXmlConstants.java +++ b/src/main/java/com/alibaba/excel/constant/ExcelXmlConstants.java @@ -11,6 +11,10 @@ public class ExcelXmlConstants { public static final String ROW_TAG = "row"; public static final String CELL_TAG = "c"; public static final String CELL_VALUE_TYPE_TAG = "t"; + /** + * Number formatted label + */ + public static final String CELL_DATA_FORMAT_TAG = "s"; public static final String CELL_FORMULA_TAG = "f"; public static final String CELL_VALUE_TAG = "v"; /** diff --git a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java index ff2215e0..ebe0d269 100644 --- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java @@ -1,6 +1,5 @@ package com.alibaba.excel.context; -import java.io.IOException; import java.io.OutputStream; import java.util.List; import java.util.Map; @@ -347,19 +346,29 @@ public class WriteContextImpl implements WriteContext { try { writeWorkbookHolder.getWorkbook().write(writeWorkbookHolder.getOutputStream()); writeWorkbookHolder.getWorkbook().close(); - if (writeWorkbookHolder.getAutoCloseStream()) { - if (writeWorkbookHolder.getOutputStream() != null) { - writeWorkbookHolder.getOutputStream().close(); - } - if (writeWorkbookHolder.getTemplateInputStream() != null) { - writeWorkbookHolder.getTemplateInputStream().close(); - } - } else { - if (writeWorkbookHolder.getFile() != null && writeWorkbookHolder.getOutputStream() != null) { - writeWorkbookHolder.getOutputStream().close(); - } + } catch (Throwable e) { + throw new ExcelGenerateException("Can not close IO", e); + } + try { + if (writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getOutputStream() != null) { + writeWorkbookHolder.getOutputStream().close(); + } + } catch (Throwable e) { + throw new ExcelGenerateException("Can not close IO", e); + } + try { + if (writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getTemplateInputStream() != null) { + writeWorkbookHolder.getTemplateInputStream().close(); + } + } catch (Throwable e) { + throw new ExcelGenerateException("Can not close IO", e); + } + try { + if (!writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getFile() != null + && writeWorkbookHolder.getOutputStream() != null) { + writeWorkbookHolder.getOutputStream().close(); } - } catch (IOException e) { + } catch (Throwable e) { throw new ExcelGenerateException("Can not close IO", e); } if (LOGGER.isDebugEnabled()) { diff --git a/src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java b/src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java index 934135e2..62b25238 100644 --- a/src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java +++ b/src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java @@ -1,6 +1,7 @@ package com.alibaba.excel.converters.string; import org.apache.poi.hssf.usermodel.HSSFDateUtil; +import org.apache.poi.ss.usermodel.DateUtil; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; @@ -38,6 +39,19 @@ public class StringNumberConverter implements Converter { contentProperty.getDateTimeFormatProperty().getFormat()); } // If there are "NumberFormat", read as number + if (contentProperty != null && contentProperty.getNumberFormatProperty() != null) { + return NumberUtils.format(cellData.getDoubleValue(), contentProperty); + } + // Excel defines formatting + if (cellData.getDataFormat() != null) { + if (DateUtil.isADateFormat(cellData.getDataFormat(), cellData.getDataFormatString())) { + return DateUtils.format(HSSFDateUtil.getJavaDate(cellData.getDoubleValue(), + globalConfiguration.getUse1904windowing(), null)); + } else { + return NumberUtils.format(cellData.getDoubleValue(), contentProperty); + } + } + // Default conversion number return NumberUtils.format(cellData.getDoubleValue(), contentProperty); } diff --git a/src/main/java/com/alibaba/excel/metadata/CellData.java b/src/main/java/com/alibaba/excel/metadata/CellData.java index 03fee7a7..e03a883b 100644 --- a/src/main/java/com/alibaba/excel/metadata/CellData.java +++ b/src/main/java/com/alibaba/excel/metadata/CellData.java @@ -23,6 +23,14 @@ public class CellData { private Boolean booleanValue; private Boolean formula; private String formulaValue; + /** + * The number formatting + */ + private Integer dataFormat; + /** + * The string of number formatting + */ + private String dataFormatString; public CellData(CellData other) { this.type = other.type; @@ -31,6 +39,8 @@ public class CellData { this.booleanValue = other.booleanValue; this.formula = other.formula; this.formulaValue = other.formulaValue; + this.dataFormat = other.dataFormat; + this.dataFormatString = other.dataFormatString; } public CellData(String stringValue) { @@ -123,6 +133,22 @@ public class CellData { this.formulaValue = formulaValue; } + public Integer getDataFormat() { + return dataFormat; + } + + public void setDataFormat(Integer dataFormat) { + this.dataFormat = dataFormat; + } + + public String getDataFormatString() { + return dataFormatString; + } + + public void setDataFormatString(String dataFormatString) { + this.dataFormatString = dataFormatString; + } + @Override public String toString() { switch (type) { diff --git a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java index 25dee195..970fd3cb 100644 --- a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java +++ b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java @@ -51,7 +51,7 @@ public class ExcelBuilderImpl implements ExcelBuilder { } catch (RuntimeException e) { finish(); throw e; - } catch (Exception e) { + } catch (Throwable e) { finish(); throw new ExcelGenerateException(e); } @@ -88,7 +88,7 @@ public class ExcelBuilderImpl implements ExcelBuilder { } catch (RuntimeException e) { finish(); throw e; - } catch (Exception e) { + } catch (Throwable e) { finish(); throw new ExcelGenerateException(e); } diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java index 5e6978e0..85f5f39b 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java @@ -53,7 +53,7 @@ public class ConverterDataTest { } @Test - public void T03ReadAllConverter03() { + public void T04ReadAllConverter03() { readAllConverter("converter" + File.separator + "converter03.xls"); } diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java index 6ff280ec..4f758a02 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java @@ -42,4 +42,5 @@ public class ReadAllConverterData { private String stringError; private String stringFormulaNumber; private String stringFormulaString; + private String stringNumberDate; } diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java index d756a69b..f4f0a1e7 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java @@ -64,6 +64,7 @@ public class ReadAllConverterDataListener extends AnalysisEventListener list = EasyExcel.read("D:\\test\\styleTest.xls").sheet().headRowNumber(0).doReadSync(); + for (Object data : list) { + LOGGER.info("返回数据:{}", JSON.toJSONString(data)); + } + } + + @Test + public void poi() throws Exception { + InputStream is = new FileInputStream("D:\\test\\styleTest.xls"); + HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is); + HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0); + HSSFRow hssfRow = hssfSheet.getRow(0); + System.out.println(hssfRow.getCell(0).getCellStyle().getDataFormatString()); + DataFormatter formatter = new DataFormatter(); + System.out.println(hssfRow.getCell(0).getNumericCellValue()); + System.out.println(hssfRow.getCell(1).getNumericCellValue()); + System.out.println(hssfRow.getCell(2).getNumericCellValue()); + System.out.println(hssfRow.getCell(0).getCellStyle().getDataFormatString()); + System.out.println(hssfRow.getCell(1).getCellStyle().getDataFormatString()); + System.out.println(hssfRow.getCell(2).getCellStyle().getDataFormatString()); + + } + + @Test + public void poi07() throws Exception { + InputStream is = new FileInputStream("D:\\test\\styleTest.xlsx"); + Workbook workbook = WorkbookFactory.create(is); // 这种方式 Excel 2003/2007/2010 都是可以处理的 + Sheet sheet = workbook.getSheetAt(0); + Row hssfRow = sheet.getRow(0); + System.out.println(hssfRow.getCell(0).getCellStyle().getDataFormatString()); + DataFormatter formatter = new DataFormatter(); + System.out.println(hssfRow.getCell(0).getNumericCellValue()); + System.out.println(hssfRow.getCell(1).getNumericCellValue()); + System.out.println(hssfRow.getCell(2).getNumericCellValue()); + System.out.println(hssfRow.getCell(0).getCellStyle().getDataFormat()); + System.out.println(hssfRow.getCell(1).getCellStyle().getDataFormat()); + System.out.println(hssfRow.getCell(2).getCellStyle().getDataFormat()); + System.out.println(hssfRow.getCell(3).getCellStyle().getDataFormat()); + System.out.println(hssfRow.getCell(0).getCellStyle().getDataFormatString()); + System.out.println(hssfRow.getCell(1).getCellStyle().getDataFormatString()); + System.out.println(hssfRow.getCell(2).getCellStyle().getDataFormatString()); + System.out.println(hssfRow.getCell(3).getCellStyle().getDataFormatString()); + isDate(hssfRow.getCell(0)); + isDate(hssfRow.getCell(1)); + isDate(hssfRow.getCell(2)); + isDate(hssfRow.getCell(3)); + + } + + @Test + public void poi0701() throws Exception { + InputStream is = new FileInputStream("D:\\test\\f1.xlsx"); + Workbook workbook = WorkbookFactory.create(is); // 这种方式 Excel 2003/2007/2010 都是可以处理的 + Sheet sheet = workbook.getSheetAt(0); + print(sheet.getRow(0).getCell(0)); + print(sheet.getRow(1).getCell(0)); + print(sheet.getRow(2).getCell(0)); + print(sheet.getRow(3).getCell(0)); + } + + private void print(Cell cell) { + System.out.println( + DateUtil.isADateFormat(cell.getCellStyle().getDataFormat(), cell.getCellStyle().getDataFormatString())); + System.out.println(cell.getCellStyle().getDataFormat()); + System.out.println(cell.getCellStyle().getDataFormatString()); + DataFormatter f = new DataFormatter(); + System.out.println(f.formatCellValue(cell)); + if (cell.getCellStyle().getDataFormatString() != null) { + + } + ExcelStyleDateFormatter ff = new ExcelStyleDateFormatter(cell.getCellStyle().getDataFormatString()); + + } + + @Test + public void testFormatter() throws Exception { + ExcelStyleDateFormatter ff = new ExcelStyleDateFormatter("yyyy年m月d日"); + + System.out.println(ff.format(new Date())); + } + + private void isDate(Cell cell) { + System.out.println( + DateUtil.isADateFormat(cell.getCellStyle().getDataFormat(), cell.getCellStyle().getDataFormatString())); + System.out.println(HSSFDateUtil.isCellDateFormatted(cell)); + DataFormatter f = new DataFormatter(); + System.out.println(f.formatCellValue(cell)); + + } + + @Test + public void testBuiltinFormats() throws Exception { + System.out.println(BuiltinFormats.getBuiltinFormat(48)); + System.out.println(BuiltinFormats.getBuiltinFormat(57)); + System.out.println(BuiltinFormats.getBuiltinFormat(28)); + + } + +} diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiFormatTest.java b/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiFormatTest.java new file mode 100644 index 00000000..99885fa9 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiFormatTest.java @@ -0,0 +1,53 @@ +package com.alibaba.easyexcel.test.temp.poi; + +import java.io.File; +import java.io.IOException; + +import org.apache.poi.ss.usermodel.DateUtil; +import org.apache.poi.xssf.streaming.SXSSFRow; +import org.apache.poi.xssf.streaming.SXSSFSheet; +import org.apache.poi.xssf.streaming.SXSSFWorkbook; +import org.apache.poi.xssf.usermodel.XSSFRow; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.Ignore; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.alibaba.easyexcel.test.util.TestFileUtil; + +/** + * 测试poi + * + * @author Jiaju Zhuang + **/ +@Ignore +public class PoiFormatTest { + private static final Logger LOGGER = LoggerFactory.getLogger(PoiFormatTest.class); + + @Test + public void lastRowNum() throws IOException { + String file = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; + SXSSFWorkbook xssfWorkbook = new SXSSFWorkbook(new XSSFWorkbook(file)); + SXSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0); + LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum()); + SXSSFRow row = xssfSheet.getRow(0); + LOGGER.info("第一行数据:{}", row); + xssfSheet.createRow(20); + LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum()); + } + + @Test + public void lastRowNumXSSF() throws IOException { + String file = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; + XSSFWorkbook xssfWorkbook = new XSSFWorkbook(file); + LOGGER.info("一共:{}个sheet", xssfWorkbook.getNumberOfSheets()); + XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0); + LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum()); + XSSFRow row = xssfSheet.getRow(0); + LOGGER.info("第一行数据:{}", row); + xssfSheet.createRow(20); + LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum()); + } +} diff --git a/src/test/resources/converter/converter03.xls b/src/test/resources/converter/converter03.xls index 6ae32b9a..3373d81e 100644 Binary files a/src/test/resources/converter/converter03.xls and b/src/test/resources/converter/converter03.xls differ diff --git a/src/test/resources/converter/converter07.xlsx b/src/test/resources/converter/converter07.xlsx index a210bbc2..92b93e20 100644 Binary files a/src/test/resources/converter/converter07.xlsx and b/src/test/resources/converter/converter07.xlsx differ diff --git a/src/test/resources/simple/simple07.xlsx b/src/test/resources/simple/simple07.xlsx index f01c4460..3d25fcd8 100644 Binary files a/src/test/resources/simple/simple07.xlsx and b/src/test/resources/simple/simple07.xlsx differ diff --git a/update.md b/update.md index e76566b4..12a2f85f 100644 --- a/update.md +++ b/update.md @@ -5,6 +5,7 @@ * 极大优化读大文件的内存和效率 * sheetNo 改成0开始 * 读支持指定列名 +* 升级poi 到4.0.1 # 1.2.4 修复read()方法存在的bug # 1.2.1