页面导出Excel文件总结
DELIMITER $$ USE `aop`$$ DROP FUNCTION IF EXISTS `rownums`$$ CREATE DEFINER=`root`@`%` FUNCTION `rownums`() RETURNS INT(11) BEGIN SET @rownum = @rownum + 1; RETURN @rownum; END$$ DELIMITER ;
DELIMITER $$ USE `aop`$$ DROP FUNCTION IF EXISTS `rownums_reset`$$ CREATE DEFINER=`root`@`%` FUNCTION `rownums_reset`() RETURNS INT(11) BEGIN SET @rownum = 0; RETURN 1; END$$ DELIMITER ;利用两个查询行号,select rownums(),sa from t where rownums_reset() = 1;
SELECT (@rowNum := @rowNum + 1) AS rownum FROM t_stu_info, (SELECT (@rowNum := 0)) b ORDER BY t_stu_info.age DESC
for(int i = 0 ; i<data.size();i++) { Map<String,Object> map=(Map<String, Object>) data.get(i); HSSFRow datarow = sheet.createRow(i+1); HSSFCell cell0 = datarow.createCell(0); HSSFRichTextString t0 = new HSSFRichTextString((i+1)+""); cell0.setCellValue(t0); }
A B C D A B1 F G A H I L B C D T L B R U E T B W Q S I C E K C V C X T O P
A B C D A B1 F G A H I L
B C D T L B R U E T B W Q S I
C E K C V C X T O P
List<List<Map<String,Object>>> itemList = new ArrayList<List<Map<String,Object>>>(); List<Object> data=msg.getData(); String aa = “-1”; for(int i=0;i<data.size();i++) { Map<String,Object> map=(Map<String, Object>) data.get(i); String a= map.get("A").toString(); Map<String,Object> item = new HashMap<String, Object>(); item.put("A", map.get("A")); item.put("B", map.get("B")); item.put("C", map.get("C")); item.put("D", map.get("D")); item.put("E", map.get("E")); if(!aa.equals(a)) { aa=a; List<Map<String,Object>> t=new ArrayList<Map<String, Object>>(); t.add(item); itemList.add(t); } else { if(itemList.size() >= 1) { itemList.get(itemList.size()-1).add(item); } } } for(int j=0;j<itemList.size();j++) { for(int i = 0 ; i<itemList.get(j).size();i++) { Map<String,Object> map=(Map<String, Object>) itemList.get(j).get(i); } }
HSSFWorkbook workbook = new HSSFWorkbook(); for(int j=0;j<itemList.size();j++) { HSSFSheet sheet = workbook.createSheet("清单"+j); sheet.setDefaultColumnWidth((short) 15); } FileOutputStream stream = new FileOutputStream("d:/one.xls"); workbook.write(stream);根据查询数据的数目来分sheet,应为之前是通过船代编码分组成多个list集合
HSSFRow rowHead = sheet.createRow(0); // 设置行高 rowHead.setHeight((short) 400); // 创建第一列 HSSFCell cellHead = rowHead.createCell(0); cellHead.setCellValue(new HSSFRichTextString("清单")); cellHead.setCellStyle(headStyle); CellRangeAddress range = new CellRangeAddress(0, 0, 0, 11); sheet.addMergedRegion(range); HSSFRow rowTitle = sheet.createRow(1); rowTitle.setHeight((short) 400); HSSFCell cellTitle = rowTitle.createCell(0); cellTitle.setCellValue(new HSSFRichTextString(str+":")); cellTitle.setCellStyle(headTitle); range = new CellRangeAddress(1, 1, 0, 11); sheet.addMergedRegion(range); // 产生表格标题行 HSSFRow row = sheet.createRow(1); HSSFCell cell = row.createCell(0); HSSFRichTextString text = new HSSFRichTextString("序号"); cell.setCellValue(text); cell.setCellStyle(normalTitle);由于标题创建行和表头创建行号一致,HSSFRow rowTitle = sheet.createRow(1); 重复设置第二行(从0开始),导致表头无法出现,这也浪费了很多时间
HSSFFont tabFont = workbook.createFont(); tabFont.setFontName("微软雅黑"); tabFont.setFontHeightInPoints((short)10); HSSFCellStyle tabTitle = workbook.createCellStyle(); tabTitle.setFont(tabFont); tabTitle.setAlignment(HSSFCellStyle.ALIGN_LEFT); tabTitle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); tabTitle.setBorderRight((short) 1); tabTitle.setBorderBottom((short) 1); tabTitle.setBorderLeft((short) 1); tabTitle.setBorderTop((short) 1);
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/you23hai45/article/details/47110309