第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

POI API 的事件模型在 3.15 版本中是否不適用(出現(xiàn)編譯錯誤)?

POI API 的事件模型在 3.15 版本中是否不適用(出現(xiàn)編譯錯誤)?

幕布斯7119047 2023-06-08 19:11:51
我已經(jīng)實現(xiàn)了一個程序,我試圖使用 SAX 和 XSSF(POI API) 以及 jar 版本 4.1 讀取 excel 文件 (.xlsx),它工作正常。但它在 POI 版本 3.15 中給出了編譯錯誤。代碼看起來像這樣(在 4.10 中運行良好):默認處理程序?qū)崿F(xiàn)(SAXExcelSheetHandler.java):import org.apache.poi.xssf.model.SharedStringsTable;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler;public class SAXExcelSheetHandler extends DefaultHandler {    private SharedStringsTable sst;    private String lastContents;    private boolean nextIsString;    public SAXExcelSheetHandler(SharedStringsTable sst) {        this.sst = sst;    }    public void startElement(String uri, String localName, String name,                             Attributes attributes) throws SAXException {        // c => cell        if(name.equals("c")) {            // Print the cell reference            System.out.print(attributes.getValue("r") + " - ");            // Figure out if the value is an index in the SST            String cellType = attributes.getValue("t");            if(cellType != null && cellType.equals("s")) {                nextIsString = true;            } else {                nextIsString = false;            }        }        // Clear contents cache        lastContents = "";    }    public void endElement(String uri, String localName, String name)            throws SAXException {        // Process the last contents as required.        // Do now, as characters() may be called more than once        if(nextIsString) {            int idx = Integer.parseInt(lastContents);            lastContents = sst.getItemAt(idx).getString();            nextIsString = false;        }        // v => contents of a cell        // Output after we've seen the string contents        if(name.equals("v")) {            System.out.println(lastContents);        }    }    public void characters(char[] ch, int start, int length) {        lastContents += new String(ch, start, length);    }}
查看完整描述

1 回答

?
慕娘9325324

TA貢獻1783條經(jīng)驗 獲得超4個贊

請不要堅持使用過時的軟件版本。這樣做不是我們在軟件開發(fā)方面取得進步的方式。


但當(dāng)然,XSSF and SAX (Event API)版本也存在3.15。


但在這個舊版本中SharedStringsTable已經(jīng)沒有辦法了getItemAt。它只有g(shù)etEntryAtwhich 返回 a org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst。所以類中的代碼SAXExcelSheetHandler必須更改為:


...

import org.apache.poi.xssf.usermodel.XSSFRichTextString;

...


...

? ? public void endElement(String uri, String localName, String name)

? ? ? ? ? ? throws SAXException {

? ? ? ? // Process the last contents as required.

? ? ? ? // Do now, as characters() may be called more than once

? ? ? ? if(nextIsString) {

? ? ? ? ? ? int idx = Integer.parseInt(lastContents);

? ? ? ? ? ? //lastContents = sst.getItemAt(idx).getString();

? ? ? ? ? ? lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString();

? ? ? ? ? ? nextIsString = false;

? ? ? ? }


? ? ? ? // v => contents of a cell

? ? ? ? // Output after we've seen the string contents

? ? ? ? if(name.equals("v")) {

? ? ? ? ? ? System.out.println(lastContents);

? ? ? ? }

? ? }


...

也還沒有一個SAXHelper. 所以類的代碼POISaxXaafV2必須像這樣改變:


...

import java.io.File;

import java.io.InputStream;

import java.util.Iterator;

//import org.apache.poi.ooxml.util.SAXHelper;

import org.apache.poi.openxml4j.opc.OPCPackage;

import org.apache.poi.xssf.eventusermodel.XSSFReader;

import org.apache.poi.xssf.model.SharedStringsTable;

import org.xml.sax.ContentHandler;

import org.xml.sax.InputSource;

import org.xml.sax.XMLReader;


import javax.xml.parsers.SAXParserFactory;

import javax.xml.parsers.SAXParser;

...


...

? ? ? ? OPCPackage opcPackage = OPCPackage.open(file);

? ? ? ? XSSFReader xssfReader = new XSSFReader(opcPackage);

? ? ? ? SharedStringsTable sharedStringsTable = xssfReader.getSharedStringsTable();


? ? ? ? //XMLReader xmlParser = SAXHelper.newXMLReader();

? ? ? ? SAXParserFactory parserFactory = SAXParserFactory.newInstance();

? ? ? ? SAXParser parser = parserFactory.newSAXParser();

? ? ? ? XMLReader xmlParser = parser.getXMLReader();

? ? ? ? ContentHandler contentHandler = new SAXExcelSheetHandler(sharedStringsTable);

? ? ? ? xmlParser.setContentHandler(contentHandler);

...


查看完整回答
反對 回復(fù) 2023-06-08
  • 1 回答
  • 0 關(guān)注
  • 230 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號