本文共 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页中一一对应 * */ publicList 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/