注意用maven,自己配置依赖jar包 比较难凑齐,不然老出问题
pom 配置:
<dependency> ? ? <groupId>cn.hutool</groupId> ? ? <artifactId>hutool-all</artifactId> ? ? <version>4.5.10</version> </dependency>
<dependency> ? ? <groupId>org.apache.poi</groupId> ? ? <artifactId>poi-ooxml</artifactId> ? ? <version>4.1.0</version> </dependency>
code:
import java.util.ArrayList; import java.util.Date; import java.util.LinkedHashMap; import java.util.List; import java.util.Map;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.poi.excel.ExcelUtil; import cn.hutool.poi.excel.ExcelWriter;
public class BeanMode { ?? ?private String name; ? ? private int age; ? ? private double score; ? ? private boolean isPass; ? ? private Date examDate; ? ? private String sex; ? ? public String getSex() { ?? ??? ?return sex; ?? ?} ?? ?public void setSex(String sex) { ?? ??? ?this.sex = sex; ?? ?} ?? ?public String getName() { ? ? ? ? return name; ? ? } ? ? public void setName(String name) { ? ? ? ? this.name = name; ? ? } ? ? public int getAge() { ? ? ? ? return age; ? ? } ? ? public void setAge(int age) { ? ? ? ? this.age = age; ? ? } ? ? public double getScore() { ? ? ? ? return score; ? ? } ? ? public void setScore(double score) { ? ? ? ? this.score = score; ? ? } ? ? public boolean isPass() { ? ? ? ? return isPass; ? ? } ? ? public void setPass(boolean isPass) { ? ? ? ? this.isPass = isPass; ? ? } ? ? public Date getExamDate() { ? ? ? ? return examDate; ? ? } ? ? public void setExamDate(Date examDate) { ? ? ? ? this.examDate = examDate; ? ? } ? ?? ? ?? ? ? public static void listMode() { ?? ??? ?// TODO Auto-generated method stub ?? ??? ?List<String> row1 = CollUtil.newArrayList("ID", "姓名", "年龄", "爱好"); ?? ??? ?List<String> row2 = CollUtil.newArrayList("1", "张三", "31", "网球"); ?? ??? ?List<String> row3 = CollUtil.newArrayList("2", "李四", "34", "篮球"); ?? ??? ?List<String> row4 = CollUtil.newArrayList("3", "王五", "21", "羽毛球"); ?? ??? ?List<String> row5 = CollUtil.newArrayList("4", "小明", "16", "乒乓球"); ?? ??? ?List<List<String>> rows = CollUtil.newArrayList(row1, row2, row3, row4, row5); ?? ??? ? ?? ??? ?ExcelWriter writer = ExcelUtil.getWriter("listMode.xlsx"); ?? ? ?? ??? ?writer.passCurrentRow(); ?? ??? ?// 合并单元格后的标题行,使用默认标题样式 ?? ??? ?writer.merge(row1.size() - 1, "爱好列表"); ?? ??? ? ?? ??? ?writer.write(rows, true); ?? ??? ? ?? ??? ?writer.close(); ?? ?} ?? ? ?? ?public static void ?mapMode() { ?? ??? ?// TODO Auto-generated method stub ?? ??? ?Map<String, Object> row1 = new LinkedHashMap<>(); ?? ??? ?row1.put("名字", "小明"); ?? ??? ?row1.put("英语", 68); ?? ??? ?row1.put("数学", 88.32); ?? ??? ?row1.put("是否及格", true); ?? ??? ?row1.put("日期", DateUtil.date()); ?? ??? ?Map<String, Object> row2 = new LinkedHashMap<>(); ?? ??? ?row2.put("名字", "小敏"); ?? ??? ?row2.put("英语", 33); ?? ??? ?row2.put("数学", 59.50); ?? ??? ?row2.put("是否及格", false); ?? ??? ?row2.put("日期", DateUtil.date()); ?? ??? ?ArrayList<Map<String, Object>> rows = CollUtil.newArrayList(row1, row2); ?? ??? ? ?? ??? ?ExcelWriter writer = ExcelUtil.getWriter("mapMode.xlsx"); ?? ??? ?// 合并单元格后的标题行,使用默认标题样式 ?? ??? ?writer.merge(row1.size() - 1, "成绩单"); ?? ??? ? ?? ??? ?writer.write(rows, true); ?? ??? ? ?? ??? ?writer.close(); ?? ?}
?? ?public static void main(String[] args) { ?? ??? ?// TODO Auto-generated method stub ?? ??? ?listMode(); ?? ??? ?mapMode(); ?? ??? ?BeanMode bean1 = new BeanMode(); ?? ??? ?bean1.setName("小明"); ?? ??? ?bean1.setAge(22); ?? ??? ?bean1.setPass(true); ?? ??? ?bean1.setScore(66.30); ?? ??? ?bean1.setExamDate(DateUtil.date()); ?? ??? ?bean1.setSex("男"); ?? ??? ?TestBean bean2 = new TestBean(); ?? ??? ?bean2.setName("小紅"); ?? ??? ?bean2.setAge(28); ?? ??? ?bean2.setPass(false); ?? ??? ?bean2.setScore(38.50); ?? ??? ?bean2.setExamDate(DateUtil.date()); ?? ??? ?bean2.setSex("女"); ?? ??? ?List<Object> rows = CollUtil.newArrayList(bean1, bean2); ?? ??? ? ?? ??? ?ExcelWriter writer = ExcelUtil.getWriter("BeanMode.xlsx"); ?? ??? ?writer.merge(2, "信息表"); ?? ??? ?writer.write(rows, true); ?? ??? ?writer.close();
?? ?}
}
|