1.引用
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
2.使用
public static void main(String[] args) throws IOException {
String []title = {"订单ID","流水号"};
//创建HSSF工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
//创建一个Sheet页
HSSFSheet sheet = workbook.createSheet();
//创建第一行(一般是表头)
HSSFRow row0 = sheet.createRow(0);
//创建列
HSSFCell cell = null;
//设置表头
for (int i = 0; i <title.length ; i++) {
cell=row0.createCell(i);
cell.setCellValue(title[i]);
}
//填充20行数据
for (int i = 1; i < 20; i++) {
HSSFRow row =sheet.createRow(i);
HSSFCell cell1 = row.createCell(0);
cell1.setCellValue(RandomStringUtils.randomNumeric(18));
HSSFCell cell2 = row.createCell(1);
cell2.setCellValue(RandomStringUtils.randomNumeric(12));
}
//保存到本地
File file = new File("D:/test.xls");
FileOutputStream outputStream = new FileOutputStream(file);
//将Excel写入输出流中
workbook.write(outputStream);
outputStream.flush();
}
参考:
Java中使用POI操作Excel
java实现Excel数据导出
Java操作Excel之Poi基本操作
Java POI 操作Excel(读取/写入)