项目中导出Excel

1、需要的jar包(放在lib下)

poi-3.9.jar

poi-ooxml-3.9.jar

poi-ooxml-schemas-3.9.jar

2、创建下载

~~~

//创建HSSFWorkbook对象:其实它就最终生成Excel文件对应的对象

HSSFWorkbook workbook = new HSSFWorkbook();

//通过HSSFWorkbook对象创建HSSFSheet对象

HSSFSheet createSheet = workbook.createSheet("sheet名称");

HSSFDataFormat createDataFormat = workbook.createDataFormat();

HSSFCellStyle dateHSSFCellStyle = workbook.createCellStyle();

dateHSSFCellStyle.setDataFormat(createDataFormat.getFormat("yyyy年MM月dd日 HH:mm:ss"));

HSSFCellStyle floatHSSFCellStyle = workbook.createCellStyle();

floatHSSFCellStyle.setDataFormat(createDataFormat.getFormat("#,#.000"));

HSSFCellStyle richTextHSSFCellStyle = workbook.createCellStyle();

richTextHSSFCellStyle.setWrapText(true); //自动折行

HSSFRow row = createSheet.createRow(0);//自定义第一行内容

row.createCell(0).setCellValue("姓名");

row.createCell(1).setCellValue("身份证号码");

row.createCell(2).setCellValue("联系电话");

//第二行内容(批量数据可以自己封装list等,放入数据即可)

HSSFRow row1 = createSheet.createRow(1);

row1.createCell(0).setCellValue("内容1");

row1.createCell(1).setCellValue("内容2");

row1.createCell(2).setCellValue("内容3");

OutputStream output = response.getOutputStream();

response.reset();

String fileName = "excel名称";

response.setContentType("application/vnd.ms-excle;charset=utf-8");

response.setHeader("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes(),"iso-8859-1")+".xls");

workbook.write(output);

output.close();

~~~

3、jar包下载地址:

https://search.maven.org/search?q=g:org.apache.poi%20AND%20a:poi


搜索jar包下载

你可能感兴趣的:(项目中导出Excel)