永发信息网

java 怎么读xlsx文件

答案:7  悬赏:80  手机版
解决时间 2021-12-01 01:44
  • 提问者网友:不爱我么
  • 2021-11-30 18:25
java 怎么读xlsx文件
最佳答案
  • 五星知识达人网友:毛毛
  • 2021-11-30 19:55
package rw_excel;

import static org.junit.Assert.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class test_poi {

@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
}

@Test
public void test() throws IOException {
// fail("Not yet implemented");
String file_dir = "test.xlsx";
Workbook book = null;
book = getExcelWorkbook(file_dir);
Sheet sheet = getSheetByNum(book,0);

int lastRowNum = sheet.getLastRowNum();

System.out.println("last number is "+ lastRowNum);

for(int i = 0 ; i <= lastRowNum ; i++){
Row row = null;
row = sheet.getRow(i);
if( row != null ){
System.out.println("reading line is " + i);
int lastCellNum = row.getLastCellNum();
System.out.println("lastCellNum is " + lastCellNum );
Cell cell = null;

for( int j = 0 ; j <= lastCellNum ; j++ ){
cell = row.getCell(j);
if( cell != null ){
String cellValue = cell.getStringCellValue();
System.out.println("cell value is \n" + cellValue);
}
}
}
}

}

public static Sheet getSheetByNum(Workbook book,int number){
Sheet sheet = null;
try {
sheet = book.getSheetAt(number);
// if(sheet == null){
// sheet = book.createSheet("Sheet"+number);
// }
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
return sheet;
}
public static Workbook getExcelWorkbook(String filePath) throws IOException{
Workbook book = null;
File file = null;
FileInputStream fis = null;

try {
file = new File(filePath);
if(!file.exists()){
throw new RuntimeException("文件不存在");
}else{
fis = new FileInputStream(file);
book = WorkbookFactory.create(fis);
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
} finally {
if(fis != null){
fis.close();
}
}
return book;
}
//
}
全部回答
  • 1楼网友:一袍清酒付
  • 2021-12-01 01:00
其实这个很简单的啊,jxl同样可以读取2007的,
  • 2楼网友:山君与见山
  • 2021-12-01 00:15
可以apache 的poi框架,他的官网上有api,操作还挺简单的
  • 3楼网友:琴狂剑也妄
  • 2021-11-30 23:03
你可以用POI来解析,这个应该没有xls版本的限制。需要案例,请联系追问有什么案例呢
  • 4楼网友:荒野風
  • 2021-11-30 22:37
需要jxl的包

//打开文件
Workbook book = Workbook.getWorkbook(new File(path)) ;
//取得第一个sheet
Sheet sheet = book.getSheet(0);
int rows = sheet.getRows();
//i和j是你要的行和列

Cell [] cell = sheet.getRow(i);
Cell cloumn_cell = sheet.getCell(j, i);
//str是你的内容

String str = cloumn_cell.getContents();

方法有点老了 不知道能不能帮到你
  • 5楼网友:山有枢
  • 2021-11-30 21:39
高版本的jxl好像是可以的  老版本的jar包只支持2003
解析Excel还可以用Apache POI  ,这个支持新老版本的Excel,非常好用。
  • 6楼网友:动情书生
  • 2021-11-30 21:19
apache的poi可以,http://poi.apache.org/spreadsheet/index.html这是文档
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