java中导出Excel表
需要的jar包:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
实体类:
public class student{
@Column
private String name;//姓名
@Column
private String gender;//性别
@Column
private int age;//年龄
@Column
private String date;//出生日期
@Column
private String familyAddress;//家庭地址
}
需要导出的字段:
public class studentQueryForm {
private String name;//姓名
private String gender;//性别
private int age;//年龄
}
业务层接口类:
/**
* 导出表格
*/
HSSFWorkbook exportBlock(List<student> list);
业务层实现类:
/**
* 导出表格
*/
@Override
public HSSFWorkbook exportBlock(List<student> list) {
HSSFWorkbook wb = new HSSFWorkbook();//创建HSSFWorkbook对象
HSSFSheet sheet = wb.createSheet("基本安置数据");//创建HSSFSheet对象
sheet.setColumnWidth(0, 30 * 256);
sheet.setColumnWidth(1, 20 * 256);
sheet.setColumnWidth(2, 20 * 256);
HSSFCellStyle cellStyle=wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 设置单元格的横向和纵向对齐方式
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFFont fontStyle=wb.createFont();
fontStyle.setFontName("宋体");//设置字体样式
fontStyle.setFontHeightInPoints((short)11);//设置字体高度
cellStyle.setFont(fontStyle);// 将字体对象赋值给单元格样式对象
HSSFRow row = sheet.createRow(0);//创建HSSFRow对象
row.setHeightInPoints(30);
HSSFCell cell=row.createCell(0);//创建HSSFCell对象
cell.setCellStyle(cellStyle);
cell.setCellValue("姓名");
cell = row.createCell(1);
cell.setCellStyle(cellStyle);
cell.setCellValue("性别");
cell = row.createCell(2);
cell.setCellStyle(cellStyle);
cell.setCellValue("年龄");
for(int i = 0;i<list.size();i++){
Info orders = list.get(i);
row = sheet.createRow(i+1);//创建HSSFRow对象
row.setHeightInPoints(30);
cell=row.createCell(0);//创建HSSFCell对象
cell.setCellStyle(cellStyle);
cell.setCellValue(orders.getName());//姓名
cell = row.createCell(1);
cell.setCellStyle(cellStyle);
cell.setCellValue(orders.getGender());//性别
cell = row.createCell(2);
cell.setCellStyle(cellStyle);
cell.setCellValue(orders.getAge());//年龄
}
return wb;
}
控制层:
/**
* 导出到Excel信息表格
*/
@RequestMapping("/studentExcle")
@ResponseBody
public String studentExcle(studentQueryForm form,HttpServletResponse response){
String result;
try {
List<student> ordersList = studentService.getStudent(form);
HSSFWorkbook workbook = studentService.exportBlock(ordersList);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
Date date = new Date();
String filedisplay = "学生基本数据"+sdf.format(date)+".xls";
filedisplay = new String(filedisplay.getBytes("gbk"), "iso8859-1");
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="+filedisplay);
OutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
result = "导出成功";
} catch (Exception e) {
result = "发生异常";
e.printStackTrace();
}
return result;
}
前端页面
<button type="button" class="btn waves-effect waves-light btn-info" id="export">导出</button>
//导出安置数据
$("#export").on("click",function(){
var name=$("#name").val();
var gender=$("#gender").val();
var age=$("#age").val();
window.location.href='请求路径/exportExcle?name='+name+'&gender='+gender+'&age='+age;
});