基于Spring3 MVC实现批量导出数据成Excel文件!
2016-04-09 15:44
746 查看
在jsp中:
<button onclick="download()" class="btn btn-warning " type="button">批量导出Excel <i class="fa fa-download"></i></button>
<script type="text/javascript"> //导出信息函数 function download(){ if(confirm("是否要导出数据?")){ var url = "${path}/master/child/downloadChild"; $.post(url,function(data){ alert(data.message); }); } } </script>MasterChildController.java:
@RequestMapping(value = "downloadChild", method = RequestMethod.POST) @ResponseBody public Map<String, Object> downloadChild() throws Exception { Date date = new Date(); DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS"); String fileName = sdf.format(date); //得到桌面路径 File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory(); String desktopPath = desktopDir.getAbsolutePath(); String desktopDirPath = desktopPath.replace("\\","\\\\"); String filePath = desktopDirPath + "\\\\" +fileName + ".xls"; //System.out.println(filePath); String[] titles = {"年级班级名称","幼儿学号","姓名","性别","年龄","出生日期","添加幼儿时间","删除标志","父亲姓名","父亲电话","父亲工作单位","母亲姓名","母亲电话","母亲工作单位","家庭住址","备注"}; Map<String, Object> params = new HashMap<String, Object>(); List<Child> listsChild = childService.queryAll(params); Grades grades = new Grades(); SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); List<Map<Integer, String>> lists = new ArrayList<Map<Integer,String>>(); for (int i = 0; i < listsChild.size(); i++) { Child child = listsChild.get(i); Map<Integer, String> paramsLists = new HashMap<Integer, String>(); //通过child.getGradesId()来查询班级名称 grades = gradesService.queryBean(child.getGradesId()); paramsLists.put(0, grades.getGradesName()); paramsLists.put(1, child.getChildNo()); paramsLists.put(2, child.getChildName()); paramsLists.put(3, child.getSex()); paramsLists.put(4, child.getAge().toString()); paramsLists.put(5, child.getBrithDay()); //格式化添加幼儿时间 paramsLists.put(6, dateFormater.format(child.getAddDate())); paramsLists.put(7, child.getDr()); paramsLists.put(8, child.getFatherName()); paramsLists.put(9, child.getFatherPhone()); paramsLists.put(10, child.getFatherWork()); paramsLists.put(11, child.getMotherName()); paramsLists.put(12, child.getMotherPhone()); paramsLists.put(13, child.getMotherWork()); paramsLists.put(14, child.getAddress()); paramsLists.put(15, child.getComments()); lists.add(paramsLists); } ExcelUtil.writeExcel(filePath, titles, lists); Map<String,Object> json = new HashMap<String, Object>(); json.put("message", "导出Excel文件到桌面,文件名为:" + fileName + ".xls"); return json; }ExcelUtil.java 对Excel操作的工具类。
public class ExcelUtil { /** * @info 写出Excel标题 * @param fos * @return */ public static void writeExcelTitle(String filePath, String[] ss) throws IOException { OutputStream fos = new FileOutputStream(filePath); HSSFWorkbook xls = new HSSFWorkbook(); HSSFSheet sheet = xls.createSheet(); HSSFRow row = sheet.createRow(0);// 第一行 for (int i = 0; i < ss.length; i++) { row.createCell(i).setCellValue(ss[i]); } xls.write(fos); fos.close(); } /** * @info 写出Excel标题内容 * @param fos * @return */ public static byte[] writeExcel(String[] titles, List<Map<Integer, String>> lists) throws IOException { HSSFWorkbook xls = new HSSFWorkbook(); HSSFSheet sheet = xls.createSheet(); HSSFRow row = sheet.createRow(0);// 第一行 for (int i = 0; i < titles.length; i++) { row.createCell(i).setCellValue(titles[i]); } // 内容 int rowNum = 1; for (Map<Integer, String> map : lists) { HSSFRow rowTmp = sheet.createRow(rowNum); int cols = map.size(); for (int i = 0; i < cols; i++) { rowTmp.createCell(i).setCellValue(map.get(i)); } rowNum++; } ByteArrayOutputStream fos = new ByteArrayOutputStream(); xls.write(fos); byte[] buf = fos.toByteArray();// 获取内存缓冲区中的数据 fos.close(); return buf; } /** * @info 写出Excel标题内容 * @param fos * @return */ public static void writeExcel(String filePath, String[] titles, List<Map<Integer, String>> lists) throws IOException { OutputStream fos = new FileOutputStream(filePath); HSSFWorkbook xls = new HSSFWorkbook(); HSSFSheet sheet = xls.createSheet(); HSSFRow row = sheet.createRow(0);// 第一行 for (int i = 0; i < titles.length; i++) { row.createCell(i).setCellValue(titles[i]); } // 内容 int rowNum = 1; for (Map<Integer, String> map : lists) { HSSFRow rowTmp = sheet.createRow(rowNum); int cols = map.size(); for (int i = 0; i < cols; i++) { rowTmp.createCell(i).setCellValue(map.get(i)); } rowNum++; } xls.write(fos); fos.close(); } /** * @info 读取Excel内容,List行,MAP行数据 * @param filePath * @return */ public static List<Map<String, String>> readExcelKeyMap(String filePath) throws IOException { List<Map<String, String>> contents = new LinkedList<Map<String, String>>(); InputStream is = new FileInputStream(filePath); POIFSFileSystem fs = new POIFSFileSystem(is); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); // 得到总行数 int rowNum = sheet.getLastRowNum(); HSSFRow row = sheet.getRow(0);// 第一行 // 总列数 int colNum = row.getPhysicalNumberOfCells(); // 正文内容应该从第二行开始,第一行为表头的标题 String[] keys = readExcelTitle(filePath); for (int i = 1; i <= rowNum; i++) { row = sheet.getRow(i); int j = 0; Map<String, String> content = new HashMap<String, String>(); while (j < colNum) { String cellValue = getCellFormatValue(row.getCell(j)).trim(); content.put(keys[j], cellValue); j++; } contents.add(content); } is.close(); return contents; } public static List<Map<String, String>> readExcelKeyMap(InputStream is) throws IOException { List<Map<String, String>> contents = new LinkedList<Map<String, String>>(); POIFSFileSystem fs = new POIFSFileSystem(is); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); // 得到总行数 int rowNum = sheet.getLastRowNum(); HSSFRow row = sheet.getRow(0);// 第一行 // 总列数 int colNum = row.getPhysicalNumberOfCells(); // 正文内容应该从第二行开始,第一行为表头的标题 // 标题总列数 String[] keys = new String[colNum]; for (int i = 0; i < colNum; i++) { keys[i] = getCellFormatValue(row.getCell(i)); } for (int i = 1; i <= rowNum; i++) { row = sheet.getRow(i); int j = 0; Map<String, String> content = new HashMap<String, String>(); while (j < colNum) { String cellValue = getCellFormatValue(row.getCell(j)).trim(); content.put(keys[j], cellValue); j++; } contents.add(content); } is.close(); return contents; } /** * @info 读取Excel标题 * @param is * @return */ public static String[] readExcelTitle(String filePath) throws IOException { InputStream is = new FileInputStream(filePath); POIFSFileSystem fs = new POIFSFileSystem(is); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); HSSFRow row = sheet.getRow(0);// 第一行 // 标题总列数 int colNum = row.getPhysicalNumberOfCells(); String[] title = new String[colNum]; for (int i = 0; i < colNum; i++) { title[i] = getCellFormatValue(row.getCell(i)); } is.close(); return title; } /** * @info 读取Excel内容,List行,MAP行数据 * @param filePath * @return */ public static List<Map<Integer, String>> readExcelContent(String filePath) throws IOException { List<Map<Integer, String>> contents = new LinkedList<Map<Integer, String>>(); InputStream is = new FileInputStream(filePath); POIFSFileSystem fs = new POIFSFileSystem(is); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); // 得到总行数 int rowNum = sheet.getLastRowNum(); HSSFRow row = sheet.getRow(0);// 第一行 // 总列数 int colNum = row.getPhysicalNumberOfCells(); // 正文内容应该从第二行开始,第一行为表头的标题 for (int i = 1; i <= rowNum; i++) { row = sheet.getRow(i); int j = 0; Map<Integer, String> content = new HashMap<Integer, String>(); while (j < colNum) { String cellValue = getCellFormatValue(row.getCell(j)).trim(); content.put(j, cellValue); j++; } contents.add(content); } is.close(); return contents; } /** * @info 读取Excel值 * @param cell * @return */ static String getCellFormatValue(HSSFCell cell) { String cellvalue = ""; if (cell != null) { switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: { BigDecimal b = new BigDecimal(cell.getNumericCellValue()); cellvalue = b.toPlainString(); break; } case HSSFCell.CELL_TYPE_FORMULA: { cell.setCellType(Cell.CELL_TYPE_STRING); cellvalue = cell.getStringCellValue(); //System.out.println(cellvalue); break; } case HSSFCell.CELL_TYPE_STRING: cellvalue = cell.getRichStringCellValue().getString(); //System.out.println(cellvalue); break; default: cellvalue = ""; } } else { cellvalue = ""; } return cellvalue; } /** * @info 读取Excel值 * @param cell * @return */ static String getStringCellValue(HSSFCell cell) { String strCell = ""; switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_STRING: strCell = cell.getStringCellValue(); break; case HSSFCell.CELL_TYPE_NUMERIC: strCell = String.valueOf(cell.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_BOOLEAN: strCell = String.valueOf(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_BLANK: strCell = ""; break; default: strCell = ""; break; } if (strCell.equals("") || strCell == null) { return ""; } return strCell; } }
相关文章推荐
- 2.1 使用eclipse4.4 搭建 maven简单结构项目。
- Java获取月度篇(一)
- Struts2文件上传与过滤
- 快速排序_java实现
- java.lang.RuntimeException: Binary XML file line #52: You must supply a layout_height attribute.
- java泛型
- 《Java完全参考手册》总结(1)
- Java使用poi批量导入excel
- Java ,== ,equals的区别
- [Java]单向链表实现
- webservice WSDL java例子
- Java利用JSOUP爬取教务处成绩信息简单示例
- java中Class对象详解和类名.class, class.forName(), getClass()区别
- Java中的默认值和构造器
- spring - property和constructor-arg的使用
- java 对象与内存
- spring profile来用不同的文件,配置不同的环境, deploy不同的war
- JavaWeb学习篇之----容器Response详解
- Java类中变量初始化、方法调用的顺序
- 框架整合之struts2 404问题