用的是poi的excel导出,用的时候先导入poi的jar包或者在maven里边写上poi的配置
1.接口:
public HSSFWorkbook siteExport();
2.接口实现:
public HSSFWorkbook siteExport() {
//创建HSSFWorkbook对象(excel的文档对象)
HSSFWorkbook wb = new HSSFWorkbook();
//建立新的sheet对象(excel的表单)
HSSFSheet sheet = wb.createSheet("站点信息");
//在sheet里创建第一行
HSSFRow row1 = sheet.createRow(0);
//设置宽度
sheet.setColumnWidth(0, 3766);
sheet.setColumnWidth(1, 3766);
sheet.setColumnWidth(2, 3766);
sheet.setColumnWidth(3, 3766);
sheet.setColumnWidth(4, 3766);
sheet.setColumnWidth(5, 3766);
sheet.setColumnWidth(6, 3766);
sheet.setColumnWidth(7, 3766);
sheet.setColumnWidth(8, 3766);
sheet.setColumnWidth(9, 3766);
sheet.setColumnWidth(10, 3766);
//设置行高
row1.setHeight((short) 440);
//创建单元格
HSSFCell cell1_1 = row1.createCell(0);
HSSFCell cell1_2 = row1.createCell(1);
HSSFCell cell1_3 = row1.createCell(2);
HSSFCell cell1_4 = row1.createCell(3);
HSSFCell cell1_5 = row1.createCell(4);
HSSFCell cell1_6 = row1.createCell(5);
HSSFCell cell1_7 = row1.createCell(6);
HSSFCell cell1_8 = row1.createCell(7);
HSSFCell cell1_9 = row1.createCell(8);
HSSFCell cell1_10 = row1.createCell(9);
HSSFCell cell1_11 = row1.createCell(10);
//样式
Font titleFont = wb.createFont();
titleFont.setBold(true);
titleFont.setFontHeightInPoints((short) 11);
HSSFCellStyle cellStyle = wb.createCellStyle(); //表格内容
cellStyle.setFont(titleFont);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
// 创建单元格并设置单元格内容
cell1_1.setCellValue("大洲");
cell1_2.setCellValue("国家");
cell1_3.setCellValue("城市");
cell1_4.setCellValue("站点代号");
cell1_5.setCellValue("站点名称");
cell1_6.setCellValue("站点地址");
cell1_7.setCellValue("站点类型");
cell1_8.setCellValue("经度");
cell1_9.setCellValue("纬度");
cell1_10.setCellValue("联系人");
cell1_11.setCellValue("联系电话");
cell1_1.setCellStyle(cellStyle);
cell1_2.setCellStyle(cellStyle);
cell1_3.setCellStyle(cellStyle);
cell1_4.setCellStyle(cellStyle);
cell1_5.setCellStyle(cellStyle);
cell1_6.setCellStyle(cellStyle);
cell1_7.setCellStyle(cellStyle);
cell1_8.setCellStyle(cellStyle);
cell1_9.setCellStyle(cellStyle);
cell1_10.setCellStyle(cellStyle);
cell1_11.setCellStyle(cellStyle);
// 在sheet里创建第二行
List<Site> list = this.getAllList(Site.class);
HSSFRow rown;
for (int i = 0; i < list.size(); i++) {
rown = sheet.createRow(i + 1);
//设置行高
rown.setHeight((short) 440);
HSSFCell celn_l = rown.createCell(0);
HSSFCell celn_2 = rown.createCell(1);
HSSFCell celn_3 = rown.createCell(2);
HSSFCell celn_4 = rown.createCell(3);
HSSFCell celn_5 = rown.createCell(4);
HSSFCell celn_6 = rown.createCell(5);
HSSFCell celn_7 = rown.createCell(6);
HSSFCell celn_8 = rown.createCell(7);
HSSFCell celn_9 = rown.createCell(8);
HSSFCell celn_l0 = rown.createCell(9);
HSSFCell celn_l1 = rown.createCell(10);
celn_l.setCellValue(list.get(i).getCity().getCountry().getContinent().getContinentName());
celn_2.setCellValue(list.get(i).getCountryName());
celn_3.setCellValue(list.get(i).getCity().getCityName());
celn_4.setCellValue(list.get(i).getSiteCode());
celn_5.setCellValue(list.get(i).getSiteName());
celn_6.setCellValue(list.get(i).getSiteAddress());
String siteTppe = String.valueOf(list.get(i).getSiteType());
if ("0".equals(siteTppe)) {
celn_7.setCellValue("遥控站");
}
if ("1".equals(siteTppe)) {
celn_7.setCellValue("采集点");
}
celn_8.setCellValue(list.get(i).getLongitude());
celn_9.setCellValue(list.get(i).getLatitude());
celn_l0.setCellValue(list.get(i).getContactsPerson());
celn_l1.setCellValue(list.get(i).getContactsPerPhone());
//样式
Font titleFont2 = wb.createFont();
titleFont2.setBold(false);
titleFont2.setFontHeightInPoints((short) 11);
HSSFCellStyle cellStyle2 = wb.createCellStyle(); //表格内容
cellStyle2.setFont(titleFont2);
cellStyle2.setAlignment(HorizontalAlignment.CENTER);
cellStyle2.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle2.setBorderBottom(BorderStyle.THIN);
cellStyle2.setBorderLeft(BorderStyle.THIN);
cellStyle2.setBorderRight(BorderStyle.THIN);
cellStyle2.setBorderTop(BorderStyle.THIN);
//设置样式
celn_l.setCellStyle(cellStyle2);
celn_2.setCellStyle(cellStyle2);
celn_3.setCellStyle(cellStyle2);
celn_4.setCellStyle(cellStyle2);
celn_5.setCellStyle(cellStyle2);
celn_6.setCellStyle(cellStyle2);
celn_7.setCellStyle(cellStyle2);
celn_8.setCellStyle(cellStyle2);
celn_9.setCellStyle(cellStyle2);
celn_l0.setCellStyle(cellStyle2);
celn_l1.setCellStyle(cellStyle2);
}
return wb;
}
3.页面实现:
直接用click点击事件取得他得id信息
4.后台实现:
//发送响应流方法
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(),"ISO8859-1");
} catch (UnsupportedEncodingException e) {
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();
}
}
@RequestMapping("/export")
public void basicInfoExport(HttpServletRequest request,HttpServletResponse response) throws IOException{
String id = request.getParameter("id");
if ("4".equals(id)) {
// 创建HSSFWorkbook对象(excel的文档对象)
HSSFWorkbook wb = service.siteExport();
// 输出Excel文件
OutputStream output = response.getOutputStream();
String fileName = "站点信息"+ ".xls";
this.setResponseHeader(response, fileName);
wb.write(output);
output.write(wb.getBytes());
output.close();
}
}
5.实现内容:
6.导出内容:
亲测可用