模板类: package com.emanual.manage.factory.exportpaper; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.Cell; import org.springframework.web.servlet.view.document.AbstractExcelView; import com.emanual.manage.factory.domain.FactoryDevice; import com.emanual.manage.pub.util.ActionMapping; public class InstorageExport extends AbstractExcelView{ private List<FactoryDevice> devices; private String sDate; private String endDate; public InstorageExport(List<FactoryDevice> devices,String sDate,String endDate) { this.devices = devices; this.sDate = sDate; this.endDate = endDate; } @Override protected void buildExcelDocument(Map<String, Object> arg0, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception { HSSFSheet sheet = workbook.createSheet("入库详情"); sheet.setDefaultColumnWidth((short) 15); HSSFCellStyle style = workbook.createCellStyle(); HSSFFont font = workbook.createFont(); font.setFontHeight((short)300); font.setFontName("宋体"); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示 style.setFont(font); // 设置这些样式 style.setFillForegroundColor(HSSFColor.WHITE.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 生成并设置另一个样式 HSSFCellStyle style2 = workbook.createCellStyle(); style2.setFillForegroundColor(HSSFColor.WHITE.index); style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); style2.setBorderLeft(HSSFCellStyle.BORDER_THIN); style2.setBorderRight(HSSFCellStyle.BORDER_THIN); style2.setBorderTop(HSSFCellStyle.BORDER_THIN); style2.setAlignment(HSSFCellStyle.ALIGN_CENTER); style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 生成另一个字体 HSSFFont font2 = workbook.createFont(); font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); font2.setFontHeight((short)250); // 把字体应用到当前的样式 style2.setFont(font2); HSSFRow timeRow = sheet.createRow(0); Cell cell0 = timeRow.createCell(0); cell0.setCellStyle(style2); cell0.setCellValue("查询起始时间"); Cell cell1 = timeRow.createCell(1); cell1.setCellStyle(style2); cell1.setCellValue(sDate); Cell cell2 = timeRow.createCell(2); Cell cell3 = timeRow.createCell(3); cell3.setCellStyle(style2); cell3.setCellValue("查询结束时间"); Cell cell4 = timeRow.createCell(4); cell4.setCellStyle(style2); cell4.setCellValue(endDate); String titles[] = {"入库单号","设备编号","设备名称", "设备序列号", "设备型号", "设备状态","箱次","设备使用部门","手机号","原始成本","入账日期","仓库管理员","经办人","存放地点","备注1","备注2"}; HSSFRow titleRow = sheet.createRow(1); titleRow.setHeight((short)370); int index = 0; /** * 写入表头 */ for (String string : titles) { HSSFRichTextString text = new HSSFRichTextString(string); Cell cell=titleRow.createCell(index); cell.setCellStyle(style); cell.setCellValue(text); index++; } List<String[]>listData=new ArrayList<String[]>(); for (FactoryDevice device : devices) { String str[]={device.getInOutNumber(),device.getDeviceNumber(),device.getDeviceName(),device.getDeviceIMEI(),device.getModel(),ActionMapping.getStatusName(device.getStatusNumber()), device.getBoxTimes(),device.getUseBranchName(),device.getMobileNumber(),device.getCost(),device.getInDate(), device.getStorageMg(),device.getAgent(),device.getAddressName(),device.getRemark1(),device.getRemark2()}; listData.add(str); } int i=2; for (String[] strings : listData) { HSSFRow dataRow=sheet.createRow(i++); dataRow.setHeight((short)300); int cellIndex=0; for (String string : strings) { HSSFRichTextString text = new HSSFRichTextString(string); Cell cell=dataRow.createCell(cellIndex); cell.setCellStyle(style2); cell.setCellValue(text); cellIndex++; } } String filename = "";// 设置下载时客户端Excel的名称 filename="入库详情.xls"; response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename=" + filename); OutputStream ouputStream = response.getOutputStream(); workbook.write(ouputStream); ouputStream.flush(); ouputStream.close(); } } action: @RequestMapping("/batchImportModel") public ModelAndView batchImportModel(ModelMap model, HttpServletRequest request) throws EmanualException { try{ BatchInstallExport batchInstallExport = new BatchInstallExport(); return new ModelAndView(batchInstallExport, model); }catch(Exception e){ logger.error("详情错误"+e); } } window.location.href = "请求地址就行" 所需要的jar: http://item.taobao.com/item.htm?id=38986813605