今天给大家分享的是JAVA如何导出EXCEL表格,因为最近有做这样一个功能
导出
首先把pom依赖导到项目中
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.6</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-examples</artifactId>
<version>3.9</version>
</dependency>
在写个方法
/**
* 导出
*/
void createExcel(OutputStream os);
@Override
public void createExcel(OutputStream os) {
// 按条件查询
List<CompanyAndWash> companyAndWashList = this.findByWashAll();
//创建一个工作簿
HSSFWorkbook book = new HSSFWorkbook();
//创建一个工作表并起名称
HSSFSheet sheet = book.createSheet("用户信息");
//创建一个行并写入表头
HSSFRow row = sheet.createRow(0);
// 定义好每一列的标题
String[] headerNames = {"编号", "名称", "部门名称", "数量", "租赁单价","租赁费"};
// 指定每一列的宽度
int[] columnWidths = {4000, 8000, 8000, 8000,8000,8000};
//创建一个单元格
HSSFCell cell = null;
for (int i = 0; i < headerNames.length; i++) {
// 创建一个单元格
cell = row.createCell(i);
//设置单元格的值
cell.setCellValue(headerNames[i]);
// 设置单元格行宽
sheet.setColumnWidth(i, columnWidths[i]);
}
// 写入内容
int i = 1;
for (CompanyAndWash companyAndWash : companyAndWashList) {
row = sheet.createRow(i);
//必须按照hderarNames的顺序来
row.createCell(0).setCellValue(companyAndWash.getId());//编号
row.createCell(1).setCellValue(companyAndWash.getCompanyName()==null?"":companyAndWash.getCompanyName());
row.createCell(2).setCellValue(companyAndWash.getCompanyDeptName()==null?"":companyAndWash.getCompanyDeptName());
row.createCell(3).setCellValue(companyAndWash.getBillChoose()==null?"":companyAndWash.getBillChoose());
row.createCell(4).setCellValue(companyAndWash.getLeasePrice()==null?"":companyAndWash.getLeasePrice().toString());
row.createCell(5).setCellValue(companyAndWash.getCount()==null?"":companyAndWash.getCount().toString());
i++;
}
try {
// 写入到输出流中
book.write(os);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 关闭工作簿
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
controller
@RequestMapping(value = "/createExcel")
public void createExcel(HttpServletResponse response) throws IOException {
System.out.println("!11111");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
// 设置文件名称
String filename = "租赁账单" + format.format(new Date()) + ".xls";
// 设置输出流表头信息
/* response.setHeader("Content-Disposition", "attachment;filename=" +
new String(filename.getBytes(), "UTF-8"));//中文名称进行转码*/
response.setHeader("Content-Disposition","attachment;filename=DailyIncome");
response.setContentType("application/octet-stream;charset=utf-8");
//调用导出业务
this.setResponseHeader(response,filename);
mealPriceSetService.createExcel(response.getOutputStream() );
}
//发送响应流方法
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(),"ISO8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
写到这里就可以实现导出Execl表格了