.rpt文件内容读取java_java读取xls/xlsx文件

适用场景:java读取excel对应xls或xlsx格式文件

1.桌面对应文件xls与xlsx

4574818d35d64ca5bba0b20db7139cd7

2.文件中的内容

xls:

ef6efdce6bcf4c57ad3fa5a3be2d38a8

xlsx:

4a92156f7f5f46138c7a1cbe1a869a88

注意点:excel格式文件列中对应是数值时需要变成文本,如下图

ee895f1e6a1441c6a4bb26a6b2a90a44

3.运行代码

be51c2aaafcc4c749b91b52d3dbda6cc

代码:


package com.test;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

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.xssf.usermodel.XSSFWorkbook;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)

@SpringBootTest

public class TestExcel {

@Test

public void testReadExel() {

System.out.println("------开始xls文件读取------");

//对应桌面地址的xls格式文件

String xlsPath="C:甥敳獲AdministratorDesktopcs.xls";

System.out.println(analyzeExcel(xlsPath));

System.out.println("------开始xlsx文件读取------");

//对应桌面地址的xlsx格式文件

String xlsxPath="C:甥敳獲AdministratorDesktopcs.xlsx";

System.out.println(analyzeExcel(xlsxPath));

}

public String analyzeExcel(String path) {

StringBuffer tempBuffer = new StringBuffer();

InputStream in = null;

// 错误信息接收器

StringBuffer errorBuffer = new StringBuffer();

try{

in = new FileInputStream(path);

//根据版本选择创建Workbook的方式

Workbook wb = null;

//根据文件名判断文件是2003版本还是2007版本

if (path.endsWith(".xlsx")) {

wb = new XSSFWorkbook(in);

} else {

wb = new HSSFWorkbook(in);

}

// 得到第一个shell

Sheet sheet = wb.getSheetAt(0);

// 得到Excel的行数

int totalRows = sheet.getPhysicalNumberOfRows();

// 总列数

int totalCells = 0;

// 得到Excel的列数(前提是有行数),从第二行算起

if (totalRows >= 2 && sheet.getRow(1) != null) {

totalCells = sheet.getRow(1).getPhysicalNumberOfCells();

}

String br = "";

// 循环Excel行数,从第二行开始

for (int r = 1; r < totalRows; r++) {

Row row = sheet.getRow(r);

if (row == null) {

errorBuffer.append(br).append("第" + (r + 1) + "行数据有问题,请仔细检查!");

continue;

}

// 循环Excel的列

for (int c = 0; c < totalCells; c++) {

Cell cell = row.getCell(c);

if(cell == null) {

errorBuffer.append(br).append("第" + (c + 1) + "列数据有问题,请仔细检查!");

continue;

}

//对应第几行第1列数据

if (c == 0) {

tempBuffer.append("第"+(r+1)+"行第1列数据="+cell.getStringCellValue());

}

//对应第几行第2列数据

else if (c == 1) {

tempBuffer.append("第"+(r+1)+"行第2列数据="+cell.getStringCellValue());

}

//对应第几行第3列数据

else if (c == 2) {

tempBuffer.append("第"+(r+1)+"行第3列数据="+cell.getStringCellValue()).append(br);

}

}

}

}catch (FileNotFoundException e) {

e.printStackTrace();

}catch (IOException e) {

e.printStackTrace();

}finally {

if(in != null) {

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

if(errorBuffer.length() >0) {

return errorBuffer.toString();

}

return tempBuffer.toString();

}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值