[英]Reading data from Excel sheet 2007 in JAVA
I am having a problem when trying to read an Excel 2007 spreadsheet (.xlsx). 尝试读取Excel 2007电子表格(.xlsx)时遇到问题。 I am trying to implement the method in JAVA by using the POI library , but I get this error:
我正在尝试通过使用POI库在JAVA中实现该方法,但出现此错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException
线程“主”中的异常java.lang.NoClassDefFoundError:org / apache / xmlbeans / XmlException
and this is my method: 这是我的方法:
public void No_rows() throws IOException {
File inputWorkbook = new File(inputFile);
FileInputStream w = new FileInputStream(inputWorkbook);
XSSFWorkbook workbook = new XSSFWorkbook(w);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator rows = sheet.rowIterator();
int number=sheet.getLastRowNum();
this.num_rows = number;
w.close();
}
As mentioned by @Michael-O in the comments, XML Beans are missing from the classpath. 正如@ Michael-O在评论中提到的那样,类路径中缺少XML Bean。 The XMLBeans library can be found at http://xmlbeans.apache.org/ .
XMLBeans库可以在http://xmlbeans.apache.org/找到。
You have to include xmlbeans library in your build path. 您必须在构建路径中包括xmlbeans库。 It is usually in your ooxml-lib in your poi-apache library.
它通常在poi-apache库的ooxml-lib中。
This is to create and read an *.xlsx file. 这是为了创建和读取* .xlsx文件。 If you get that error, include the
jaxp--api-1.4.jar
. 如果出现该错误,请包括
jaxp--api-1.4.jar
。
public class Readxlsx {
public static void main(String[] args) {
FileOutputStream fos = null;
FileInputStream fis = null;
try
{
fos = new FileOutputStream(new File("D:\\prac\\sample1.xlsx"));
XSSFWorkbook wb = new XSSFWorkbook();
for(int m=0;m<3;m++){
if(m==0)
{
XSSFSheet sh = wb.createSheet("Sachin");
System.out.println(" Sheet NO:"+m);
for (int k = 0; k < 30; k++) {
XSSFRow row = sh.createRow((short)k);
for (int i = 0; i < 30; i++) {
XSSFCell cell = row.createCell((short)i);
cell.setCellValue(wb.getSheetName(m)+i);
}
}
}
else if(m==1){
XSSFSheet sh1 = wb.createSheet("Dravid");
System.out.println(" Sheet NO:"+m);
for (int k = 0; k < 30; k++) {
XSSFRow row = sh1.createRow((short)k);
for (int i = 0; i < 30; i++) {
XSSFCell cell = row.createCell((short)i);
cell.setCellValue(wb.getSheetName(m)+i);
}
}
}
else
{
XSSFSheet sh2 = wb.createSheet("Dhoni");
System.out.println(" Sheet NO:"+m);
for (int k = 0; k < 30; k++) {
XSSFRow row = sh2.createRow((short)k);
for (int i = 0; i < 30; i++) {
XSSFCell cell = row.createCell((short)i);
cell.setCellValue(wb.getSheetName(m)+i);
}
}
}
}
wb.write(fos);
fos.close();
fis= new FileInputStream(new File("D:\\prac\\sample1.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);
java.util.Iterator<org.apache.poi.ss.usermodel.Row> rows = sheet.rowIterator();
int number=sheet.getLastRowNum();
System.out.println(" number of rows"+ number);
while (rows.hasNext())
{
XSSFRow row = ((XSSFRow) rows.next());
int r=row.getRowNum();
System.out.println(" Row NO:"+r);
java.util.Iterator<org.apache.poi.ss.usermodel.Cell> cells = row.cellIterator();
while(cells.hasNext()) {
XSSFCell cell = (XSSFCell) cells.next();
String Value=cell.getStringCellValue();
System.out.println(Value);
}
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.