我正在使用POI 3.9从xlsx读取数据。但是现在我遇到了一个问题,POI不支持xlsb文件,我需要从程序上读取xlsb中的数据。有人知道如何从程序上读取xlsb中的数据吗?赞赏。
答案 0 :(得分:1)
在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副本。
答案 1 :(得分:0)
POI开发人员显然没有支持XLSB的计划:http://mail-archives.apache.org/mod_mbox/poi-dev/201401.mbox/%3Calpine.DEB.2.02.1401250721280.31868%40urchin.earth.li%3E
这将是相当多的工作,因为你们都需要更新记录 应对更长/不同的格式,然后重做所有marshling的东西 处理它做的非常不同的方式。到目前为止,没有人想过 为了非常微薄的利益而投入所有这些工作
似乎有一个javascript library for reading xlsb,你可以用它来将数据导出为JSON并从java中读取。
答案 2 :(得分:0)
Apache POI在3.16中增加了对XLSB流媒体读取(无写入支持)的支持。 Apache Tika 1.15现在支持从XLSB中提取。
答案 3 :(得分:0)
使用poi可以将XLSB读取到DB,Structure(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;
}
}
答案 4 :(得分:-3)
这只是一种解决方法..
您可以将xlsb文件转换为xlsx文件,并使用POI从中提取数据。
你试过吗?我知道它不是正确的答案,但希望它有所帮助。 :)