gpt4 book ai didi

java - 如何从 Excel (.xlsb) 二进制文件格式中读取数据?

转载 作者:搜寻专家 更新时间:2023-10-31 08:04:42 25 4
gpt4 key购买 nike

我正在使用 POI 3.9 从 xlsx 读取数据。但是现在我遇到了一个问题,POI 不支持 xlsb 文件,我需要通过程序从 xlsb 中读取数据。有人知道如何以编程方式从 xlsb 读取数据吗?表示赞赏。

最佳答案

使用 poi,您可以将 XLSB 读取到 DB、结构(XML,...)、目录列表等。
以下代码将 XLSB 转换为行/评论列表列表和映射以获取额外信息。
您可以根据需要自定义代码。


请从link中找到许多示例;感谢 poi 的作者。

//主类

package excel;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.binary.XSSFBSharedStringsTable;
import org.apache.poi.xssf.binary.XSSFBSheetHandler;
import org.apache.poi.xssf.binary.XSSFBStylesTable;
import org.apache.poi.xssf.eventusermodel.XSSFBReader;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class Excel {

public static void main (String [] args){

String xlsbFileName = "C:\\Users\\full path to .xlsb file";
callXLToList(xlsbFileName);
}

static void callXLToList(String xlsbFileName){
OPCPackage pkg;
try {
pkg = OPCPackage.open(xlsbFileName);
XSSFBReader r = new XSSFBReader(pkg);
XSSFBSharedStringsTable sst = new XSSFBSharedStringsTable(pkg);
XSSFBStylesTable xssfbStylesTable = r.getXSSFBStylesTable();
XSSFBReader.SheetIterator it = (XSSFBReader.SheetIterator) r.getSheetsData();

List<XLSB2Lists> workBookAsList = new ArrayList<>();
int sheetNr = 1;
while (it.hasNext()) {
InputStream is = it.next();
String name = it.getSheetName();

System.out.println("Begin parsing sheet "+sheetNr+": "+name);

XLSB2Lists testSheetHandler = new XLSB2Lists();
testSheetHandler.startSheet(name);
XSSFBSheetHandler sheetHandler = new XSSFBSheetHandler(is,
xssfbStylesTable,
it.getXSSFBSheetComments(),
sst, testSheetHandler,
new DataFormatter(),
false);
sheetHandler.parse();
testSheetHandler.endSheet();

System.out.println("End parsing sheet "+sheetNr+": "+name);
sheetNr++;

// Add parsed sheet to workbook list
workBookAsList.add(testSheetHandler);
}

// For every sheet in Workbook
System.out.println("\nShort Report:");
for(XLSB2Lists sheet:workBookAsList){
// sheet content
System.out.println("Size of content: " +sheet.getSheetContentAsList().size());
// sheet comment
System.out.println("Size fo comment: "+sheet.getSheetCommentAsList().size());
// sheet extra info
System.out.println("Extra info.: "+sheet.getMapOfInfo().toString());
}

} catch (InvalidFormatException e) {
// TODO Please do your catch hier
e.printStackTrace();
} catch (IOException e) {
// TODO Please do your catch hier
e.printStackTrace();
} catch (OpenXML4JException e) {
// TODO Please do your catch hier
e.printStackTrace();
} catch (SAXException e) {
// TODO Please do your catch hier
e.printStackTrace();
}
}
}

//解析类

package excel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
import org.apache.poi.xssf.usermodel.XSSFComment;

/**
*
* @author Dominique
*/
public class XLSB2Lists implements XSSFSheetXMLHandler.SheetContentsHandler {

private final List sheetAsList = new ArrayList<>();
private List rowAsList;
private final List sheetCommentAsList = new ArrayList<>();
private List rowCommentAsList;
private final Map propertyMap = new HashMap<>();

public void startSheet(String sheetName) {
propertyMap.put("sheetName", sheetName);
}

@Override
public void startRow(int rowNum) {
rowAsList = new ArrayList<>();
rowCommentAsList = new ArrayList<>();
}

@Override
public void endRow(int rowNum) {
sheetAsList.add(rowNum, rowAsList);
sheetCommentAsList.add(rowNum, rowCommentAsList);
}

@Override
public void cell(String cellReference, String formattedValue, XSSFComment comment) {
formattedValue = (formattedValue == null) ? "" : formattedValue;
rowAsList.add(formattedValue);
if (comment == null) {
rowCommentAsList.add("");
} else {
propertyMap.put("comment author at "+comment.getRow()+":"+cellReference, comment.getAuthor());
rowCommentAsList.add(comment.getString().toString().trim());
}
}

@Override
public void headerFooter(String text, boolean isHeader, String tagName) {
if (isHeader) {
propertyMap.put("header tag", tagName);
propertyMap.put("header text", text);
} else { // footer
propertyMap.put("header tag", tagName);
propertyMap.put("header text", text);
}
}

public List getSheetContentAsList(){
return sheetAsList;
}

public List getSheetCommentAsList(){
return sheetCommentAsList;
}

public Map getMapOfInfo(){
return propertyMap;
}
}

关于java - 如何从 Excel (.xlsb) 二进制文件格式中读取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17183928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com