Apache POI SAX XSSFReader读取的日期格式错误
创始人
2024-09-04 16:01:20
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日期对象,并使用日期格式解析器将其格式化为指定的日期字符串。

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...