java通过class读写excel的例子

  • n0_576111
    了解作者
  • 2.3MB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-14 08:13
    上传日期
我们常常遇到要把一个类class写入excel的时候,有时候class的字段非常多,我们不可能一个个的去get写入excel。这里写了一个通用方法。只要传入数据的list和类型,就能很方便的把数据写入excel,也能很方便的读取出来。
ExcelTest.zip
  • ExcelTest
  • bin
  • Io
  • User.class
    687B
  • BeanRefUtil.class
    6.3KB
  • ExcelIo.class
    8.2KB
  • .settings
  • org.eclipse.jdt.core.prefs
    629B
  • src
  • Io
  • ExcelIo.java
    5.9KB
  • User.java
    344B
  • BeanRefUtil.java
    5.7KB
  • .project
    385B
  • .classpath
    406B
  • poi-3.9-20121203.jar
    1.8MB
  • jxl.jar
    708.7KB
内容介绍
package Io; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.UUID; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Row; import jxl.Workbook; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import jxl.write.biff.RowsExceededException; /** * EXCEL IO操作 * * @作者 joe * @版本 V1.0 * @更新时间 2014-4-18 下午4:28:01 */ public class ExcelIo { private static <T> String[][] parseLand(List<T> objects,Class<T> clazz) { int size = objects.size(); Field[] fields = clazz.getDeclaredFields(); String[][] content = new String[size + 1][fields.length]; for (int i = 0; i < fields.length; i++) { Field f = fields[i]; content[0][i] = f.getName(); } for (int i = 0 ; i < size; i++) { T school = objects.get(i); for (int j = 0; j < fields.length; j++) { Field f = fields[j]; f.setAccessible(true); try { String value = ""; if (f.get(school) != null) { value = f.get(school).toString(); } content[i+1][j] = value; } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } return content; } public static String writeFile(String fileName, String[][] content) { WritableWorkbook wwb = null; String filePath = "c:\\" + fileName +".xls"; try { wwb = Workbook.createWorkbook(new File(filePath)); } catch (IOException e) { e.printStackTrace(); } if (wwb != null) { WritableSheet ws = wwb.createSheet(fileName, 1); for (int row = 0; row < content.length; row++) { for (int j = 0; j < content[row].length; j++) { Label labelC = new Label(j, row, content[row][j]); try { ws.addCell(labelC); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } } } try { wwb.write(); wwb.close(); return filePath; } catch (IOException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } } return null; } public static <T> List<T> excelToClass(File file,Class<T> clazz){ List<T> lands = new ArrayList<T>(); try { FileInputStream fileInputStream = new FileInputStream(file); POIFSFileSystem poifs = new POIFSFileSystem(fileInputStream); HSSFWorkbook workbook = new HSSFWorkbook(poifs); HSSFSheet sheet = workbook.getSheetAt(0); Iterator<Row> rows = sheet.rowIterator(); int index = 0; List<String> keys = new ArrayList<String>(); while (rows.hasNext()) { HSSFRow row = (HSSFRow) rows.next(); if(index == 0){ // int num = row.getRowNum(); int num = row.getLastCellNum(); for(int i = 0 ; i < num;i++ ){ HSSFCell cell = row.getCell(i); if(cell!=null){ String value = getStringCellValue(cell); keys.add(value); } } } if(index>=1){ //决定从哪一行开始提取,这里从第一行 Map<String,String> map = new HashMap<String, String>(); int num = row.getLastCellNum(); for(int i = 0 ; i < num;i++ ){ HSSFCell cell = row.getCell(i); if(cell!=null){ map.put(keys.get(i), getStringCellValue(cell)); } } try { T land = newTclass(clazz); BeanRefUtil.setFieldValue(land, map); lands.add(land); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } } index++; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return lands; } private static <T> T newTclass(Class<T> clazz) throws InstantiationException, IllegalAccessException{ T a=clazz.newInstance(); return a; } private static String getStringCellValue(HSSFCell cell) {// 获取单元格数据内容为字符串类型的数据 String strCell = ""; switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_STRING: strCell = cell.getStringCellValue(); break; case HSSFCell.CELL_TYPE_NUMERIC: strCell = String.valueOf(cell.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_BOOLEAN: strCell = String.valueOf(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_BLANK: strCell = ""; break; default: strCell = ""; break; } if (strCell.equals("") || strCell == null) { return ""; } return strCell; } private static String getUUID() { UUID uuid = UUID.randomUUID(); String str = uuid.toString(); return str.substring(0, 8); } public static void main(String[] args) { //写入测试 List<User> users=new ArrayList<User>(); User u=new User(); u.setName("joe"); u.setPassword("123"); users.add(u); User s=new User(); s.setName("sophia"); s.setPassword("123456"); users.add(s); String[][] content = parseLand(users,User.class); String uuid = getUUID(); writeFile(uuid, content); System.out.println("写入完成"); // //读取测试 User temp=new User(); File file = new File("c:\\" + uuid +".xls"); List<User> us=excelToClass(file,User.class); for(User i:us) { System.out.println(i.getName()); System.out.println(i.getPassword()); } System.out.println("读取完成"); } }
评论
    相关推荐
    • Java
      Java
    • java
      Java
    • Java
      Java
    • Java
      Java项目
    • Java
      Java 对于Java练习
    • java
      Java 此回购包括我学习Java的旅程。
    • java
      Java Java基础
    • Java
      Java
    • Java
      Java 我创建的Java项目
    • Java
      Java 2021/02/19 .java Main.java 学生.java Bmi.java 。班级 Bmi类 主类 MyBmi.class 学生班 学生样本类 2021/02/20 .java DeleteFile.java WriteFile.java MyDataPrint.java 。班级 DeleteFile.class ...