maven依赖:
<!--读取excel文件 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
代码(多线程执行):
public class CreateExcel implements Runnable {
// 数据
private List<List<String>> data;
// 表名
private String path;
// 列表名
private String[] coulmns;
public CreateExcel(List<List<String>> data, String path, String[] coulmns) {
this.data = data;
this.path = path;
this.coulmns = coulmns;
}
private HSSFWorkbook excel;
private HSSFSheet sheet;
@Override
public void run() {
File file = new File(path);
int begin=0;
//判断是追加还是新建
if (file.exists()) {
POIFSFileSystem ps;
try {
ps = new POIFSFileSystem(new FileInputStream(file));
excel = new HSSFWorkbook(ps);
sheet = excel.getSheetAt(0);
} catch (IOException e) {
System.out.println("读取excel异常");
}
// 找到目标下一行
HSSFRow row = sheet.getRow(0);
if ("".equals(row) || row == null) {
begin = 0;
} else {
begin = sheet.getLastRowNum();
begin = begin + 1;
}
} else {
excel = new HSSFWorkbook();
sheet = excel.createSheet("yours data");
sheet.setDefaultColumnWidth(30);
// 表头
HSSFRow row1 = sheet.createRow(0);
row1.setHeightInPoints(25);
// 表头样式
HSSFCellStyle cellStyle = excel.createCellStyle();
cellStyle.setWrapText(true);
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
HSSFFont headerfont = excel.createFont();
headerfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headerfont.setFontName("黑体");
headerfont.setFontHeightInPoints((short) 12);
cellStyle.setFont(headerfont);
// 表头数据
for (int i = 0; i < coulmns.length; i++) {
HSSFCell cell = row1.createCell(i);
cell.setCellValue(coulmns[i]);
cell.setCellStyle(cellStyle);
}
}
// 内容样式
HSSFCellStyle contentStyle = excel.createCellStyle();
contentStyle.setWrapText(true);// 设置自动换行
contentStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式
contentStyle.setBottomBorderColor(HSSFColor.BLACK.index);
contentStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
contentStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
contentStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
contentStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
// 填充内容
HSSFRow row = null;
System.out.println(data.get(0).size());
for (int j = 0; j < data.get(0).size(); j++) {
row = sheet.createRow((int) j + begin);
HSSFCell datacell = null;
for (int k = 0; k < data.size(); k++) {
datacell = row.createCell(k);
datacell.setCellValue(data.get(k).get(j));
datacell.setCellStyle(contentStyle);
}
}
// 导出数据
try {
System.out.println(path);
FileOutputStream fout = new FileOutputStream(file);
if (!file.exists()) {
file.createNewFile();
}
excel.write(fout);
System.out.println("导出成功");
} catch (Exception e) {
e.printStackTrace();
System.err.println("导出文件失败");
}
}
}