java 将list对象写入excel
来源:互联网 发布:ubuntu wifi掉线 编辑:程序博客网 时间:2024/05/04 06:48
package helloworld;import java.io.File;import java.io.IOException;import java.lang.reflect.Field;import java.util.ArrayList;import java.util.List;import jxl.Workbook;import jxl.write.Label;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;/** * jxl写入excel文件 * * @author Trina * */public class JxlExcel {public static void main(String[] args) throws Exception {People pe1 = new People("hh", "8","hebei");People pe2 = new People("ff", "4","cangzhou");List<People> peos = new ArrayList<People>();peos.add(pe1);peos.add(pe2);writeExcel(peos, "g:/hell.xls"); }/** * 通过泛型的list<?>可以匹配所有的类,只要那个类所有的属性是String类型的 * @param peos 实体的List * @param fileAbPath 绝对路径 * @throws Exception */public static void writeExcel(List<?> peos, String fileAbPath) throws Exception {// 列数int columnNum;int rows = peos.size(); // 行数File file;// 判断文件类型,如果不是以.xls结尾,那么停止执行file = new File(fileAbPath);if (!file.getName().endsWith("xls")) {return;}if (!file.exists()) {// 如果文件不存在,则重新创建file.createNewFile();}if (peos.size() == 0 || peos.equals("") || peos == null) {return;}// 反射获得类的属性个数 ,也就得到了你要打印的列数Class obClass = peos.get(0).getClass();Field[] fields = obClass.getDeclaredFields();columnNum = fields.length;// 得到该对象的属性名字,存到数组中去String[] titleNames = new String[columnNum];for (int i = 0; i < columnNum; i++) {titleNames[i] = fields[i].getName();System.out.println(titleNames[i]);}// 创建工作簿WritableWorkbook workbook = Workbook.createWorkbook(file);// 创建sheetWritableSheet sheet = workbook.createSheet("sheetOne", 0);Label label =null;for(int i = 0; i<peos.size(); i++){//写入标题--类的属性名if(i == 0){for(int j = 0; j<columnNum; j++){label = new Label(j,0,titleNames[j]);sheet.addCell(label);}}Object people = peos.get(i);//读取每个实体中的字段上的值for(int j = 0; j<columnNum; j++){String value = (String)fields[j].get((Object)people);label = new Label(j,i+1,value);sheet.addCell(label);}}//写入workbook.write(); // 关闭 workbook.close();}}/** * 测试实体类 * 实体属性必须准守 public String 类型 * @author Administrator */class People {public String name;public String age;public String addr;public People() {}public People(String name, String age,String addr) {this.name = name;this.age = age;this.addr = addr;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public String getAddr() {return addr;}public void setAddr(String addr) {this.addr = addr;}}
2 0
- java 将list对象写入excel
- java:将list写入excel中
- java将数据写入excel
- java将对象写入文件
- java 将对象写入文件
- java将list写入到excel中并导出到本地(POI)
- 使用JXL将List集合写入到Excel表中
- java Excel导入、自适应版本、将Excel转成List<map>对象
- java中将list、map对象写入文件
- java中将list、map对象写入文件
- java 将对象写入到文件中
- PYTHON将list或/dict对象写入txt/json文件
- 将文本文件写入excel
- java如何将Excel表格中内容写入oracle数据库
- 将excel中的内容写入到PDF中(java)
- java将数据库数据写入到Excel中
- 用java程序将数据写入到Excel中
- 简单实现利用java将数据写入到excel
- Qt之资源系统
- HTML学习笔记(一)
- 8086汇编语言 在debug中使用段前缀
- 查看和修改mysql最大连接数
- idea maven mvn archetype:generate 速度缓慢问题
- java 将list对象写入excel
- ImageView充满屏幕
- Android酷炫实用的开源框架(UI框架)
- Docker从零单排系列之基础篇(一)
- 我对广播的一下理解
- Mybatis中运用小技巧(三)存储过程的运用
- AngularJS 路由
- Android序列化
- 前端开发中的js调试技巧