}
/** * 追加文件:使用RandomAccessFile * * @param fileName 文件名 * @param content 追加的内容 * @param fileTitle 文件表头 * @param resultData 文件内容 * @throws IOException */ public static void appendContent(String fileName, List resultData,String fileTitle,String fileType) throws IOException { StringBuffer fileContent = new StringBuffer(); String separate = "";//文件分割 csv用引号分割 txt 用逗号分割txt 可以换 String firstSep = "";//开头分割 txt为空 csv双引号 if("txt".equals(fileType)){ separate = ""; }else{ separate = "\""; firstSep = "\""; } if(!DAPUtil.isNull(fileTitle)){//内容写入 fileContent = new StringBuffer(fileTitle);//表头写入 } for(int i=0;i<resultData.size();i++) { ArrayList rowData = (ArrayList)resultData.get(i); for(int j=0;j<rowData.size();j++) { String value = (String) rowData.get(j); fileContent.append(firstSep+value.replace("\"", "\"\"")+separate); if (j != rowData.size() - 1) fileContent.append(","); } fileContent.append("\r\n"); } BufferedWriter out = null; try { out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName, true))); out.write(fileContent.toString()); } catch (Exception e) { e.printStackTrace(); } finally { try { if(out != null){ out.close(); } } catch (IOException e) { e.printStackTrace(); } } }
public static void appendContentZip(ZipOutputStream zipFileStream, List resultData,String ct,String fileType) { StringBuffer sbr = new StringBuffer(); String separate = "";//文件分割 csv用引号分割 txt 用逗号分割txt 可以换 String firstSep = "";//开头分割 txt为空 csv双引号 if("txt".equals(fileType)){ separate = ""; }else{ separate = "\""; firstSep = "\""; } if(ct==null){ for(int i=0;i<resultData.size();i++) { ArrayList rowData = (ArrayList)resultData.get(i); for(int j=0;j<rowData.size();j++) { String value = (String) rowData.get(j); sbr.append(firstSep+value.replace("\"", "\"\"")+separate); if (j != rowData.size() - 1) sbr.append(","); } sbr.append("\r\n"); } }else{ sbr = new StringBuffer(ct); } try { if(!DAPUtil.isNull(sbr)) zipFileStream.write(sbr.toString().getBytes()); } catch (Exception e) { e.printStackTrace(); } finally { } }
/** * 下载文件 * @param realPath * @param request * @param response * @throws Exception */ public static void downFile(String realPath,String fileName,HttpServletResponse response) throws Exception { BufferedInputStream bis = null; BufferedOutputStream bos = null; InputStream fis = null; OutputStream fos = null; try { File uploadFile = new File(realPath); fis = new FileInputStream(uploadFile); bis = new BufferedInputStream(fis); response.setContentType("application/octet-stream;charset=GBK"); response.setHeader("Content-disposition", "attachment;filename ="+fileName); response.resetBuffer(); fos = response.getOutputStream(); bos = new BufferedOutputStream(fos); int bytesRead = 0; byte[] buffer = new byte[4096]; while((bytesRead = bis.read(buffer, 0, 4096)) != -1) { bos.write(buffer, 0, bytesRead); } bos.flush(); } catch (FileNotFoundException e) { PrintWriter pw = response.getWriter(); pw.print("<script>alert('文件不存在!');window.location.href=document.referrer;</script>"); pw.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (bis != null) bis.close(); if (fis != null) fis.close(); if (bos != null) bos.close(); if (fos != null) fos.close(); } }
/** * 导出成CSV TXT * resultData中每个是一行数据。 * @param out * @param colTitleList 标题 * @param resultData 数据。 * @throws Exception */ public static void writeCSV(OutputStream out,List colTitleList,List resultData) throws Exception { if (resultData == null) resultData = new ArrayList(); PrintWriter pw = null; if (EDAUtil.isWeblogic()) pw = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(out,1024),"GBK")); else pw = new PrintWriter(new BufferedOutputStream(out,1024)); if (colTitleList != null){ for(int i=0;i<colTitleList.size();i++){ String outValue = String.valueOf(colTitleList.get(i)); pw.print(outValue); if (i != colTitleList.size() - 1) pw.print(","); } pw.println(); } for(int i=0;i<resultData.size();i++) { ArrayList rowData = (ArrayList)resultData.get(i); for(int j=0;j<rowData.size();j++) { String value = (String) rowData.get(j); pw.print("\""+value.replace("\"", "\"\"")+"\""); if (j != rowData.size() - 1) pw.print(","); } pw.println(); } pw.close(); }
/** * 导出成Excel * resultData中每个是一行数据。 * @param out * @param colTitleList 标题 * @param resultData 数据。 * @throws Exception */ public void writeExcel(HttpServletResponse response,List colTitleList,List resultData,String fileName) throws Exception { OutputStream ouputStream = null; HSSFWorkbook wb = new HSSFWorkbook(); int pageSize = 1; HSSFSheet sheet = wb.createSheet("sheet"+pageSize); HSSFCellStyle style = wb.createCellStyle(); HSSFRow row = sheet.createRow(0); if (resultData == null) resultData = new ArrayList(); if (colTitleList != null){ for(int i=0;i<colTitleList.size();i++){ String outValue = String.valueOf(colTitleList.get(i)); HSSFCell cell = row.createCell(i); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellStyle(style); cell.setCellValue(outValue); } } for(int i=0;i<resultData.size();i++) { ArrayList rowData = (ArrayList)resultData.get(i); row = sheet.createRow(i+1); for(int j=0;j<rowData.size();j++) { String value = (String) rowData.get(j); row.createCell(j).setCellValue(value); } } ouputStream = response.getOutputStream(); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename = "+fileName+".xls"); wb.write(ouputStream); ouputStream.flush(); ouputStream.close(); }
虽然 代码有点多 但跑起来很快 一百多万的数据一两分钟就能导完
