jxls에서 xls 모형을 내보냅니다.

3803 단어 JXLS
도구 클래스.

public class ExportExcelUtil {
	
	private Map<String,Object> beans = new HashMap<String,Object>();
	
	private String exportFileName = "export.xls";
	
	public void setBeans(String key,Object obj){
		//      
		beans.put(key, obj);
	}
	
	
	
	public ExportExcelUtil() {
		//             beans 。
		//excel      :${dateUtil.formatToDate(entity.checkDate)}
		// ${dateUtil.formatToDateTime(entity.checkDateTime)}
		 beans.put("dateUtil", new DateUtil());
	}



	public void exportExcel(String fileName){
		try {
			Struts2Utils.getRequest().setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		String systemPath  = this.getClass().getResource("/").getPath();
		//      
		String templateFileName = systemPath + "templates/"+fileName+".xls";
//		File outFile = new File(outFileName);
	    XLSTransformer transformer = new XLSTransformer();
        try {
        	InputStream is = new BufferedInputStream(new FileInputStream(templateFileName));
//        	transformer.transformXLS(templateFileName, beans, outFileName);
        	//         ,           
        	HSSFWorkbook workbook = (HSSFWorkbook) transformer.transformXLS(is, beans);
        	resetCellFormula(workbook);
			//        -
        	Struts2Utils.getResponse().reset();
			//   xls       2013-04-01
			String outFileName = DateUtil.formatToDate(new Date())+new String(fileName.getBytes("gb2312"),"ISO8859-1");
			Struts2Utils.getResponse().setHeader("Content-Type","application/force-download");
			Struts2Utils.getResponse().setHeader("Content-Type","application/vnd.ms-excel");
			Struts2Utils.getResponse().setHeader("Content-Disposition", "attachment;success=true;filename="+outFileName+".xls"); 
			//  response     ,  excel  
			OutputStream os = Struts2Utils.getResponse().getOutputStream();
			workbook.write(os);
			is.close();
			os.flush();
			os.close();
		} catch (ParsePropertyException e) {
			e.printStackTrace();
		} catch (InvalidFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	public String getExportFileName() {
		return exportFileName;
	}

	public void setExportFileName(String exportFileName) {
		this.exportFileName = exportFileName;
	}

	//           。
	public void resetCellFormula(HSSFWorkbook wb) { 
		
		HSSFFormulaEvaluator e = new HSSFFormulaEvaluator(wb);
		//       
		int sheetNum = wb.getNumberOfSheets();
		//     
		for (int i = 0; i < sheetNum; i++) {
			HSSFSheet sheet = wb.getSheetAt(i);
			int rows = sheet.getLastRowNum() + 1;//     
			for (int j = 0; j < rows; j++) {
				HSSFRow row = sheet.getRow(j);
				if (row == null)
					continue;
				int cols = row.getLastCellNum();//      
				for (int k = 0; k < cols; k++) {
					HSSFCell cell = row.getCell(k);
					if(cell!=null){
						//        ,         
						if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
							cell.setCellFormula(cell.getCellFormula());										cell=e.evaluateInCell(cell);
													}
					} else {
						continue;
					}
				}
			}
		}


		
	}

프론트 데스크톱 js 호출.
window.location.href = url;

좋은 웹페이지 즐겨찾기