在很多时候我们都会和Excel打交道,会用Excel的人好像又多了一些技能,可以做数据统计啊,拿出来报表给老板看的时候也有逼格的感觉。
不过我是开发,这里只说下如果用程序去处理Excel,常见的导入导出Excel,给某些标题加上批注等。这里使用Java语言开发。
案例
- 首先添加依赖关系
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.0.1</version>
</dependency>
- 在代码中使用easypoi,官网也给了一些自己的使用demo,但是不够全,我们给一个比较全的demo。官网demo地址:https://github.com/lemur-open/easypoi/blob/master/basedemo.md
- 新家一个POJO对象,用于映射Excel表格的内容,标题字段什么的
import cn.afterturn.easypoi.excel.annotation.Excel;
public class Person {
@Excel(name = "姓名")
private String name;
@Excel(name = "年龄")
private Integer age;
@Excel(name = "住址",width = 50)
private String address;
@Excel(name = "电话号码")
private String phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
- 写导出数据的代码
public static void main(String[] args) throws IOException {
List<Person> list = new ArrayList<>(10);
for (int i = 0; i < 10; i++) {
Person person = new Person();
person.setName("name" + i);
person.setAge(i + 18);
person.setAddress("杭州 余杭 西湖" + i);
person.setPhone("139512100" + i);
list.add(person);
}
ExportParams exportParams = new ExportParams(
"用户统计", null, "班级1");
exportParams.setType(ExcelType.XSSF);
Workbook workbook = new XSSFWorkbook();
ExcelExportServer excelExportServer = new ExcelExportServer();
ExcelExportStylerDefaultImpl stylerDefault = new ExcelExportStylerDefaultImpl(workbook);
excelExportServer.setExcelExportStyler(stylerDefault);
excelExportServer.createSheet(workbook, exportParams, Person.class, list);
// 给想要的单元格添加批注信息
Drawing patriarch = workbook.getSheet("班级1").createDrawingPatriarch();
for (int i = 0; i < 4; i++) {
Cell cell = workbook.getSheet("班级1").getRow(10).getCell(i);
XSSFComment comment = (XSSFComment)patriarch.createCellComment(
new XSSFClientAnchor(0, 0, 0, 0, (short)3, 3, (short)5, 6));
//输入批注信息
comment.setString(new XSSFRichTextString("插件批注" + i));
//将批注添加到单元格对象中
cell.setCellComment(comment);
}
FileOutputStream outputStream = new FileOutputStream(
"C:\\Users\\ah\\Desktop\\test.xlsx");
try {
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
outputStream.close();
}
注:如果不想要批注信息,就把那个cell的comment去掉
- 效果

1558665120913.png
当然还有很多更加灵活的用法了,比如说导入excel表格的数据,然后用程序做一些处理都是可以的。
额外
设置字体颜色,大小
CellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
Font font = workbook.createFont();
font.setBold(true);
font.setColor(Font.COLOR_RED);
font.setFontHeightInPoints((short)12);
style.setFont(font);
cell.setCellStyle(style);