java读写excel文件实现POI解析Excel的方法

网友投稿 386 2023-01-21


java读写excel文件实现POI解析Excel的方法

在日常工作中,我们常常会进行文件读写操作,除去我们最常用的纯文本文件读写,更多时候我们需要对Excel中的数据进行读取操作,本文将介绍Excel读写的常用方法,希望对大家学习java读写Excel会有帮助。

package com.zhx.base.utils;

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

import org.apache.poi.ss.usermodel.*;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.ArrayList;

import java.util.List;

/**

* POI解析Excel

*/

public class ExcelReaderUtil {

/**

* 根据fileType不同读取excel文件

*

* @param path

* @param path

* @throws IOException

*/

public static List> readExcel(String path) {

String fileType = path.substring(path.lastIndexOf(".") + 1);

// return a list contains many list

List> lists = new ArrayList>();

//读取excel文件

InputStream is = null;

try {

is = new FileInputStream(path);

//获取工作薄

Workbook wb = null;

if (fileType.equals("xls")) {

wb = new HSSFWorkbook(is);

} else if (fileType.equals("xlsx")) {

wb = new XSSFWorkbook(is);

} else {

return null;

}

//读取第一个工作页sheet

Sheet sheet = wb.getSheetAt(0);

//第一行为标题

for (Row row : sheet) {

ArrayList list = new ArrayList();

for (Cell cell : row) {

//根据不同类型转化成字符串

cell.setCellType(Cell.CELL_TYPE_STRING);

list.add(cell.getStringCellValue());

}

lists.add(list);

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (is != null) is.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return lists;

}

/**

* 创建Excel.xls

* @param lists 需要写入xls的数据

* @param titles 列标题

* @param name 文件名

* @return

* @throws IOException

*/

public static Workbook creatExcel(List> lists, String[] titles, String name) throws IOException {

System.out.println(lists);

//创建新的工作薄

Workbook wb = new HSSFWorkbook();

// 创建第一个sheet(页),并命名

Sheet sheet = wb.createSheet(name);

// 手动设置列宽。第一个参数表示要为第几列设;,第二个参数表示列的宽度,n为列高的像素数。

for(int i=0;i

sheet.setColumnWidth((short) i, (short) (35.7 * 150));

}

// 创建第一行

Row row = sheet.createRow((short) 0);

// 创建两种单元格格式

CellStyle cs = wb.createCellStyle();

CellStyle cs2 = wb.createCellStyle();

// 创建两种字体

Font f = wb.createFont();

Font f2 = wb.createFont();

// 创建第一种字体样式(用于列名)

f.setFontHeightInPoints((short) 10);

f.setColor(IndexedColors.BLACK.getIndex());

f.setBoldweight(Font.BOLDWEIGHT_BOLD);

// 创建第二种字体样式(用于值)

f2.setFontHeightInPoints((short) 10);

f2.setColor(IndexedColors.BLACK.getIndex());

// 设置第一种单元格的样式(用于列名)

cs.setFont(f);

cs.setBorderLeft(CellStyle.BORDER_THIN);

cs.setBorderRight(CellStyle.BORDER_THIN);

cs.setBorderTop(CellStyle.BORDER_THIN);

cs.setBorderBottom(CellStyle.BORDER_THIN);

cs.setAlignment(CellStyle.ALIGN_CENTER);

// 设置第二种单元格的样式(用于值)

cs2.setFont(f2);

cs2.setBorderLeft(CellStyle.BORDER_THIN);

cs2.setBorderRight(CellStyle.BORDER_THIN);

cs2.setBorderTop(CellStyle.BORDER_THIN);

cs2.setBorderBottom(CellStyle.BORDER_THIN);

cs2.setAlignment(CellStyle.ALIGN_CENTER);

//设置列名

for(int i=0;i

Cell cell = row.createCell(i);

cell.setCellValue(titles[i]);

cell.setCellStyle(cs);

}

if(lists == null || lists.size() == 0){

return wb;

}

//设置每行每列的值

for (short i = 1; i <= lists.size(); i++) {

// Row 行,Cell 方格OfMQn , Row 和 Cell 都是从0开始计数的

// 创建一行,在页sheet上

Row row1 = sheet.createRow((short)i);

for(short j=0;j

// 在row行上创建一个方格

Cell cell = row1.createCell(j);

cell.setCellValue(lists.get(i-1).get(j));

cell.setCellStyle(cs2);

}

}

return wb;

}

public static void main(String[] args) {

String path = "d:/software/企发支付-员工信息表.xlsx";

List> lists = readExcel(path);

for (List list : lists) {

for (String strs : list) {

System.out.println(strs);

}

}

}

}

需要导入的jar包:

org.apache.poi

poi-excelant

3.14

准备需要读写的文件:

上述工具类中将每行放到一个list中,然后每行的每列放入到一个list中,这里再根据自己需求去对表中数据进行处理:

我现在要取得企业名称(资和信***)和从第七行起开始的id、姓名、识别号存入数据库中,这边我只展示service层处理,mybatis进行批量插入:

public Map insEm(File file) throws FileNotFoundException {

String companyName = "";

String epid = "";

List listMap = new ArrayList<>();

List> lists = ExcelReaderUtil.readExcel(file.getPath());

for (int j = 0; j < lists.size(); j++) {

List list = lists.get(j);

for (int i = 0; i < list.size(); i++) {

if (list.get(i).equals("企业名称")) {

companyName = list.get(i + 1).toString();

epid = employeeMapper.selEPID(companyName);

break;

} else if (list.get(i).equals("员工识别号")) {

for (int m = j + 1; m < lists.size() - 1; m++) {

Map map = new HashMap();

if (null != lists.get(m) && lists.get(m).size() > 0) {

List dataList = lists.get(m);

map.put("id", dataList.get(0));

map.put("epid", epid);

map.put("name", dataList.get(1));

map.put("identify", dataList.get(2));

listMap.add(map);

}

}

}

}

}

Map dataMap = new HashMap();

dataMap.put("employees", listMap);

employeeMapper.insEm(dataMap);

return null;

}

insert into qf_employee_info(epid,employee_id,user_name,phone,user_email,status,create_time,creater,create_type) value

(#{item.epid},"",#{item.name},#{item.identify},"",1,now(),"",2)

最后数据库

sheet.setColumnWidth((short) i, (short) (35.7 * 150));

}

// 创建第一行

Row row = sheet.createRow((short) 0);

// 创建两种单元格格式

CellStyle cs = wb.createCellStyle();

CellStyle cs2 = wb.createCellStyle();

// 创建两种字体

Font f = wb.createFont();

Font f2 = wb.createFont();

// 创建第一种字体样式(用于列名)

f.setFontHeightInPoints((short) 10);

f.setColor(IndexedColors.BLACK.getIndex());

f.setBoldweight(Font.BOLDWEIGHT_BOLD);

// 创建第二种字体样式(用于值)

f2.setFontHeightInPoints((short) 10);

f2.setColor(IndexedColors.BLACK.getIndex());

// 设置第一种单元格的样式(用于列名)

cs.setFont(f);

cs.setBorderLeft(CellStyle.BORDER_THIN);

cs.setBorderRight(CellStyle.BORDER_THIN);

cs.setBorderTop(CellStyle.BORDER_THIN);

cs.setBorderBottom(CellStyle.BORDER_THIN);

cs.setAlignment(CellStyle.ALIGN_CENTER);

// 设置第二种单元格的样式(用于值)

cs2.setFont(f2);

cs2.setBorderLeft(CellStyle.BORDER_THIN);

cs2.setBorderRight(CellStyle.BORDER_THIN);

cs2.setBorderTop(CellStyle.BORDER_THIN);

cs2.setBorderBottom(CellStyle.BORDER_THIN);

cs2.setAlignment(CellStyle.ALIGN_CENTER);

//设置列名

for(int i=0;i

Cell cell = row.createCell(i);

cell.setCellValue(titles[i]);

cell.setCellStyle(cs);

}

if(lists == null || lists.size() == 0){

return wb;

}

//设置每行每列的值

for (short i = 1; i <= lists.size(); i++) {

// Row 行,Cell 方格OfMQn , Row 和 Cell 都是从0开始计数的

// 创建一行,在页sheet上

Row row1 = sheet.createRow((short)i);

for(short j=0;j

// 在row行上创建一个方格

Cell cell = row1.createCell(j);

cell.setCellValue(lists.get(i-1).get(j));

cell.setCellStyle(cs2);

}

}

return wb;

}

public static void main(String[] args) {

String path = "d:/software/企发支付-员工信息表.xlsx";

List> lists = readExcel(path);

for (List list : lists) {

for (String strs : list) {

System.out.println(strs);

}

}

}

}

