博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Excel操作-ApachePOI
阅读量:4304 次
发布时间:2019-05-27

本文共 3603 字,大约阅读时间需要 12 分钟。

只想无保护措施的玩一宿马银霜

POM文件需要引入三个包,都是apache poi的

org.apache.poi
poi
3.14
org.apache.poi
poi-ooxml
3.14
org.apache.poi
poi-ooxml-schemas
3.14

导出

private static void writer() throws IOException {		// 创建工作文档对象		Workbook wb = new XSSFWorkbook();		// 创建sheet对象		Sheet sheet1 = wb.createSheet("sheet1");		// 循环写入行数据		for (int i = 0; i < 5; i++) {			Row row = sheet1.createRow(i);			// 循环写入列数据			for (int j = 0; j < 8; j++) {				Cell cell = row.createCell(j);				cell.setCellValue("测试" + j);			}		}		OutputStream out = new FileOutputStream("C:\\Users\\aaa.xlsx");		wb.write(out);		wb.close();	}	//导出时对cell加前景颜色(你没看错,不是背景,我一直以为是背景颜色)	//以下代码在博客中纯手打,如果我写错了个字母,复制的时候可能有错,因为我也不知道手打的是否正确	public void aaa(){			CellStyle cs=wb.createCellStyle();			cs.setFillForegroundColor(HSSFColor.RED.index);			cs.setFillPattern(CellStyle.SOLID_FOREGROUND);			row.createCell(0).setCellStyle(cs);//对本行第一列填充红色	}

导入

假设有一个User类,里面有name,age,sex三个属性:那么
excel中有两个sheet页,一个叫data,一个叫config,其中config中的第一行第一列的值是name,age,sex,注意用英文逗号隔开,第一行第二列的值是0,表示从data页的第0行开始读取数据,是几就是从第几行开始读取,下面是java代码

import java.io.FileInputStream;import java.io.InputStream;import java.lang.reflect.Field;import java.util.ArrayList;import java.util.List;import java.util.regex.Pattern;import javax.naming.NameNotFoundException;import org.apache.poi.openxml4j.exceptions.InvalidFormatException;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.ss.usermodel.WorkbookFactory;public class ExcelUtils {		/**	 * 上传的excel表格中,声明的所有字段都必须有值,因为没有值就没有任何意义,那何必上传呢??	 * 所以有值为空,则不允许	 * 1.必须有一个sheet页叫做config,里面(0,0)装的是实体类对应的字段名字,(0,1)里是一个数字,表示从第几行开始读取	 * 2.必须有一个sheet页叫做data,它的每行字段,都与config页中一一对应	 * 	 */	public 
List
toObject(InputStream excelStream,Class
clazz) throws Exception{ Workbook workbook = WorkbookFactory.create(excelStream); Sheet config=workbook.getSheet("config"); if(config==null) { throw new NameNotFoundException("表格中不存在名字为config的sheet页"); } Cell configCell=config.getRow(0).getCell(0); //表示从第几行开始读取数据,第一行是0 int fromRead=0; try{ Cell cell01=config.getRow(0).getCell(1); cell01.setCellType(Cell.CELL_TYPE_STRING); String fr=config.getRow(0).getCell(1).getStringCellValue(); fromRead=Integer.valueOf(fr); }catch (Exception e) { throw new InvalidFormatException("config sheet页中的(0,1)单元格不是数字"); } String fieldConfig=configCell.getStringCellValue(); if(!Pattern.matches("[a-z,]*", fieldConfig)) { throw new IllegalArgumentException("config sheet页中的(0,0)单元格的格式不符合约定,它必须是[字段,字段]这种形式"); } int count=0; Sheet datas=workbook.getSheet("data"); if(datas==null) { throw new NameNotFoundException("表格中不存在名字为data的sheet页"); } String [] fields=fieldConfig.split(","); List
objs=new ArrayList<>(); int nowRow=1;//当前正在读取第几行 for(Row row:datas) { if(count!=fromRead) { count++; continue; } T rowEntity=(T)clazz.getConstructor().newInstance(); int nowColl=1; for(int cellnum=0;cellnum
users=eu.toObject(inputStream, User.class); System.out.println(users.size()); for(User u:users) { System.out.println(u.getName()); } }}

转载地址:http://yvhws.baihongyu.com/

你可能感兴趣的文章
Sqlerver2005+按照ID分组取前几条
查看>>
php异常处理
查看>>
mac上安装glfw
查看>>
vue 父子传值,子页面没有实时刷新的问题
查看>>
Python的编码和解码
查看>>
docker
查看>>
停车场系统安全岛设计施工要求
查看>>
Docker实战
查看>>
asp.net core结合Gitlab-CI实现自动化部署
查看>>
RDIFramework.NET ━ .NET快速信息化系统开发框架 V2.7 版本发布
查看>>
EasyNVR H5无插件摄像机直播解决方案前端解析之:关于直播页面和视频列表页面切换的问题...
查看>>
django搭建一个小型的服务器运维网站-拿来即用的bootstrap模板
查看>>
Connection strings for SQL Server 2008
查看>>
BulkInsert导入CSV文件
查看>>
RNN 入门教程 Part 4 – 实现 RNN-LSTM 和 GRU 模型
查看>>
使用 multiparty 模块进行文件上传
查看>>
你要知道
查看>>
经典IPC$入侵及问题
查看>>
RESTful服务 安全
查看>>
vue加载本地json文件
查看>>