Apache POI SAX XSSFReader读取的日期格式错误
创始人
2024-09-04 16:00:50
0

问题描述: 当使用Apache POI的SAX方式读取Excel文件时,使用XSSFReader读取日期单元格时,日期格式会出现错误。

解决方法: 通过设置日期格式解析器来解决日期格式错误的问题。下面是一个示例代码:

import org.apache.poi.POIXMLProperties;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFDateUtil;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class XSSFReaderExample {
    public static void main(String[] args) throws Exception {
        String filePath = "path_to_your_excel_file.xlsx";

        OPCPackage opcPackage = OPCPackage.open(filePath, PackageAccess.READ);
        XSSFReader xssfReader = new XSSFReader(opcPackage);

        StylesTable stylesTable = xssfReader.getStylesTable();
        SharedStringsTable stringsTable = xssfReader.getSharedStringsTable();

        XMLReader xmlReader = XMLReaderFactory.createXMLReader();
        SheetHandler sheetHandler = new SheetHandler(stylesTable, stringsTable);

        xmlReader.setContentHandler(sheetHandler);

        XSSFReader.SheetIterator sheetIterator = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
        while (sheetIterator.hasNext()) {
            InputStream sheetInputStream = sheetIterator.next();
            InputSource sheetSource = new InputSource(sheetInputStream);
            xmlReader.parse(sheetSource);
            sheetInputStream.close();
        }

        opcPackage.close();
    }

    private static class SheetHandler extends DefaultHandler {
        private StylesTable stylesTable;
        private SharedStringsTable stringsTable;

        private boolean isCell;
        private boolean isDateCell;
        private String cellValue;
        private short formatIndex;
        private String formatString;

        private SimpleDateFormat dateFormat;

        public SheetHandler(StylesTable stylesTable, SharedStringsTable stringsTable) {
            this.stylesTable = stylesTable;
            this.stringsTable = stringsTable;

            this.isCell = false;
            this.isDateCell = false;
            this.cellValue = "";
            this.formatIndex = -1;
            this.formatString = "";

            this.dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        }

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            if ("c".equals(qName)) {
                String cellType = attributes.getValue("t");
                if ("s".equals(cellType)) {
                    isCell = true;
                } else if ("d".equals(cellType)) {
                    isCell = true;
                    isDateCell = true;
                } else {
                    isCell = false;
                }

                formatIndex = -1;
                formatString = "";
            }
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            if (isCell && isDateCell && "v".equals(qName)) {
                Date dateValue = XSSFDateUtil.getJavaDate(Double.parseDouble(cellValue));
                String formattedValue = dateFormat.format(dateValue);
                System.out.println("Formatted Date Value: " + formattedValue);

                isCell = false;
                isDateCell = false;
                cellValue = "";
                formatIndex = -1;
                formatString = "";
            }
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            if (isCell && isDateCell) {
                cellValue += new String(ch, start, length);
            }
        }
    }
}

在上面的示例代码中,我们创建了一个SheetHandler类来处理XML解析事件,并设置了日期格式解析器dateFormatyyyy-MM-dd。在endElement方法中,我们使用XSSFDateUtil.getJavaDate方法将Excel日期值转换为Java日期对象,并使用日期格式解析器将其格式化为指定的日期字符串。

相关内容

热门资讯

重大通报!吉安中至小程序微信辅... 重大通报!吉安中至小程序微信辅助,捉住捣蛋鸡辅助(讲解有挂辅助脚本);无需打开直接搜索微信(1367...
透视透明挂!中至九江黑科技ap... 透视透明挂!中至九江黑科技app怎么下载,wepoker辅助软件视频(辅助挂)详细开挂辅助下载;亲,...
专业讨论!随意玩有辅助吗,赣牌... 专业讨论!随意玩有辅助吗,赣牌圈小程序破解版(有挂解密开挂辅助器);无需打开直接搜索薇:136704...
透视美元局!边锋小程序跑的快辅... 边锋小程序跑的快辅助是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户可以加...
实测教程!皮皮衡阳字牌科技,八... 实测教程!皮皮衡阳字牌科技,八仙竞技馆辅助(真的有挂辅助插件);无需打开直接搜索打开薇:136704...
透视辅助!白金岛放炮罚开挂,p... 透视辅助!白金岛放炮罚开挂,pokemmo辅助脚本(辅助挂)详细开挂辅助平台;无需打开直接搜索薇:1...
盘点一款!哈局八张模拟器,蜜瓜... >>您好:蜜瓜大厅辅助免费下载确实是有挂的,很多玩家在这款蜜瓜大厅辅助免费下载游戏中打牌都会发现很多...
透视最新!微信小程序开心十三张... 透视最新!微信小程序开心十三张攻略,wpk官网下载链接(辅助挂)详细开挂辅助神器;无需打开直接搜索薇...
详细说明!闲逸透视app免费版... 闲逸透视app免费版在哪里开挂教程视频分享装挂详细步骤在当今的网络游戏中,闲逸透视app免费版在哪里...
透视玄学!边锋干橙眼辅助免费,... 边锋干橙眼辅助免费是一款专注玩家量身打造的游戏记牌类型软件,在边锋干橙眼辅助免费这款游戏中我们可以记...