Java POI导入Excel及解析Excel数据

1、需要的maven依赖

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.13</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>

2、后端代码配置

    包含入参,file转Excel文件,及Excel文件内容基本解析(仅参考)

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
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.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;

@RestController
public class TestController {

    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public void importExcel(MultipartFile file) throws Exception {
		// file为上传的后台接收到的前端文件,文件名称可访问,内容为字节数据需要转成POI专属的Workbook对象进行内容数据获取
		Assert.notNull(file, "上传文件不能为空");
		
		// 将输入的file文件转成POI的Excel文件对象Workbook
		String fileName = file.getOriginalFilename(); // 获取文件名
		Workbook workbook = null;
		if (StringUtils.endsWithIgnoreCase(fileName, "XLS")) {
			try {
				workbook = new HSSFWorkbook(file.getInputStream()); // XLS类型,2003Excel版本
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else if (StringUtils.endsWithIgnoreCase(fileName, "XLSX")) {
			try {
				workbook = new XSSFWorkbook(file.getInputStream()); // XLSX类型,2007Excel版本
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			System.out.println("文件类型不符,不是Excel文件 ");
		}
		
		// 解析Workbook对象,获取上传Excel内容(实际中,上传Excel文件需要按照指定规范编写上传)
		// 得到一个Sheet工作表
		Sheet sheet = workbook.getSheetAt(0);
		System.out.println("sheet:"+JSON.toJSONString(sheet));
		// 获得Sheet工作表的数据总行数(第0行开始)
		int totalRowNum = sheet.getLastRowNum();
		System.out.println("总行数totalRowNum:"+totalRowNum);
		// 获取当前sheet的第一行
		Row row1 = sheet.getRow(0);
		System.out.println("row1:"+JSON.toJSONString(row1));
		// 获取当前sheet的第二行
		Row row2 = sheet.getRow(1);
		System.out.println("row2:"+JSON.toJSONString(row2));
		// 获取当前sheet的第二行总列数(第1列开始)
		int physicalNumberOfCells = row2.getPhysicalNumberOfCells();
		System.out.println("获得第二行的总列数physicalNumberOfCells:"+physicalNumberOfCells);
		// 获取第二行的第一个cell
		Cell cell = row2.getCell(0);
		System.out.println("第二行第一列cell:"+cell);
		// 获取cell的数据类型
		int cellType = cell.getCellType();
		System.out.println("cellType:"+JSON.toJSONString(cellType));
		// 获取cell的数据,需要根据类型选择调用以下的方法
		// cell类型为String,获取内容
		String stringCellValue = cell.getStringCellValue();
		System.out.println("stringCellValue:"+JSON.toJSONString(stringCellValue));
		// cell类型为数字,获取内容
//		double numericCellValue = cell.getNumericCellValue();
 		cell类型为布尔型,获取内容
//		boolean booleanCellValue = cell.getBooleanCellValue();
	}
}

3、接口调用及测试

导入excel文件入下

后台获取响应

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
org.apache.poi.xssf.usermodel.XSSFWorkbookApache POI库中的一个类,用于操作xlsx格式的Excel文件。它是org.apache.poi.xssf.usermodel包中的一部分。你可以使用该类来创建、读取和修改xlsx文件。引用提供了一些关于使用org.apache.poi库的示例代码,并引用了org.apache.poi.hssf.usermodel.HSSFWorkbookorg.apache.poi.xssf.usermodel.XSSFWorkbook类。引用提供了一个关于NoClassDefFoundError异常的解决方案,其中涉及到了org/apache/poi/xssf/usermodel/XSSFWorkbook类。引用给出了使用Maven构建工具添加Apache POI库的依赖项的示例代码。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [org.apache.poi JAR包 Java](https://download.csdn.net/download/u014156358/9583484)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [java.lang.NoClassDefFoundError: org/apache/poi/xssf/usermodel/XSSFWorkbook错误解决方法](https://blog.csdn.net/m0_64685289/article/details/124633631)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Java通过poi创建Excel文件并分页追加数据](https://blog.csdn.net/yuge_legend/article/details/115747048)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值