Export the excel/txt file of report list by java

First of all, we should click a button or some pic to trigger the action of exporting,here I used a button:

<input type="button" value="Export" onclick="export()" class="buttoncss"/>

Then the export function should like:

window.location.href="*.do?method=exportList"

 whick the method used for achieve the data that need exported.

List excelList = getPrintService().queryTask(ref, type, taskName,level);//data content
List<String> listHeader = new ArrayList<String>();//head of the data
listHeader.add("Reference");
listHeader.add("TaskName");
request.setAttribute("listHeader", listHeader);
request.setAttribute("excelList", excelList);

 then mapping this action to this jsp:

<%@page language="java" import="java.util.List, com.alba.util.ExcelBean, com.alba.util.DateUtil" pageEncoding="UTF-8" %> 
<% 
	response.setContentType("text/plain");//set as a txt document
	response.setHeader("Content-Disposition", "attachment;filename=" + 
		new String(("PrintTask_" + 
				DateUtil.formatFullDate(new java.util.Date().getTime()) + ".txt").getBytes(), "iso-8859-1"));//save as a attachment 
	ExcelBean eb = new ExcelBean();
	List<String> listHeader= (List<String>)request.getAttribute("listHeader");
	List excelList= (List)request.getAttribute("excelList");
	try{
		eb.exportTask(response.getOutputStream(),excelList,listHeader);//exporting method
	}catch(Exception e){
		e.printStackTrace();
	}
	
	out.clear();
	out = pageContext.pushBody();//save the object 'Out' and refresh
%>

 then the exportTask method is:

public void exportTask(OutputStream os,List excelList,List<String> listHeader){
		OutputStreamWriter fw = new OutputStreamWriter(os);
		StringBuffer row = null;
		if(fw != null){
			row = new StringBuffer();
			for(int i=0;i<listHeader.size();i++){
				if(i!=listHeader.size()-1){
					row.append(listHeader.get(i)+"\t");
				}else{
					row.append(listHeader.get(i)+"\n");
				}
			}
            try {
				fw.write(row.toString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
			//add the contend
			if(excelList!=null&&excelList.size()>0){
				for(int i=0;i<excelList.size();i++){
						row = new StringBuffer();
						PrintTask task = (PrintTask) excelList.get(i);
							String reference = task.getProjectprint().getProject().getReference()!=null?task.getProjectprint().getProject().getReference()+"\t":"\t";
							String level = getLevelValue(task.getLevel()!=null?task.getLevel():0);
							String status = getStatusValue(task.getProjectprint().getStatus()!=null?task.getProjectprint().getStatus():10);
							
							row.append(reference)
							   .append(task.getTaskname()!=null?task.getTaskname()+"\t":"\t")
							   .append(task.getProjectprint().getType()!=null?task.getProjectprint().getType()+"\t":"\t")
							   .append(task.getProjectprint().getDestination()!=null?task.getProjectprint().getDestination()+"\t":"\t")
							   .append(task.getProjectprint().getProject().getDescription()!=null?task.getProjectprint().getProject().getDescription()+"\t":"\t")
							   .append(task.getProjectprint().getProject().getPlanWeekToShow()!=null?task.getProjectprint().getProject().getPlanWeekToShow()+"\t":"\t")
							   .append(level+"\t")
							   .append(task.getNeededtime()!=null?task.getNeededtime()+"\t":"\t")
							   .append(status+"\n");
							 try {
									fw.write(row.toString());
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
							}
					}
		  }
		try {
			fw.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   }
	}

 when we want  to export as a excel file,we can use this method and do some changes like export txt file is ok:

public void exportExcelByJxl(List excelList,List<String> listHeader){
		WritableWorkbook wwb = null;
		try {
			File fileName = new File("d:/PrintList/");
			if(!fileName.exists()){
				fileName.mkdirs();
			}
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
			wwb = Workbook.createWorkbook(new File("d:/PrintList/PrintList"+sdf.format(date)+".xls"));		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		//add the header
		if(wwb != null){
			WritableSheet ws = wwb.createSheet("test", 0);
			for(int i=0;i<listHeader.size();i++){
				Label labelC = new Label(i, 0, listHeader.get(i));
				try {
					ws.addCell(labelC);
				} catch (RowsExceededException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (WriteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		
			//add the contend
			if(excelList!=null&&excelList.size()>0){
				for(int i=0;i<excelList.size();i++){
					for(int j=0;j<listHeader.size();j++){
						ProjectPrint print = (ProjectPrint) excelList.get(i);	
						Label labelC = null;
						
						if(print == null){
							labelC = new Label(j,i+1,"");
						}else{
							String reference = "";
							String cusName = "";
							String mark = getMarkValue(print.getMarked());
							String revise = getReviseValue(print.getRevised());
							String status = getStatusValue(print.getStatus());
							if(print.getMarked()!=2){
								reference = print.getProject().getReference()!=null?print.getProject().getReference():"";
								cusName = print.getProject().getCustomerName()!=null?print.getProject().getCustomerName():"";
							}
							switch(j){
							  case 0 : labelC = new Label(j,i+1,print.getId()!=null? print.getId()+"" :"");break;
							  case 1 : labelC = new Label(j,i+1,reference);break;
							  case 2 : labelC = new Label(j,i+1,print.getType()!=null?print.getType():"");break;
							  case 3 : labelC = new Label(j,i+1,print.getInkMaterial1()!=null?print.getInkMaterial1():"");break;
							  case 4 : labelC = new Label(j,i+1,print.getSurfaceMaterial()!=null?print.getSurfaceMaterial():"");break;
							  case 5 : labelC = new Label(j,i+1,print.getProject().getPlanWeekToShow()!=null?print.getProject().getPlanWeekToShow():"");break;
							  case 6 : labelC = new Label(j,i+1,cusName);break;
							  case 7 : labelC = new Label(j,i+1,print.getProject().getDescription()!=null?print.getProject().getDescription():"");break;
							  case 8 : labelC = new Label(j,i+1,print.getNumberofPage()!=null?print.getNumberofPage()+"":"");break;
							  case 9 : labelC = new Label(j,i+1,mark);break;
							  case 10 : labelC = new Label(j,i+1,revise);break;
							  case 11 : labelC = new Label(j,i+1,status);break;
							  default : break;
							}
						}
						
						try {
							ws.addCell(labelC);
						} catch (RowsExceededException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (WriteException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
			try {
				wwb.write();
				wwb.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (WriteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	   }
	}

 

你可能感兴趣的:(java,Excel,OS,J#,idea)