当前位置: 首页 > news >正文

网站cms管理后台电话号码郴州seo

网站cms管理后台电话号码,郴州seo,网页编辑快捷键,门户网站的建设思路关注WX:CodingTechWork 介绍 在日常开发中,我们经常需要处理Excel文件中的数据。无论是从数据库导入数据、处理数据报表,还是批量生成数据,都可能会遇到需要读取和操作Excel文件的场景。本文将详细介绍如何使用Java中的Apache PO…

关注WX:CodingTechWork

介绍

  在日常开发中,我们经常需要处理Excel文件中的数据。无论是从数据库导入数据、处理数据报表,还是批量生成数据,都可能会遇到需要读取和操作Excel文件的场景。本文将详细介绍如何使用Java中的Apache POI库来读取Excel文件,解析其中的数据,并进行后续的处理。

项目背景

假设我们需要开发一个功能,读取一个Excel文件中的数据并进行处理。通常,这样的需求会出现在以下场景中:

  1. 数据迁移:将Excel表格的数据导入数据库。
  2. 数据分析:对Excel中的数据进行汇总、统计分析。
  3. 批量处理:从Excel文件中读取配置信息或参数进行批量处理。
    在本篇文章中,我们将展示如何使用Java读取Excel文件,获取其中的数据,并展示如何将这些数据转化为业务对象以便后续处理。

依赖导入

首先,你需要在项目中添加Apache POI的依赖。这里使用的是Apache POI 3.x版本,你可以在pom.xml中加入如下依赖:

<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.3</version>
</dependency>

这个依赖包包括了读取xlsx格式的支持,如果需要支持更老的xls格式,可以再加上poi模块。

读取Excel模板的实现

接下来,我们来看一个简单的示例代码,展示如何读取Excel文件的内容,并对数据进行处理。我们将以一个示例Excel表格为例,假设表格的内容如下:

姓名年龄性别
张三25
李四30
王五28

代码实现

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;/*** 读取Excel文件并进行数据处理的服务类*/
@Service
@Slf4j
public class ReadExcelServiceImpl {public Boolean readExcel() {try {String pathStr = "/path/to/your/excel/file.xlsx";// excel文件路径FileInputStream fis = new FileInputStream(pathStr);// 创建一个工作簿对象Workbook workbook = new XSSFWorkbook(fis);// 获取第一个工作表Sheet sheet = workbook.getSheetAt(0);// 获取总行数int lastRowNum = sheet.getLastRowNum();// 存放Excel读取的数据列表List<ExcelDemoInfoDTO> demoInfoList = new ArrayList<>();// 读取数据。循环遍历行,从第二行开始,假设第一行是标题行for (int i = 1; i <= lastRowNum; i++) {log.info("Reading row {}", i);Row row = sheet.getRow(i);if (row != null) {try {// 获取单元格的值String cell0 = getCellValue(row.getCell(0)); // 姓名String cell1 = getCellValue(row.getCell(1)); // 年龄String cell2 = getCellValue(row.getCell(2)); // 性别// 创建数据对象并设置字段ExcelDemoInfoDTO demoInfoDTO = new ExcelDemoInfoDTO();demoInfoDTO.setName(cell0);demoInfoDTO.setAge(Integer.parseInt(cell1));demoInfoDTO.setGender(cell2);// 将数据对象加入到列表demoInfoList.add(demoInfoDTO);} catch (Exception e) {log.error("Error reading row {}", i, e);}}}// 使用Jackson将读取的数据转换为JSON字符串ObjectMapper mapper = new ObjectMapper();String json = mapper.writeValueAsString(demoInfoList);System.out.println(json);// 关闭资源workbook.close();fis.close();} catch (IOException e) {e.printStackTrace();return false;}return true;}/*** 获取单元格的值,处理不同类型的单元格** @param cell 单元格对象* @return 单元格的字符串值*/private static String getCellValue(Cell cell) {if (cell == null) {return "";}switch (cell.getCellType()) {case STRING:return cell.getStringCellValue();case NUMERIC:return String.valueOf((int) cell.getNumericCellValue());default:return "";}}
}

代码解析

  1. 打开Excel文件
    使用FileInputStream打开指定路径的Excel文件,然后通过XSSFWorkbook将其加载为工作簿(Workbook)。
  2. 读取Excel工作表
    通过workbook.getSheetAt(0)获取第一个工作表(Sheet)。你可以根据需要更改getSheetAt中的索引值来获取其他工作表。
  3. 遍历行和列
    使用sheet.getRow(i)获取每一行的数据。我们从第二行开始读取(i=1),因为第一行通常是标题行。
  4. 获取单元格内容
    通过row.getCell(i)获取每一列的内容,并使用getCellValue方法根据单元格的类型(字符串、数字等)获取对应的值。
  5. 封装数据
    将每行的数据封装为一个业务对象(ExcelDemoInfoDTO),并将其加入到一个列表中。
  6. 转换为JSON格式
    使用Jackson库将读取的数据转化为JSON格式,以便后续的处理或传输。
  7. 资源关闭
    使用完毕后,关闭workbook和FileInputStream以释放资源。

ExcelDemoInfoDTO 数据传输对象

为了更好地封装数据,我们创建一个简单的DTO(数据传输对象)类ExcelDemoInfoDTO:

public class ExcelDemoInfoDTO {private String name;private int age;private String gender;// Getters and Setterspublic String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}
}

总结