需要导入的jar包:

org.apache.poi

poi-excelant

3.14

准备需要读写的文件:

上述工具类中将每行放到一个list中,然后每行的每列放入到一个list中,这里再根据自己需求去对表中数据进行处理:

我现在要取得企业名称(资和信***)和从第七行起开始的id、姓名、识别号存入数据库中,这边我只展示service层处理,mybatis进行批量插入:

public Map insEm(File file) throws FileNotFoundException {

String companyName = "";

String epid = "";

List listMap = new ArrayList<>();

List> lists = ExcelReaderUtil.readExcel(file.getPath());

for (int j = 0; j < lists.size(); j++) {

List list = lists.get(j);

for (int i = 0; i < list.size(); i++) {

if (list.get(i).equals("企业名称")) {

companyName = list.get(i + 1).toString();

epid = employeeMapper.selEPID(companyName);

break;

} else if (list.get(i).equals("员工识别号")) {

for (int m = j + 1; m < lists.size() - 1; m++) {

Map map = new HashMap();

if (null != lists.get(m) && lists.get(m).size() > 0) {

List dataList = lists.get(m);

map.put("id", dataList.get(0));

map.put("epid", epid);

map.put("name", dataList.get(1));

map.put("identify", dataList.get(2));

listMap.add(map);

}

}

}

}

}

Map dataMap = new HashMap();

dataMap.put("employees", listMap);

employeeMapper.insEm(dataMap);

return null;

}

