转自:https://blog.csdn.net/gaopeng096/article/details/83055702
所需pom
<!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl -->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0.0</version>
</dependency>
代码
public void writeExcel(){
//开始写入excel,创建模型文件头
String[] titleA = {"id","Cgonghao","Cname"};
//创建Excel文件,B库CD表文件
File fileA = new File("e:/TestFile.xls");
if(fileA.exists()){
//如果文件存在就删除
fileA.delete();
}
try {
fileA.createNewFile();
//创建工作簿
WritableWorkbook workbookA = Workbook.createWorkbook(fileA);
//创建sheet
WritableSheet sheetA = workbookA.createSheet("sheet1", 0);
Label labelA = null;
//设置列名
for (int i = 0; i < titleA.length; i++) {
labelA = new Label(i,0,titleA[i]);
sheetA.addCell(labelA);
}
//获取数据源
for (int i = 1; i < 10; i++) {
labelA = new Label(0,i,i+" ");
sheetA.addCell(labelA);
labelA = new Label(1,i,"20180101" + i);
sheetA.addCell(labelA);
labelA = new Label(2,i,"开发00"+i);
sheetA.addCell(labelA);
}
workbookA.write(); //写入数据
workbookA.close(); //关闭连接
logger.info("成功写入文件,请前往E盘查看文件!");
} catch (Exception e) {
logger.info("文件写入失败,报异常...");
}
}