首先:pom.xml导入所需依赖
#配置mybatis
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
#springboot配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<!-- 添加MySQL依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 添加JDBC依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
#gson工具类
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
#处理xls文本,可以采用jxl包
<!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl -->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
#本篇使用到的excel工具包
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
附上工具类及代码
package com.songshan.wieye;
import com.songshan.wieye.Util.ExcelUtil;
import com.songshan.wieye.Util.FieldInfo;
import com.songshan.wieye.entity.ExcelEntity;
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.util.CellReference;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class ExcelToList2 {
/**
* 将Excel数据转为List集合
*
* @param name Excel路径
*/
public static List<ExcelEntity> readExcel2List(String name) throws IOException {
InputStream is = new FileInputStream(name);
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
List<ExcelEntity> excelEntityList = new ArrayList<ExcelEntity>();
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
XSSFRow xssfRow = xssfSheet.getRow(rowNum);
int minColIx = xssfRow.getFirstCellNum();
int maxColIx = xssfRow.getLastCellNum();
List<String> rowList = new ArrayList<String>();
ExcelEntity excelEntity = new ExcelEntity();
for (int colIx = minColIx; colIx < maxColIx; colIx++) {
XSSFCell cell = xssfRow.getCell(colIx);
if (cell == null) {
continue;
}
rowList.add(cell.toString());
}
excelEntity.setNum(rowList.get(0));
excelEntity.setMerchantName(rowList.get(1));
excelEntity.setSrcStoreId(rowList.get(2));
excelEntity.setStoreName(rowList.get(3));
excelEntity.setTrdDt(rowList.get(4));
excelEntity.setCardId(rowList.get(5));
excelEntity.setNewPos(rowList.get(6));
excelEntity.setPOSTrdBillId(rowList.get(7));
excelEntity.setSumPointQty(rowList.get(8));
excelEntityList.add(excelEntity);
}
return excelEntityList;
}
//TODO:根据业务逻辑,修改List数据,并重新写出Excel文件
public static List<ExcelEntity> listService(List<ExcelEntity> excelEntityList) {
for (ExcelEntity excelEntity : excelEntityList) {
excelEntity.getPOSTrdBillId();
//获取当前交易日期为当年多少天,并判断其长度
int length = (method_2( excelEntity.getTrdDt())+"").length();
String s = doStringSub(excelEntity.getPOSTrdBillId(),length);
excelEntity.setPOSTrdBillId(s);
}
return excelEntityList;
}
/*
* 将处理后的List集合重新生成Excel文件
* */
public static void list2excel(String fileName,List<ExcelEntity> excelEntityList) throws IOException {
SXSSFWorkbook wb = new SXSSFWorkbook(1000); // 在内存当中保持 1000 行 , 超过的数据放到硬盘中
Sheet sh = wb.createSheet();
String info[] = {"","品牌名称","原店铺编号","店铺名称","交易日期","卡编号","new_Pos","POS交易单据编号","Sum(积分点数)"};
int rowIndex = 0;
int columnIndex = 0;
Row header = sh.createRow(0);
for (int i = 0; i < info.length; i++) {
header.createCell(columnIndex++).setCellValue(info[i]);
}
for(int rownum = 1; rownum <= excelEntityList.size();rownum++){
ExcelEntity excelEntity = excelEntityList.get(rownum-1);
Row row = sh.createRow(rownum);
row.createCell(0).setCellValue(excelEntity.getNum());
row.createCell(1).setCellValue(excelEntity.getMerchantName());
row.createCell(2).setCellValue(excelEntity.getSrcStoreId());
row.createCell(3).setCellValue(excelEntity.getStoreName());
row.createCell(4).setCellValue(excelEntity.getTrdDt());
row.createCell(5).setCellValue(excelEntity.getCardId());
row.createCell(6).setCellValue(excelEntity.getNewPos());
row.createCell(7).setCellValue(excelEntity.getPOSTrdBillId());
row.createCell(8).setCellValue(excelEntity.getSumPointQty());
}
FileOutputStream out = new FileOutputStream(fileName);
wb.write(out);
out.close();
// dispose of temporary files backing this workbook on disk
wb.dispose();
}
/*
* 将传入的字符串,根据指定长度处理截取
* */
public static String doStringSub(String s,int length){
s = s.isEmpty()?"ssssssssss":s;
StringBuffer sb = new StringBuffer(s);
s = sb.substring(0,sb.length()-length);
if(length < 3 && s.indexOf("0") == 0 ){
s = s.substring(1);
}
return s;
}
/*
* 获取传入的日期,是当月的第几天
* */
public static int method_2(String sdate) {
//创建Calendar对象
Calendar calendar = Calendar.getInstance();
//定义输入时间的格式
SimpleDateFormat format = new SimpleDateFormat("yyyy/M/d");
int days = -1;
try {
//将输入的时间转化为Date对象
Date date = format.parse(sdate);
//将Date对象传给calendar
calendar.setTime(date);
//获取它在这 一年中是第几天
days = calendar.get(Calendar.DAY_OF_YEAR);
} catch (ParseException e) {
e.printStackTrace();
}
return days;
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要替换的文件路径:(例如D://dataResource)");
String readFilePath = sc.nextLine();
System.out.println("请输入要替换的文件名:(例如:33)");
String readFileName = sc.nextLine();
//读取Excel
List<ExcelEntity> excelEntityList = readExcel2List(readFilePath+"/"+readFileName+".xlsx");
//处理数据
excelEntityList = listService(excelEntityList);
//导出数据
String fileName = readFilePath+"/result/"+readFileName+"y.xlsx";
list2excel(fileName,excelEntityList);
}
}
数据源配置
spring:
datasource:
#type: com.alibaba.druid.pool.DruidDataSource
#driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/testbysongshan?useUnicode=true&characterEncoding=utf8
username: root
password: ody1234
来源:https://blog.csdn.net/qq_34545939/article/details/98726321