Java中的poi是什么_java中的POI
POI是⼀个由Apache提供的jar包,⽤于⽀持处理Excel、Word、PPT等⽂件。
使⽤前应先导⼊jar包,并配置build path。
POI创建Excel⽂件并输⼊内容⽰例:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import flect.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 = ateRow(0);//表头
//创建单元格
for(int i = 0;i
//创建单元格
Cell cell = ateCell(i);
//填充值
cell.setCellValue(titls[i]);
}
/
/添加数 据 循环创建List长度个row
for(int i =0;i
//创建row
Row rowData = ateRow(i+1);//⾏数据
Field[] f = DeclaredFields();
for(int j = 0;j
//创建单元格
Cell cell = ateCell(j);
//获取属性的名称
f[j].setAccessible(true);
cell.setCellValue(f[j].(i)).toString());
f[j].setAccessible(false);
}
}
//写出
wb.write(fs);
//关闭
fs.close();
wb.close();
}
public static void main(String[] args) throws Exception {
List 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.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) {
java中index是什么意思
this.id = id;
}
public int getGender() {
return gender;
}
public void setGender(int gender) { der = gender;
}
public int getScore() {
return score;
}
public void setScore(int score) { this.score = score;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论