⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exportreportieservlet.java

📁 利用jxl和jdom到报表用的,可以自定义excel模板
💻 JAVA
字号:
package com.glf.reportIE.servlet;

import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.glf.reportIE.commons.*;
import com.glf.reportIE.dao.*;



public class ExportReportIEServlet extends HttpServlet 
{
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	public void doGet(HttpServletRequest request, 
    			HttpServletResponse response)
    		throws ServletException, IOException
    {
    	response.setContentType("text/html;charset=UTF-8");	
		//PrintWriter out = response.getWriter();
    	//HttpSession session=request.getSession();
    	String screen=request.getServletPath();
		
    	if(screen.equals("/pages/reportIE/exportExcel")){
    		String errorMessage = "";
    		String forwardPage = "exportExcel.jsp";
    		try{
        		String formName = request.getParameter("formName");
        		String searchCondition = request.getParameter("searchCondition");
        		//下面是存放xml文件的位置
        		String xmlPath = request.getRealPath("")+"/pages/reportIE/xml/"+formName+".xml";
        		//下面存放excel模板文件的路径
        		String source = request.getRealPath("")+"/pages/reportIE/model/"+formName+".xls";
        		//下面是存放填充数据后excel的文件路径
        		String newPath = request.getRealPath("")+"/pages/reportIE/excel/["+searchCondition+"]"+formName+".xls";
        		//根据xml文件取出该报表所绑定的类名和方法名
        		String className = XmlUtils.getOtherAttributeValue(xmlPath,"bindClass","formName",formName,"className");
        		String methodName = XmlUtils.getOtherAttributeValue(xmlPath,"bindClass","formName",formName,"methodName");
        		//方法返回的一个HashMap(包含所有用户信息)
        		Class clzz = Class.forName(className);//装载类
        		Object obj = clzz.newInstance();//实例化
        		Class argClass = Class.forName("java.lang.String");//装载要调用的方法参数
        		Method sm = clzz.getMethod(methodName, argClass);//装载方法名和参数类型
        		Object[] args = new Object[1];
        		args[0] = new String(searchCondition);//设定参数值
        		HashMap archivesInfoMap = (HashMap)sm.invoke(obj, args[0]);
        		//System.out.println(rval);
//        		Class clzz = Class.forName("com.glf.reportIE.dao.ArchivesInfoDBOperate");	//装载类
//        		Object obj = clzz.newInstance();											//实例化
       					
//        		Class[] argsClass = new Class[1];
//        		argsClass[0] = Class.forName("java.lang.String");							//装载要调用的方法参数
//        			
//        		Method sm = clzz.getMethod("setRealname", argsClass);						//装载方法名和参数类型
//        		Object[] args = new Object[1];												//设定参数值
//        		args[0] = new String("tombo");
//        		String rval = sm.invoke(obj, args).toString();														//调用方法
//        		
//        		Method gm = clzz.getMethod("getRealname", null);							//装载方法名(无参型)
//        		String rval = gm.invoke(obj, null).toString();								//调用方法并接收返回值
//        		System.out.println(rval);
        		
        		//ArchivesInfoDBOperate _archivesDao = new ArchivesInfoDBOperate();
        		//HashMap archivesInfoMap = _archivesDao.getArchivesInfo(searchCondition);
        		//按place,description,field顺序取出所有
        		boolean xmlflag = XmlUtils.fileIsExits(xmlPath);
        		ArrayList cellList = new ArrayList();//【报表绑定类和方法后所有相关值的集合】
        		ArrayList cellValueList = new ArrayList();//【列、行、单元格的值】的集合
        		if(xmlflag){
        			cellList = XmlUtils.getAllCellValue(xmlPath);
        			cellValueList = new ArrayList();
	        		for(int i=0;i<cellList.size();i++){
	        			String place = cellList.get(i).toString();
	        			if(ExcelColumnMap.isCharacter(place.substring(1, 2))){
	        				cellValueList.add(ExcelColumnMap.getColumnNumber(place.substring(0, 2)));
	        				cellValueList.add(place.substring(2, place.length()));
	        				cellValueList.add(archivesInfoMap.get(cellList.get(i+2)));
	        			}else{
	        				cellValueList.add(ExcelColumnMap.getColumnNumber(place.substring(0, 1)));
	        				cellValueList.add(place.substring(1, place.length()));
	        				cellValueList.add(archivesInfoMap.get(cellList.get(i+2)));
	        			}
	        			i+=2;
	        		}
        		}
        		//out.print(cellValueList);
        		//导出填充好数据的excel表
        		boolean modelflag = XmlUtils.fileIsExits(source);
        		//System.out.print(xmlflag);
        		if(xmlflag){
        			if(modelflag){
    	        		if(archivesInfoMap.isEmpty()){
    	            		errorMessage = "您输入查询条件并不存在,不能生成excel报表!";
    	            		request.setAttribute("errorMessage", errorMessage);
    	            		request.getRequestDispatcher(forwardPage).forward(request,response);
    	        		}else{
    	        			UploadDownloadUtils _uploadDao = new UploadDownloadUtils();
    	            		ReadExcelModelUtils _readExcelDao = new ReadExcelModelUtils();
    	            		_readExcelDao.exportExcelWithData(source, newPath, cellValueList);
    	            		_uploadDao.downLoad(newPath, response, false);
    	        		}
            		}else{
            			errorMessage = "您选择的报表模板并没有导入,不能生成excel报表!";
                		request.setAttribute("errorMessage", errorMessage);
                		request.getRequestDispatcher(forwardPage).forward(request,response);
            		}
        		}else{
        			errorMessage = "您选择的报表模板还没有【设置字段位置】,不能生成excel报表!";
            		request.setAttribute("errorMessage", errorMessage);
            		request.getRequestDispatcher(forwardPage).forward(request,response);
        		}
    		}catch(Exception e){
    			errorMessage = "您选择的报表绑定的类或方法错误,不能生成excel报表,请仔细检查后再导出!";
        		request.setAttribute("errorMessage", errorMessage);
        		request.getRequestDispatcher(forwardPage).forward(request,response);
    		}	
    	}
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
    					throws ServletException, IOException
    {
        doGet(request,response);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -