对比新文件 |
| | |
| | | package com.nanometer.smartlab.util; |
| | | |
| | | import org.apache.poi.hssf.usermodel.HSSFRow; |
| | | import org.apache.poi.hssf.usermodel.HSSFSheet; |
| | | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
| | | |
| | | import javax.faces.context.FacesContext; |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | public class ExcelUtils { |
| | | |
| | | public static HSSFWorkbook exportExcelNew(Map<String, String> map, List<Map> dataList, String name){ |
| | | HSSFWorkbook hssfWorkbook = new HSSFWorkbook(); |
| | | Integer total = null; |
| | | //sheet名字 |
| | | HSSFSheet sheet = hssfWorkbook.createSheet(name); |
| | | HSSFRow titlerRow = sheet.createRow(0); |
| | | |
| | | int k = 0; |
| | | for(Map.Entry<String, String> entry:map.entrySet()){ |
| | | //表头 |
| | | titlerRow.createCell(k).setCellValue(entry.getValue()); |
| | | k++; |
| | | } |
| | | //数据 |
| | | for (int i = 0; i < dataList.size(); i++) { |
| | | HSSFRow dataRow = sheet.createRow(i + 1); |
| | | int j = 0; |
| | | for(Map.Entry<String, String> entry:map.entrySet()){ |
| | | //表头 |
| | | dataRow.createCell(j).setCellValue(dataList.get(i).get(entry.getKey()) == null ? "" : String.valueOf(dataList.get(i).get(entry.getKey()))); |
| | | j++; |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | return hssfWorkbook; |
| | | } |
| | | |
| | | |
| | | public static void export2Excel(List<Map> list, String name, Map<String, String> map) throws Exception { |
| | | if (list == null || list.size() == 0) { |
| | | throw new Exception("没有数据可以导出"); |
| | | } |
| | | |
| | | ServletOutputStream out = null; |
| | | InputStream is = null; |
| | | try { |
| | | FacesContext ctx = FacesContext.getCurrentInstance(); |
| | | ctx.responseComplete(); |
| | | String contentType = "application/x-download"; |
| | | HttpServletResponse response = (HttpServletResponse) ctx |
| | | .getExternalContext().getResponse(); |
| | | response.setContentType(contentType); |
| | | StringBuffer contentDisposition = new StringBuffer(); |
| | | contentDisposition.append("attachment;"); |
| | | contentDisposition.append("filename=\""); |
| | | contentDisposition.append(name).append(".xls"); |
| | | contentDisposition.append("\""); |
| | | response.setHeader( |
| | | "Content-Disposition", |
| | | new String(contentDisposition.toString().getBytes( |
| | | System.getProperty("file.encoding")), "ISO8859-1")); |
| | | out = response.getOutputStream(); |
| | | HSSFWorkbook hssfWorkbook = ExcelUtils.exportExcelNew(map, list, name); |
| | | hssfWorkbook.write(out); |
| | | // out.flush(); |
| | | ctx.responseComplete(); |
| | | |
| | | } catch (Exception e) { |
| | | if (is != null) { |
| | | try { |
| | | is.close(); |
| | | } catch (IOException e1) { |
| | | e1.printStackTrace(); |
| | | } |
| | | } |
| | | if (out != null) { |
| | | try { |
| | | out.close(); |
| | | } catch (IOException e1) { |
| | | e1.printStackTrace(); |
| | | } |
| | | } |
| | | e.printStackTrace(); |
| | | } finally { |
| | | if (is != null) { |
| | | try { |
| | | is.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | if (out != null) { |
| | | try { |
| | | out.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |