XSSFWorkbook写入Excel存入指定路径,XSSFWorkbook写入只支持2007以后版本
2007以前版本请将XSSFWorkbook替换为HSSFWorkbook
//极简写入excel
public static void main(String[] args) {
try {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("当前sheet页的名");
XSSFRow row;
XSSFCell cell;
//创建第一行标题行
row = sheet.createRow(0);
cell = row.createCell(0);//第i行第0列序号
cell.setCellValue("列0");
cell = row.createCell(1);//第i行第1列序号
cell.setCellValue("列1");
cell = row.createCell(2);//第i行第2列序号
cell.setCellValue("列2");
cell = row.createCell(3);//第i行第3列序号
cell.setCellValue("列3");
cell = row.createCell(4);//第i行第4列序号
cell.setCellValue("列4");
for(int i =1;i<11;i++){
//第i行
row = sheet.createRow(i);
for(int j =0;j<5;j++){
cell = row.createCell(j);//第i行第j列
cell.setCellValue("内容"+i+"-"+j);
}
}
FileOutputStream out = new FileOutputStream(new File("D:\\"+new Date().getTime()+".xlsx"));
workbook.write(out);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}