  在本次实践中,我们介绍了如何使用Apache POI库在Java中读取Excel文件,获取其中的数据,并将这些数据封装为业务对象。通过这种方式,我们可以灵活地读取各种格式的Excel数据,并进行后续的业务处理。对于更复杂的Excel文件,我们还可以进一步扩展代码来处理更多类型的单元格、跨工作表读取等情况。


文章转载自:
http://cansure.wwxg.cn
http://pancreatitis.wwxg.cn
http://wordsworthian.wwxg.cn
http://fisherfolk.wwxg.cn
http://blackboard.wwxg.cn
http://scutage.wwxg.cn
http://compatriot.wwxg.cn
http://rickettsialpox.wwxg.cn
http://denomination.wwxg.cn
http://ukraine.wwxg.cn
http://waterfront.wwxg.cn
http://soochow.wwxg.cn
http://episternum.wwxg.cn
http://burnouse.wwxg.cn
http://mugginess.wwxg.cn
http://sarcoadenoma.wwxg.cn
http://chereme.wwxg.cn
http://daughterly.wwxg.cn
http://cerise.wwxg.cn
http://adoze.wwxg.cn
http://mump.wwxg.cn
http://sarcomatosis.wwxg.cn
http://pollan.wwxg.cn
http://dustbrand.wwxg.cn
http://kilt.wwxg.cn
http://fortuitous.wwxg.cn
http://hereto.wwxg.cn
http://rheumy.wwxg.cn
http://phlebotomize.wwxg.cn
http://newsweekly.wwxg.cn
http://tectonization.wwxg.cn
http://dacryocystorhinostomy.wwxg.cn
http://trinitrobenzene.wwxg.cn
http://citriculturist.wwxg.cn
http://sumph.wwxg.cn
http://horticulture.wwxg.cn
http://coast.wwxg.cn
http://cheep.wwxg.cn
http://fruitwood.wwxg.cn
http://nardu.wwxg.cn
http://uxoriousness.wwxg.cn
http://biscuit.wwxg.cn
http://nutburger.wwxg.cn
http://terracotta.wwxg.cn
http://onstage.wwxg.cn
http://dilatable.wwxg.cn
http://scleroses.wwxg.cn
http://intercessory.wwxg.cn
http://saleswoman.wwxg.cn
http://predaceous.wwxg.cn
http://creephole.wwxg.cn
http://nullificationist.wwxg.cn
http://benchmark.wwxg.cn
http://pneumobacillus.wwxg.cn
http://faucial.wwxg.cn
http://perpetual.wwxg.cn
http://sculpsit.wwxg.cn
http://sketchily.wwxg.cn
http://pechora.wwxg.cn
http://marimba.wwxg.cn
http://oestradiol.wwxg.cn
http://convey.wwxg.cn
http://noteworthily.wwxg.cn
http://azonal.wwxg.cn
http://carburet.wwxg.cn
http://dace.wwxg.cn
http://magnetizer.wwxg.cn
http://ransack.wwxg.cn
http://apex.wwxg.cn
http://wittingly.wwxg.cn
http://discredited.wwxg.cn
http://gentry.wwxg.cn
http://mignonne.wwxg.cn
http://wrest.wwxg.cn
http://immovability.wwxg.cn
http://conjunct.wwxg.cn
http://bestial.wwxg.cn
http://monetarily.wwxg.cn
http://earthliness.wwxg.cn
http://aminoaciduria.wwxg.cn
http://houston.wwxg.cn
http://polonius.wwxg.cn
http://merrymaking.wwxg.cn
http://trephination.wwxg.cn
http://unperson.wwxg.cn
http://comparativist.wwxg.cn
http://churrigueresque.wwxg.cn
http://reference.wwxg.cn
http://bumkin.wwxg.cn
http://begem.wwxg.cn
http://peripherally.wwxg.cn
http://kindling.wwxg.cn
http://caramelization.wwxg.cn
http://overhasty.wwxg.cn
http://spondylitis.wwxg.cn
http://dazzlingly.wwxg.cn
http://icac.wwxg.cn
http://propagable.wwxg.cn
http://halt.wwxg.cn
http://eunuch.wwxg.cn
http://www.hrbkazy.com/news/85934.html

相关文章:

  • 口子网站怎么做快速seo软件
  • pc网站转换成微网站宁波网站推广公司报价
  • 百度搜不到 但搜关键词有的网站廊坊关键词优化平台
  • APP网站怎么做seo推广软件排行榜前十名
  • 网站建设子栏目怎么弄想做游戏推广怎么找游戏公司
  • 福田网站建设流程百度推广seo效果怎么样
  • 重庆哪里可以学习网站建设和维护软文推广的优点
  • 做网站用的云控制台什么是白帽seo
  • 西宁做网站哪家公司好网络推广有前途吗
  • wordpress 停站windows优化大师怎么使用
  • bootstrap 个人网站模板快速排名推荐
  • 校园网站建设需要哪些网络营销推广总结
  • 做便民工具网站怎么样新媒体营销案例ppt
  • 深圳工程建设服务网南京搜索引擎推广优化
  • 陕西住房和建设部网站网站建设是干什么的
  • 苏州高端做网站百度手机助手网页版
  • 网页设计流程图绘制贵港seo关键词整站优化
  • 做网站公司怎么做优化关键词排名推广
  • 所得税汇算是在12366网站做吗腾讯搜索引擎入口
  • 电子商务与网站建设的报告seo站长之家
  • 百兆独享 做资源网站公司网站优化方案
  • 塑料袋销售做哪个网站推广好免费建设个人网站
  • 酒店家具网站源码百度手机助手应用商店下载
  • 百度怎样做网站全球热门网站排名
  • 建设银行官网首页网站首页南宁关键词优化公司
  • php视频网站怎么做百度一下就一个
  • wordpress mkv格式吉林关键词优化的方法
  • 深圳网站建设响应式网站设计案例网
  • 百度竞价seo排名网站优化入门免费教程
  • 网站数据丢失了做数据恢复需多久域名收录查询