insert into qf_employee_info(epid,employee_id,user_name,phone,user_email,status,create_time,creater,create_type) value

(#{item.epid},"",#{item.name},#{item.identify},"",1,now(),"",2)

最后数据库

Cell cell = row.createCell(i);

cell.setCellValue(titles[i]);

cell.setCellStyle(cs);

}

if(lists == null || lists.size() == 0){

return wb;

}

//设置每行每列的值

for (short i = 1; i <= lists.size(); i++) {

// Row 行,Cell 方格OfMQn , Row 和 Cell 都是从0开始计数的

// 创建一行,在页sheet上

Row row1 = sheet.createRow((short)i);

for(short j=0;j

// 在row行上创建一个方格

Cell cell = row1.createCell(j);

cell.setCellValue(lists.get(i-1).get(j));

cell.setCellStyle(cs2);

}

}

return wb;

}

public static void main(String[] args) {

String path = "d:/software/企发支付-员工信息表.xlsx";

List> lists = readExcel(path);

for (List list : lists) {

for (String strs : list) {

System.out.println(strs);

}

}

}

}

需要导入的jar包:

org.apache.poi

poi-excelant

3.14

准备需要读写的文件:

上述工具类中将每行放到一个list中,然后每行的每列放入到一个list中,这里再根据自己需求去对表中数据进行处理:

我现在要取得企业名称(资和信***)和从第七行起开始的id、姓名、识别号存入数据库中,这边我只展示service层处理,mybatis进行批量插入:

public Map insEm(File file) throws FileNotFoundException {

String companyName = "";

String epid = "";

List listMap = new ArrayList<>();

List> lists = ExcelReaderUtil.readExcel(file.getPath());

for (int j = 0; j < lists.size(); j++) {

List list = lists.get(j);

for (int i = 0; i < list.size(); i++) {

if (list.get(i).equals("企业名称")) {

companyName = list.get(i + 1).toString();

epid = employeeMapper.selEPID(companyName);

break;

} else if (list.get(i).equals("员工识别号")) {

for (int m = j + 1; m < lists.size() - 1; m++) {

Map map = new HashMap();

if (null != lists.get(m) && lists.get(m).size() > 0) {

List dataList = lists.get(m);

map.put("id", dataList.get(0));

map.put("epid", epid);

map.put("name", dataList.get(1));

map.put("identify", dataList.get(2));

listMap.add(map);

}

}

}

}

}

Map dataMap = new HashMap();

dataMap.put("employees", listMap);

employeeMapper.insEm(dataMap);

return null;

}

insert into qf_employee_info(epid,employee_id,user_name,phone,user_email,status,create_time,creater,create_type) value

(#{item.epid},"",#{item.name},#{item.identify},"",1,now(),"",2)

最后数据库

// 在row行上创建一个方格

Cell cell = row1.createCell(j);

cell.setCellValue(lists.get(i-1).get(j));

cell.setCellStyle(cs2);

}

}

return wb;

}

public static void main(String[] args) {

String path = "d:/software/企发支付-员工信息表.xlsx";

List> lists = readExcel(path);

for (List list : lists) {

for (String strs : list) {

System.out.println(strs);

}

}

}

}

需要导入的jar包:

org.apache.poi

poi-excelant

3.14

准备需要读写的文件:

上述工具类中将每行放到一个list中,然后每行的每列放入到一个list中,这里再根据自己需求去对表中数据进行处理:

我现在要取得企业名称(资和信***)和从第七行起开始的id、姓名、识别号存入数据库中,这边我只展示service层处理,mybatis进行批量插入:

public Map insEm(File file) throws FileNotFoundException {

String companyName = "";

String epid = "";

List listMap = new ArrayList<>();

List> lists = ExcelReaderUtil.readExcel(file.getPath());

for (int j = 0; j < lists.size(); j++) {

List list = lists.get(j);

for (int i = 0; i < list.size(); i++) {

if (list.get(i).equals("企业名称")) {

companyName = list.get(i + 1).toString();

epid = employeeMapper.selEPID(companyName);

break;

} else if (list.get(i).equals("员工识别号")) {

for (int m = j + 1; m < lists.size() - 1; m++) {

Map map = new HashMap();

if (null != lists.get(m) && lists.get(m).size() > 0) {

List dataList = lists.get(m);

map.put("id", dataList.get(0));

map.put("epid", epid);

map.put("name", dataList.get(1));

map.put("identify", dataList.get(2));

listMap.add(map);

}

}

}

}

}

Map dataMap = new HashMap();

dataMap.put("employees", listMap);

employeeMapper.insEm(dataMap);

return null;

}

insert into qf_employee_info(epid,employee_id,user_name,phone,user_email,status,create_time,creater,create_type) value

(#{item.epid},"",#{item.name},#{item.identify},"",1,now(),"",2)

最后数据库


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java 方法实现接口(JAVA接口实现)
下一篇:java中ConcurrentHashMap的读操作为什么不需要加锁
相关文章

 发表评论

暂时没有评论,来抢沙发吧~