Java导出Excel
2016-02-18 17:20
471 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zjx_Tenderness/article/details/50669709
model
private String name; private String conName; private String serialNo; private float amount; private Date createDate; private Date date; private String creator;
ExportModel
private SimpleDateFormat ymdDateFormat=new SimpleDateFormat("yyyy-MM-dd"); private NumberFormat nf=new DecimalFormat("0.00#"); private HSSFWorkbook workbook; private HSSFSheet sheet; private HSSFCellStyle titleStyle; private HSSFFont titleFont; private HSSFCellStyle contententStyle; private HSSFFont contententFont; private HSSFCellStyle headerStyle; private HSSFFont headerFont; public ModelExport(){ workbook = new HSSFWorkbook();// 声明一个工作薄 titleStyle = workbook.createCellStyle(); titleFont = workbook.createFont();// 生成表头字体 contententStyle = workbook.createCellStyle(); contententFont = workbook.createFont(); headerStyle = workbook.createCellStyle(); headerFont = workbook.createFont(); // 设置标题样式 headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); headerFont.setColor(HSSFColor.BLACK.index); headerFont.setFontHeightInPoints((short) 14); headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 把字体应用到当前的样式 headerStyle.setFont(headerFont); titleStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); titleFont.setColor(HSSFColor.BLACK.index); titleFont.setFontHeightInPoints((short) 12); titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); titleStyle.setFont(titleFont); contententStyle.setFillForegroundColor(HSSFColor.WHITE.index); contententStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); contententStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); contententStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); contententFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); contententStyle.setFont(contententFont); } public void exportExcel(String title, Model model, OutputStream out) throws IOException { int rowIndex=0; // 生成一个表格 sheet = workbook.createSheet(title); // 设置表格默认列宽度为15个字节 sheet.setDefaultColumnWidth(25); //标题 HSSFRow titleRow=sheet.createRow(rowIndex); HSSFCell titleCell = titleRow.createCell(0); titleCell.setCellValue(title); titleCell.setCellStyle(headerStyle); sheet.addMergedRegion(new CellRangeAddress(rowIndex,rowIndex,0,3)); rowIndex++;//增加一个空行 rowIndex++; HSSFRow row2 = sheet.createRow(rowIndex); HSSFCell companyTitle=row2.createCell(0); companyTitle.setCellValue("积分而非:"); companyTitle.setCellStyle(titleStyle); HSSFCell company=row2.createCell(1); company.setCellValue(model.getName()); sheet.addMergedRegion(new CellRangeAddress(rowIndex,rowIndex,1,3)); rowIndex++; HSSFRow row3 = sheet.createRow(rowIndex); HSSFCell htNoTitle=row3.createCell(0); htNoTitle.setCellValue("给开理发店:"); htNoTitle.setCellStyle(titleStyle); HSSFCell htNo=row3.createCell(1); htNo.setCellValue(vo.getSerialNo()); HSSFCell htNameTitle=row3.createCell(2); htNameTitle.setCellValue("疾病和:"); htNameTitle.setCellStyle(titleStyle); HSSFCell htName=row3.createCell(3); htName.setCellValue(vo.getConName()); rowIndex++; HSSFRow row4 = sheet.createRow(rowIndex); HSSFCell htAmountTitle=row4.createCell(0); htAmountTitle.setCellValue("逛了个:"); htAmountTitle.setCellStyle(titleStyle); HSSFCell htAmount=row4.createCell(1); htAmount.setCellValue(nf.format(vo.getAmount())); HSSFCell signDateTitle=row4.createCell(2); signDateTitle.setCellValue("基本函数:"); signDateTitle.setCellStyle(titleStyle); HSSFCell signDate=row4.createCell(3); signDate.setCellValue(ymdDateFormat.format(vo.getDate())); rowIndex++;//插入一个空行 rowIndex++; HSSFRow row5 = sheet.createRow(rowIndex); HSSFCell prodTitle=row5.createCell(0); prodTitle.setCellValue("我看个够"); prodTitle.setCellStyle(titleStyle); HSSFCell cardTitle=row5.createCell(1); cardTitle.setCellValue("讲个故事"); cardTitle.setCellStyle(titleStyle); sheet.addMergedRegion(new CellRangeAddress(rowIndex,rowIndex,1,3)); Set<Entry<String, List<String>>> prodCardEntries=model.getProdCardsMap().entrySet(); for (Entry<String, List<String>> entry : prodCardEntries) { rowIndex++; String prod=entry.getKey(); List<String> cardNoList=entry.getValue(); int subCount=cardNoList.size(); for(int subRow=0;subRow<cardNoList.size();subRow++,rowIndex++){ HSSFRow cardRow=sheet.createRow(rowIndex); if(subRow==0){//创建列 sheet.addMergedRegion(new CellRangeAddress(rowIndex,rowIndex+subCount-1,0,0)); HSSFCell prodCell=cardRow.createCell(0); prodCell.setCellValue(prod); prodCell.setCellStyle(contententStyle); } HSSFCell cardCell=cardRow.createCell(1); cardCell.setCellValue(cardNoList.get(subRow)); sheet.addMergedRegion(new CellRangeAddress(rowIndex,rowIndex,1,3)); } } workbook.write(out); }
Controller
@RequestMapping(params = "action=exportXls",method=RequestMethod.GET) public void exportXls(HttpServletRequest request,HttpServletResponse response,Long id) { Model model=ModelService.getModel(id); if(model==null)return; String filename=model.getSerialNo(); response.setContentType("application/vnd.ms-excel"); response.addHeader("Content-Disposition", "attachment;filename="+filename+".xls"); ExportModel export=new ExportModel(); try { OutputStream out=response.getOutputStream(); export.exportExcel("过好几个就会更好", model, out); } catch (IOException e) { logger.error("导出过好几个就会更好失败", e); } }
相关文章推荐
- [JAVA]Java导出Excel数据的一种方法(完美实现,包含表格线)
- java 用poi包导出excel
- JAVA使用POI根据模版导出EXCEL
- java导出excel,边查询边导出
- Java中导入、导出Excel
- Java web Excel导入和导出
- JAVA POI导出excel
- java实现百万级数据导出excel
- java实现导出excel、word、 pdf
- POI(JAVA Excel导出功能)
- Java 导出Excel方法二
- Java程序员从笨鸟到菜鸟之(一百零五)java操作office和pdf文件(三)利用jxl实现数据导出excel报表以及与POI的区别
- java 导出Excel
- java导入导出excel操作(jxl)
- java利用poi对excel进行操作可导入导出
- Java学习之Apache poi导出Excel
- jxl导出excel异常:java.lang.ArrayIndexOutOfBoundsException: 31
- Java操作Excel(二)将数据库中的数据导出到Excel中
- java——导出数据到Excel
- java jxl excel导出报错 Warning: Cannot merge cells - top left and bottom right incorrectly specified