我刚才有一个关于如何使用Apache的XSSF格式读取xlsx文件的快速问题。
现在我的代码看起来像这样:
InputStream fs = new FileInputStream(filename); // (1)
XSSFWorkbook wb = new XSSFWorkbook(fs); // (2)
XSSFSheet sheet = wb.getSheetAt(0); // (3)
...导入所有相关内容。我的问题是,当我点击运行时,它会陷入第(2)行,几乎是无限循环。 filename
只是一个字符串。
如果有人能给我一些关于如何解决这个问题的示例代码,我会非常感激。我现在想要的只是从xlsx文件中读取单个单元格;我使用HSSF用于xls文件并且没有问题。
感谢您的帮助, 安德鲁
答案 0 :(得分:9)
InputStream inp = null;
try {
inp = new FileInputStream("E:/sample_poi.xls");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Header header = sheet.getHeader();
int rowsCount = sheet.getLastRowNum();
System.out.println("Total Number of Rows: " + (rowsCount + 1));
for (int i = 0; i <= rowsCount; i++) {
Row row = sheet.getRow(i);
int colCounts = row.getLastCellNum();
System.out.println("Total Number of Cols: " + colCounts);
for (int j = 0; j < colCounts; j++) {
Cell cell = row.getCell(j);
System.out.println("[" + i + "," + j + "]=" + cell.getStringCellValue());
}
}
} catch (Exception ex) {
java.util.logging.Logger.getLogger(FieldController.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
inp.close();
} catch (IOException ex) {
java.util.logging.Logger.getLogger(FieldController.class.getName()).log(Level.SEVERE, null, ex);
}
}
答案 1 :(得分:8)
我相信这会回答你的问题:http://poi.apache.org/spreadsheet/quick-guide.html#ReadWriteWorkbook
简而言之,您的代码应如下所示:
InputStream inp = new FileInputStream("workbook.xlsx");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
Cell cell = row.getCell(3);
答案 2 :(得分:3)
为什么要将文件分成InputStream? XSSFWorkbook有一个构造函数,它只是将路径作为String。只需对字符串的路径进行硬编码。一旦创建了工作簿,就可以从中创建XSSFS表。然后XSSFCells,它最终将允许您读取单个单元格的内容(单元格基于x,y位置,基本上)
答案 3 :(得分:1)
您可以尝试以下操作。
private static void readXLSX(String path) throws IOException {
File myFile = new File(path);
FileInputStream fis = new FileInputStream(myFile);
// Finds the workbook instance for XLSX file
XSSFWorkbook myWorkBook = new XSSFWorkbook (fis);
// Return first sheet from the XLSX workbook
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
// Get iterator to all the rows in current sheet
Iterator<Row> rowIterator = mySheet.iterator();
// Traversing over each row of XLSX file
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t");
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t");
break;
default :
}
}
System.out.println("");
}
}
答案 4 :(得分:0)
这很好用:试试吧
!ENTRY org.eclipse.core.jobs 4 2 2016-05-27 15:42:26.837
!MESSAGE An internal error occurred during: "Compute launch button tooltip".
!STACK 0
java.lang.NoSuchMethodError: org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer.<init>([Lorg/eclipse/jdt/internal/compiler/env/ISourceType;Lorg/eclipse/jdt/internal/compiler/env/AccessRestriction;)V
at org.eclipse.ajdt.core.codeconversion.ITDAwareNameEnvironment.find(ITDAwareNameEnvironment.java:115)
at org.eclipse.jdt.internal.core.SearchableEnvironment.findType(SearchableEnvironment.java:305)
at org.eclipse.jdt.internal.core.CancelableNameEnvironment.findType(CancelableNameEnvironment.java:45)
at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.askForType(LookupEnvironment.java:174)
at org.eclipse.jdt.internal.compiler.lookup.PackageBinding.getTypeOrPackage(PackageBinding.java:214)
at org.eclipse.jdt.internal.compiler.lookup.Scope.getTypeOrPackage(Scope.java:3228)
at org.eclipse.jdt.internal.compiler.lookup.Scope.getType(Scope.java:2940)
at org.eclipse.jdt.internal.compiler.ast.SingleTypeReference.getTypeBinding(SingleTypeReference.java:51)
at org.eclipse.jdt.internal.compiler.ast.TypeReference.internalResolveType(TypeReference.java:495)
at org.eclipse.jdt.internal.compiler.ast.TypeReference.resolveType(TypeReference.java:586)
at org.eclipse.jdt.internal.compiler.ast.TypeReference.resolveTypeArgument(TypeReference.java:598)
at org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference.internalResolveLeafType(ParameterizedSingleTypeReference.java:254)
at org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference.internalResolveType(ParameterizedSingleTypeReference.java:165)
at org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference.resolveType(ParameterizedSingleTypeReference.java:378)
at org.eclipse.jdt.internal.compiler.ast.TypeReference.resolveType(TypeReference.java:582)
at org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.resolveTypeFor(SourceTypeBinding.java:1759)
at org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.fields(SourceTypeBinding.java:934)
at org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.internalFaultInTypeForFieldsAndMethods(SourceTypeBinding.java:903)
at org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.faultInTypesForFieldsAndMethods(SourceTypeBinding.java:899)
at org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope.faultInTypes(CompilationUnitScope.java:448)
at org.eclipse.jdt.internal.core.hierarchy.HierarchyResolver.resolve(HierarchyResolver.java:827)
at org.eclipse.jdt.internal.core.hierarchy.HierarchyResolver.resolve(HierarchyResolver.java:628)
at org.eclipse.jdt.internal.core.hierarchy.HierarchyBuilder.buildSupertypes(HierarchyBuilder.java:116)
at org.eclipse.jdt.internal.core.hierarchy.IndexBasedHierarchyBuilder.build(IndexBasedHierarchyBuilder.java:151)
at org.eclipse.jdt.internal.core.hierarchy.TypeHierarchy.compute(TypeHierarchy.java:315)
at org.eclipse.jdt.internal.core.hierarchy.TypeHierarchy.refresh(TypeHierarchy.java:1286)
at org.eclipse.jdt.internal.core.CreateTypeHierarchyOperation.executeOperation(CreateTypeHierarchyOperation.java:90)
at org.eclipse.jdt.internal.core.JavaModelOperation.run(JavaModelOperation.java:724)
at org.eclipse.jdt.internal.core.JavaModelOperation.runOperation(JavaModelOperation.java:790)
at org.eclipse.jdt.internal.core.SourceType.newSupertypeHierarchy(SourceType.java:743)
at org.eclipse.jdt.internal.core.SourceType.newSupertypeHierarchy(SourceType.java:695)
at org.eclipse.jdt.internal.launching.JavaLaunchableTester.hasSuperclass(JavaLaunchableTester.java:467)
at org.eclipse.jdt.internal.launching.JavaLaunchableTester.test(JavaLaunchableTester.java:611)
at org.eclipse.core.internal.expressions.Property.test(Property.java:58)
at org.eclipse.core.internal.expressions.TestExpression.evaluate(TestExpression.java:100)
at org.eclipse.core.internal.expressions.CompositeExpression.evaluateOr(CompositeExpression.java:68)
at org.eclipse.core.internal.expressions.OrExpression.evaluate(OrExpression.java:22)
at org.eclipse.core.internal.expressions.CompositeExpression.evaluateAnd(CompositeExpression.java:53)
at org.eclipse.core.internal.expressions.AndExpression.evaluate(AndExpression.java:31)
at org.eclipse.core.internal.expressions.CompositeExpression.evaluateAnd(CompositeExpression.java:53)
at org.eclipse.core.internal.expressions.IterateExpression.evaluate(IterateExpression.java:160)
at org.eclipse.core.internal.expressions.CompositeExpression.evaluateAnd(CompositeExpression.java:53)
at org.eclipse.core.internal.expressions.WithExpression.evaluate(WithExpression.java:75)
at org.eclipse.core.internal.expressions.CompositeExpression.evaluateAnd(CompositeExpression.java:53)
at org.eclipse.core.internal.expressions.EnablementExpression.evaluate(EnablementExpression.java:55)
at org.eclipse.debug.internal.ui.launchConfigurations.LaunchShortcutExtension.evalEnablementExpression(LaunchShortcutExtension.java:281)
at org.eclipse.debug.internal.ui.contextlaunching.LaunchingResourceManager.getShortcutsForSelection(LaunchingResourceManager.java:454)
at org.eclipse.debug.internal.ui.contextlaunching.LaunchingResourceManager.computeLabels(LaunchingResourceManager.java:240)
at org.eclipse.debug.internal.ui.contextlaunching.LaunchingResourceManager$1$1.run(LaunchingResourceManager.java:142)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
答案 5 :(得分:0)
public class ExcelReader{
public String path;
public static FileInputStream fis;
public ExcelReader(){
System.out.println("hello");
}
public ExcelReader(String path){
this.path=path;
fis=new FileInputStream(path);
XSSFWorkbook workbook=new XSSFWorkbook(fis);
XSSFSheet sheet=workbook.getSheet("Sheet1");//name of the sheet
System.out.println(sheet.getSheetName());
System.out.println(sheet.getLastRowNum());
System.out.println(sheet.getRow(2).getCell(3));
}
public static void main(String[] args) throws IOException {
ExcelReader excel=new ExcelReader("path of xlsx");
}
}
答案 6 :(得分:0)
我有同样的错误,我刚刚用相同的版本更新了pom依赖项。奏效了。
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
答案 7 :(得分:0)
// Load sheet- Here we are loading first sheet only
XSSFSheet xlSheet = wb.getSheetAt(0);
这里的代码只能识别第一张纸-在我的Excel中有多张纸,因此面临着将sheet2
更改为sheet1(getSheetAt(0))
...的问题...