java xlsx读写_java读取xls/xlsx文件-java读写文件

本文介绍了如何使用Java读取和写入Excel文件,包括xls和xlsx格式。通过Apache POI库,演示了读取指定路径下xls和xlsx文件的特定列数据,并处理可能的错误情况。
摘要由CSDN通过智能技术生成

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

1.桌面对应文件xls与xlsx

f9e06f0a13d7588083796a53f3914f3f.png

2.文件中的内容

xls:

8bbd8bd7f3588446d58788ac2b88b23e.png

xlsx:

a295dc131d73b253b6a74630597ef3b9.png

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

b9e31eaf02279c87883371a70600dd47.png

3.运行代码

bfa75a66d5b046867a4021d02a8641d3.png

代码:

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:\\Users\\Administrator\\Desktop\\cs.xls";

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

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

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

String xlsxPath="C:\\Users\\Administrator\\Desktop\\cs.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 = "\n";

// 循环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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值