为了能够用java实现数据自动填入Excel,学习了一些相关的知识内容,这里记录一下。
一、使用poi将数据写入Excel
对于2007+版本的Excel,使用XSSF进行操作,需要导入两个jar包:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.1</version>
</dependency>
使用XSSF将数据写入Excel,具体操作流程和相关方法如下:
//创建Workbook,相当于Excel对象。文件输入流指定具体要操作的Excel文件,这样就可以读取文件内容了
File file=new File("D://test.xlsx");
XSSFWorkbook workbook=new XSSFWorkbook(new FileInputStream(file));
//获取第一个sheet表,可以根据sheet表的位置查找,也可以根据sheet名查找
XSSFSheet sheet=workbook.getSheetAt(0);
//XSSFSheet sheet=workbook.getSheet("sheetname");
//设置单元格的字体格式
XSSFFont font=workbook.createFont();
font.setFontName("宋体");
font.setFontHeight(11);
//创建单元格风格
XSSFCellStyle style=workbook.createCellStyle();
//将字体格式放入style中
style.setFont(font);
style.setAlignment(HorizontalAlignment.CENTER);//水平居中
style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
style.setBorderBottom(BorderStyle.THIN);//细边框
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setFillForegroundColor(IndexedColors.BLUE.getIndex());//背景颜色
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//在文件末尾循环创建10行
int index=sheet.getLastRowNum()+1;
for(int i=0;i<10;i++){
XSSFRow row=sheet.createRow(index+i);//创建一行
row.setHeight((short)720);//行高
//循环创建并设置一行中的10个单元格
for(int j=0;j<10;j++){
XSSFCell cell=row.createCell(j);
cell.setCellValue("单元格内容");
//将之前设置的风格加入单元格
cell.setCellStyle(style);
}
}
//可以接着读取第二个sheet表,按照上面方法设置内容
//最后,将上面的内容写入文件
workbook.write(new FileOutputStream(file));
//如果想要在表格中插入一行
//sheet.shiftRows(index,sheet.getLastRowNum(),1,true,false);
//这样就可以在index所指的位置插入1行
整个操作流程就是:创建workbook(相当于Excel文件的对象)→为workbook指定具体Excel文件的输入流→查找sheet表→创建行和列→设置内容和格式→创建文件输出流并write。
代码中之所以循环设置单元格的风格,而不是直接调用row.setRowStyle(style)设置一行的风格,是因为这个方法不起作用(不知道是自己不会用,还是poi本身有问题)。
对于2007以下版本的Excel,使用HSSF类进行操作,操作的流程和方法跟上面的大同小异,就不多说了。
二、使用easyExcel将数据写入表格
导入的jar包:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.4</version>
</dependency>
自己创建的类,用于设置表头,每个对象相当于一行
public class Head{
@ExcelProperty(value="第一列",index = 0)//value是表头名,index是指这一列所在的位置
private int col1;//给col1赋值,就是单元格的值
@ExcelProperty(value="第二列",index = 1)
private int col2;
@ExcelProperty(value="第三列",index = 2)
private int col3;
public int getCol1() {
return col1;
}
public void setCol1(int col1) {
this.col1 = col1;
}
public int getCol2() {
return col2;
}
public void setCol2(int col2) {
this.col2 = col2;
}
public int getCol3() {
return col3;
}
public void setCol3(int col3) {
this.col3 = col3;
}
}
接下来是具体操作:
ExcelWriter writer= EasyExcel.write("D://test.xlsx",Head.class).build();//创建表格的writer
WriteSheet sheet=EasyExcel.writerSheet("测试").build();//创建sheet表的writer
List<Head> data=new ArrayList<Head>();//list用于存放要写入表格的数据
for(int i=0;i<3;i++){
Head head=new Head();//自己创建的表头对象,用于存放一行的数据
head.setCol1(i);//设置一行中每一格的值
head.setCol2(i);
head.setCol3(i);
data.add(head);//将一行添加进list
}
writer.write(data,sheet);//将全部数据写入sheet表,可以多次write
writer.finish();
操作流程:为表格创建实体对象,用于设置表格的表头和存放数据→创建list<实体对象>,存放表格数据→创建ExcelWriter→创建WriteSheet→使用ExcelWriter将list写入表格→结束。