package com.pingan.jinke.sms.examine.controller; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Date; import java.util.Hashtable; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.commons.beanutils.BeanUtils; import org.w3c.dom.Document; public class Test { /** * 导出为CVS文件 * * @param exportData */ public static File createCSVFile(List exportData, LinkedHashMap rowMapper, String outPutPath) { File csvFile = null; BufferedWriter csvFileOutputStream = null; try { csvFile = File.createTempFile("temps", ".csv", new File(outPutPath)); // GB2312使正确读取分隔符"," csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "GB2312"), 1024); // 写入文件头部 for (Iterator propertyIterator = rowMapper.entrySet().iterator(); propertyIterator.hasNext();) { java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next(); csvFileOutputStream.write("\"" + propertyEntry.getValue().toString() + "\""); if (propertyIterator.hasNext()) { csvFileOutputStream.write(","); } } csvFileOutputStream.newLine(); // 写入文件内容 for (Iterator iterator = exportData.iterator(); iterator.hasNext();) { Object row = (Object) iterator.next(); for (Iterator propertyIterator = rowMapper.entrySet().iterator(); propertyIterator.hasNext();) { java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next(); csvFileOutputStream.write("\"" + BeanUtils.getProperty(row, propertyEntry.getKey().toString()).toString() + "\""); if (propertyIterator.hasNext()) { csvFileOutputStream.write(","); } } if (iterator.hasNext()) { csvFileOutputStream.newLine(); } } csvFileOutputStream.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { csvFileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return csvFile; } /** * 导出为CSV文件 * * @param response * @param exportData * @param propertyNames * @param fileName * @param outputPath * @throws FileNotFoundException */ public static void exportToCSVFile(HttpServletResponse response, List exportData, LinkedHashMap rowMapper, String fileName, String outputPath) throws FileNotFoundException { File csvFile = createCSVFile(exportData, rowMapper, outputPath); csvFile.delete(); } public static void main(String[] args) { LinkedHashMap map = new LinkedHashMap(); map.put("1", "第一列"); map.put("2", "第二列"); map.put("3", "第三列"); map.put("4", "第四列"); List exportData = new ArrayList<Map>(); Map row1 = new LinkedHashMap<String, String>(); row1.put("1", "11"); row1.put("2", "12"); row1.put("3", "13"); row1.put("4", "14"); exportData.add(row1); row1 = new LinkedHashMap<String, String>(); row1.put("1", "21"); row1.put("2", "22"); row1.put("3", "23"); row1.put("4", "24"); exportData.add(row1); Test.createCSVFile(exportData, map, "E:/"); } public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("gbk"); response.setContentType("text/html;charset=gbk"); HttpSession session = request.getSession(); String status = request.getParameter("status"); status = status == null ? "" : status.trim(); Document dom = null; dom = (Document)session.getAttribute("domList"); session.setAttribute("download",dom); session.setAttribute("sname","download"); Hashtable hsjxl=new Hashtable(); hsjxl.put("title",new Date().getTime()); session.setAttribute("header",hsjxl); session.setAttribute("para","header"); response.sendRedirect(request.getContextPath()+"/Dom2Csv"); } }
最近下载更多