package com.test.order.config;import com.test.order.domain.HavalDO;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.xssf.usermodel.*;import java.io.*;import java.util.ArrayList;import java.util.List;/** * @program: TestDemo * @Date: 2018/8/19 12:55 * @Author: Mr.Niu * @Description: */public class ExcelHelper { public ListexcleIn() { List list = new ArrayList (); HavalDO havalDO = null; try { InputStream is = new FileInputStream("E:\\bzk.xlsx"); XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is); // 获取选项卡对象 第0个选项卡 , 因为们这里只有一个选项卡,如果你每个选项卡的内容是一样,可以通过循环取出 XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0); // 循环取出每行的值 for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) { XSSFRow xssfRow = xssfSheet.getRow(rowNum); havalDO = new HavalDO(); //注意Poi读取的内容是有类型的,处理起来也jxl有所不同 String str = null; if(xssfRow.getCell(0)!=null){ xssfRow.getCell(0).setCellType(Cell.CELL_TYPE_STRING); str = xssfRow.getCell(0).getStringCellValue(); } String [] strs=str.split("-"); String append = null; for (String s1 :strs){ append+=s1; } String strss = append.substring(4,append.length()); havalDO.setOem(strss); havalDO.setName(xssfRow.getCell(1).getStringCellValue()); havalDO.setPrice(xssfRow.getCell(2).getNumericCellValue()); list.add(havalDO); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } /** * 针对Book类进行导出的操作 * * @param list */ public void excleOut(List list) { // 创建Excel文档 XSSFWorkbook hwb = new XSSFWorkbook(); // 通过excle对象创建一个选项卡对象 XSSFSheet sheet = hwb.createSheet("sheet1"); HavalDO havalDO = null; // 循环list创建行 // 新建一行 XSSFRow row = sheet.createRow(0); // 设置i+1行第0列的数据 row.createCell(0).setCellValue("oem"); // 设置i+1行第1列的数据 row.createCell(1).setCellValue("name"); // 设置i+1行第2列的数据 row.createCell(2).setCellValue("price_4s"); // for (int i = 1; i < list.size(); i++) { // 新建一行 row = sheet.createRow(i); havalDO = list.get(i); // 设置i+1行第0列的数据 row.createCell(0).setCellValue(havalDO.getOem()); // 设置i+1行第1列的数据 row.createCell(1).setCellValue(havalDO.getName()); // 设置i+1行第2列的数据 row.createCell(2).setCellValue(havalDO.getPrice()); } OutputStream out = null; try { out = new FileOutputStream("E:/bookPoi1.xlsx"); hwb.write(out); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { ExcelHelper eh = new ExcelHelper(); List lh = eh.excleIn(); Integer i = 0; for (HavalDO hd : lh) { i++; System.out.println("第" + i + "行数据:" + hd.toString()); } eh.excleOut(lh); }}