导入jar包 poi-3.7.jar
public static void main(String[] args) {
try {
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook workbook = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的工作簿,并设置工作簿名称
HSSFSheet sheet = workbook.createSheet("新建表111");
// 第三步,在建立的工作簿中创建一行
HSSFRow row = sheet.createRow(0);
// 第四步,创建该行中的对应列
HSSFCell cell00 = row.createCell(0);
// 第五步,向该行该列中设置内容
cell00.setCellValue("列表0");
HSSFCell cell01 = row.createCell(1);
cell01.setCellValue("列表1");
HSSFCell cell02 = row.createCell(2);
cell02.setCellValue("列表2");
// 第六步,定义输出的位置
FileOutputStream out = new FileOutputStream("C:\\Users\\dj\\Desktop\\Java编程思想包\\exportPdf\\workbook01.xls");
//如果是浏览器通过request请求需要在浏览器中输出则使用下面方式
//OutputStream out = response.getOutputStream();
workbook.write(out);
out.close();
System.out.println("导出excel成功~");
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
显示结果为 这就是一行的表头列
HSSFRow row = sheet.createRow(0);
// 第四步,创建该行中的对应列
HSSFCell cell00 = row.createCell(0);
// 第五步,向该行该列中设置内容
cell00.setCellValue("列表0");
HSSFCell cell01 = row.createCell(1);
cell01.setCellValue("列表1");
HSSFCell cell02 = row.createCell(2);
cell02.setCellValue("列表2");
HSSFRow row1 = sheet.createRow(1);
// 第四步,创建该行中的对应列
HSSFCell cell10 = row1.createCell(0);
// 第五步,向该行该列中设置内容
cell10.setCellValue("测试值1");
HSSFCell cell11 = row1.createCell(1);
cell11.setCellValue("测试值2");
HSSFCell cell12 = row1.createCell(2);
cell12.setCellValue("测试值3");
結果爲