我正在使用 POI 3.9 从 xlsx 读取数据。但现在我面临一个问题,POI 不支持 xlsb 文件,我需要以程序方式从 xlsb 读取数据。有人知道如何以编程方式从 xlsb 读取数据吗?赞赏。
5 回答
Apache POI 在 3.16 中增加了对 XLSB 流式读取的支持(无写入支持)。Apache Tika 1.15 现在支持从 XLSB 提取。
在 Perl 中,Win32::OLE模块可以将 XLSB 转换为 XLSX。缺点:您必须安装 MS Excel。这是一些基于我使用的示例代码...
use File::Spec::Functions qw/rel2abs/;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Excel';
use Win32::OLE::Variant;
Win32::OLE->Option( Warn => 3 );
my $xlsb = 'C:\Users\wohlfarj\Documents\File.xlsb';
# This block uses an already open instance of Excel, or starts a new one if it isn't already open.
my $excel;
eval { $excel = Win32::OLE->GetActiveObject('Excel.Application') };
die 'MS Excel not installed' if $@;
unless (defined $excel) {
$excel = Win32::OLE->new( 'Excel.Application', 'Quit' )
or die 'Cannot start MS Excel';
}
# After all of the setup, converting the file is painless.
my $xlsx = rel2abs( $xlsb );
$xlsx =~ s/\.xlsb$/\.xlsx/i;
my $workbook = $excel->Workbooks->Open( {FileName => rel2abs( $xlsb )} );
$workbook->SaveAs( {FileFormat => xlOpenXMLWorkbook, Filename => $xlsx} );
$workbook->Close( {SaveChanges => xlDoNotSaveChanges} );
从这里,Spreadsheet::XLSX模块可以很好地读取 XLSX 副本。
使用 poi,您可以将 XLSB 读取到 DB、结构(XML、...)、内容列表等
。以下代码将 XLSB 转换为行/注释列表列表并映射以获取额外信息。
只是您可以根据需要自定义代码。
请从链接中找到许多示例;感谢 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;
}
}
POI 开发人员显然没有计划支持 XLSB:http ://mail-archives.apache.org/mod_mbox/poi-dev/201401.mbox/%3Calpine.DEB.2.02.1401250721280.31868%40urchin.earth.li%3E
这将是相当多的工作,因为你们都需要更新记录以应对更长/不同的格式,然后重做所有编组的东西以处理非常不同的方式。到目前为止,没有人愿意为了微不足道的利益投入所有工作
似乎有一个用于读取 xlsb 的 javascript 库,您可以使用它来将数据导出为 JSON 并从 java 中读取。
这只是一种解决方法..
您可以将 xlsb 文件转换为 xlsx 文件并使用 POI 从中提取数据。
你试过了吗?我知道它不是正确的答案,但希望它有所帮助。:)