java中的POI

2019-07-31  本文已影响0人  以宇宙为海的蓝鲸

POI是一个由Apache提供的jar包,用于支持处理Excel、Word、PPT等文件。

官网下载地址:http://poi.apache.org/

使用前应先导入jar包,并配置build path。

POI创建Excel文件并输入内容示例:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
​
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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;
​
public class ExportPoi {

 /**
 * 
 * @param clz  导出集合对象中每个元素的Class对象
 * @param ls  导出的集合对象
 * @param fileName  导出的文件名称
 * @param filePath  导出的文件路径
 * @param titls  导出文件的标题
 * @param fieldName  导出集合中对象的每个属性名称(英文)
 * @throws IllegalAccessException 属性的是不可见 抛出该异常
 * @throws IllegalArgumentException  非法参数异常
 * @throws IOException 连接错误
 */
 public static void export(Class<?> clz,List<?> ls,String fileName,String filePath,String[] titls,String[] fieldName) throws IllegalArgumentException, IllegalAccessException, IOException  {
 //创建工作簿对象
 Workbook wb = new HSSFWorkbook();

 //创建流对象
 FileOutputStream fs = new FileOutputStream(filePath+File.separator+fileName);

 //创建sheet
 Sheet sheet = wb.createSheet(fileName.substring(0,fileName.indexOf(".")));

 //创建row
 Row rowTitle = sheet.createRow(0);//表头

 //创建单元格
 for(int i = 0;i<titls.length;i++) {
 //创建单元格
 Cell cell = rowTitle.createCell(i);
 //填充值
 cell.setCellValue(titls[i]);
 }

 //添加数 据 循环创建List长度个row
 for(int i =0;i<ls.size();i++) {
 //创建row
 Row rowData = sheet.createRow(i+1);//行数据
 Field[] f = clz.getDeclaredFields();
 for(int j = 0;j<titls.length;j++) {
 //创建单元格
 Cell cell = rowData.createCell(j);
 //获取属性的名称
 f[j].setAccessible(true);
 cell.setCellValue(f[j].get(ls.get(i)).toString());
 f[j].setAccessible(false);
 }
 }

 //写出
 wb.write(fs);
 //关闭
 fs.close();
 wb.close();
 }

 public static void main(String[] args) throws Exception {

 List<Student> ls = new ArrayList<>();
 ls.add(new Student("张三1", 1, 1, 78));
 ls.add(new Student("张三2", 4, 2, 68));
 ls.add(new Student("张三3", 2, 1, 98));
 ls.add(new Student("张三5", 3, 2, 18));
 ls.add(new Student("张三4", 5, 1, 38));

 export(Student.class,ls,"学生成绩单.xls","C:\\Users\\22507\\Desktop",new String[] {"姓名","学号","性别","成绩"},new String[] {"name","id","gender","score"});

 }

}
​
class Student {
 private String name;
 private int id;
 private int gender;
 private int score;
 public Student(String name, int id, int gender, int score) {
 super();
 this.name = name;
 this.id = id;
 this.gender = gender;
 this.score = score;
 }
 public Student() {
 // TODO Auto-generated constructor stub
 }
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public int getId() {
 return id;
 }
 public void setId(int id) {
 this.id = id;
 }
 public int getGender() {
 return gender;
 }
 public void setGender(int gender) {
 this.gender = gender;
 }
 public int getScore() {
 return score;
 }
 public void setScore(int score) {
 this.score = score;
 } 
}
上一篇下一篇

猜你喜欢

热点阅读