POI 后台生成Excel,在前台显示进度

使用Apache POI和DWR

poi用于在后台生成Excel,用DWR在前台显示后台生成Excel的进度

基本的生成Excel类:

ExcelHelper.java

public class ExcelHelper extends Thread{
	private int rowIndex=0;
	private int count;
	private String fileName;
	private String sheetName;
	private List dataList;

	public ExcelHelper(String fileName, String sheetName, List dataList) {
		this.fileName = fileName;
		this.sheetName = sheetName;
		this.dataList = dataList;
		this.count=dataList.size();
	}

	@Override
	public void run() {
		try {
			if (rowIndex==0) {
				generateWorkbook();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 生成Excel文件
	 * 
	 * @param fileName
	 *            要生成的Excel文件名(可用绝对或相对路径)
	 * @param sheetName
	 *            生成的Excel文件中的sheet名
	 * @param dataList
	 *            要放入Excel文件的内容
	 * @throws IOException
	 */
	public void generateWorkbook() throws IOException {
		Workbook wb = null;
		if (fileName.endsWith(".xlsx")) {
			wb = new XSSFWorkbook();
		} else if (fileName.endsWith(".xls")) {
			wb = new HSSFWorkbook();
		} else {
			fileName = fileName.concat(".xls");
			wb = new HSSFWorkbook();
		}
		
		CellStyle cellStyle = wb.createCellStyle();
		Font font = wb.createFont();
		font.setColor(HSSFColor.RED.index);
		cellStyle.setFont(font);
		
		Sheet sheet = wb.createSheet(sheetName);
		rowIndex = 0;
		for (String[] rowData : dataList) {
			Row row = sheet.createRow(rowIndex);
			rowIndex++;
			int cellIndex = 0;
			for (String cellData : rowData) {
				Cell cell = row.createCell(cellIndex);
				cell.setCellValue(cellData);
				cell.setCellStyle(cellStyle);
				cellIndex++;
			}
		}

		FileOutputStream fileOut = new FileOutputStream(fileName);
		wb.write(fileOut);
		fileOut.close();
	}
	
	public int getRowIndex() {
		return rowIndex;
	}

	public int getCount() {
		return count;
	}
}

 DWR操作类:

ExcelHelperDWR.java

public class ExcelHelperDWR {
	private static Map excelHelperMap;
	public ExcelHelperDWR() {
		if (excelHelperMap == null) {
			excelHelperMap = new HashMap();
		}
	}

	public int getRowIndex(long threadId) {
		ExcelHelper excelHelper = excelHelperMap.get(threadId);
		if (excelHelper.getRowIndex() dataList = new ArrayList();
		for (int i = 0; i < 1000; i++) {
			dataList.add(new String[] { "1", "2612601001", "学生1", "123", "3", null, "", "13712345678" });
			dataList.add(new String[] { "1", "2612601001", "学生2", "123", "3", "", "", "13712345678" });
			dataList.add(new String[] { "1", "2612601001", "学生3", "123", "3", "", "", "13712345678" });
			dataList.add(new String[] { "1", "2612601001", "学生4", "123", "3", "", "", "13712345678" });
		}
		//end test case
		
		ExcelHelper excelHelper = new ExcelHelper(fileName, sheetName, dataList);
		excelHelper.start();
		long threadId = excelHelper.getId();
		excelHelperMap.put(threadId, excelHelper);
		return threadId;
	}
}

 前台index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



	
		My JSP 'index.jsp' starting page
		
		
		
		
		
		
		
		
		
		
	

	
		
		

  OK了,很简易~

 

你可能感兴趣的:(Java)