福网

ZK AJAX 学习笔记第十天 导出Excel

ZK AJAX 学习笔记
—-Run Java and JSP on ZK

我是利用poi,首先下载最新的poi,从http://poi.apache.org,下面是个直接的:http://mirror.candidhosting.com/pub/apache/poi/release/bin/

我下载的是poi-bin--FINAL-20070705.tar.gz,解压后将poi--FINAL-20070705.jar引入路径。

建立相关的java和jsp文件,太多了,看看下图需要建立那些文件(有Database.java,DatabaseImpl.java, ExcelListbox.java, ExcelServlet.java, excel.jsp)

 

下面是各个文件的代码:

Database.java

package edu.bigc.zk.excel;

public class Database {

	private String line1;

	private String line2;

	private String line3;

	private String line4;

	private String line5;

	public String getLine1() {
		return line1;
	}

	public void setLine1(String line1) {
		this.line1 = line1;
	}

	public String getLine2() {
		return line2;
	}

	public void setLine2(String line2) {
		this.line2 = line2;
	}

	public String getLine3() {
		return line3;
	}

	public void setLine3(String line3) {
		this.line3 = line3;
	}

	public String getLine4() {
		return line4;
	}

	public void setLine4(String line4) {
		this.line4 = line4;
	}

	public String getLine5() {
		return line5;
	}

	public void setLine5(String line5) {
		this.line5 = line5;
	}
}

DatabaseImpl.java

package edu.bigc.zk.excel;

import java.util.ArrayList;
import java.util.List;

public class DatabaseImpl {
	public static List<Database> databases = new ArrayList<Database>();

	public static void init() {
		if (databases.size() < 1) {
			for (int i = 0; i < 20; i++) {
				Database database = new Database();
				String s = String.valueOf(i) + "_";
				database.setLine1(s + "1");
				database.setLine2(s + "2");
				database.setLine3(s + "3");
				database.setLine4(s + "4");
				database.setLine5(s + "5");

				databases.add(database);
			}
		}
	}
}

ExcelListbox.java

package edu.bigc.zk.excel;

import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;

public class ExcelListbox extends Listbox {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void onCreate() {

		Listbox listbox = (Listbox) getSpaceOwner().getFellow("excelListbox");

		DatabaseImpl.init();
		for (Database d : DatabaseImpl.databases) {

			Listitem listitem = new Listitem();
			Listcell listcell1 = new Listcell(d.getLine1());
			Listcell listcell2 = new Listcell(d.getLine2());
			Listcell listcell3 = new Listcell(d.getLine3());
			Listcell listcell4 = new Listcell(d.getLine4());
			Listcell listcell5 = new Listcell(d.getLine5());
			listitem.appendChild(listcell1);
			listitem.appendChild(listcell2);
			listitem.appendChild(listcell3);
			listitem.appendChild(listcell4);
			listitem.appendChild(listcell5);
			listbox.appendChild(listitem);
		}
	}

}

ExcelServlet.java

package edu.bigc.zk.excel;

import java.net.URLEncoder;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExcelServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) {
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("new sheet");

		HSSFRow row = sheet.createRow((short) 0);

		String[] cellHeader = new String[] { "序号", "第一行", "第二行", "第三行", "第四行",
				"第五行" };

		for (int i = 0; i < cellHeader.length; i++) {
			HSSFCell cell = row.createCell((short) i);
			cell.setCellValue(new HSSFRichTextString(cellHeader[i]));
		}

		HSSFCellStyle style = getAlignLeftStyle(workbook);
		short rowNum = 1;

		for (Database d : DatabaseImpl.databases) {
			HSSFRow dataRow = sheet.createRow(rowNum);

			HSSFCell cell0 = dataRow.createCell((short) 0);
			cell0.setCellStyle(style);
			cell0.setCellValue(rowNum);

			HSSFCell cell1 = dataRow.createCell((short) 1);
			cell1.setCellValue(new HSSFRichTextString(d.getLine1()));

			HSSFCell cell2 = dataRow.createCell((short) 2);
			cell2.setCellValue(new HSSFRichTextString(d.getLine2()));

			HSSFCell cell3 = dataRow.createCell((short) 3);
			cell3.setCellValue(new HSSFRichTextString(d.getLine3()));

			HSSFCell cell4 = dataRow.createCell((short) 4);
			cell4.setCellValue(new HSSFRichTextString(d.getLine4()));

			HSSFCell cell5 = dataRow.createCell((short) 5);
			cell5.setCellValue(new HSSFRichTextString(d.getLine5()));

			rowNum++;
		}

		try {
			setResponseHeader(response, "zk表.xls");
			workbook.write(response.getOutputStream());
			response.getOutputStream().flush();
			response.getOutputStream().close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private HSSFCellStyle getAlignLeftStyle(HSSFWorkbook wb) {
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
		style.setAlignment(HSSFCellStyle.VERTICAL_CENTER);
		return style;
	}

	private void setResponseHeader(HttpServletResponse response, String fileName)
			throws Exception {
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Disposition", "attachment;filename=\""
				+ URLEncoder.encode(fileName, "UTF-8") + "\"");
		response.setHeader("Cache-Control",
				"must-revalidate, post-check=0, pre-check=0");
		response.setHeader("Pragma", "public");
		response.setDateHeader("Expires", 0);
	}
}

这是一个servlet, 需要在web.xml中对应位置添加下面的代码:

<servlet>
		<servlet-name>ExcelServlet</servlet-name>
		<servlet-class>edu.bigc.zk.excel.ExcelServlet</servlet-class>
</servlet>
<servlet-mapping>
		<servlet-name>ExcelServlet</servlet-name>
		<url-pattern>/ExcelServlet</url-pattern>
</servlet-mapping>

excel.jsp

<%@ page language="java" pageEncoding="UTF-8"%>

<html xmlns:x="http://www.zkoss.org/2005/zul">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>导出excel</title>
</head>
<body>
<x:window id="window" title="导出excel" border="normal" width="100%">
	<x:vbox spacing="10px" width="100%">
		<x:listbox id="excelListbox" width="100%" mold="paging" pageSize="10"
			use="edu.bigc.zk.excel.ExcelListbox">
			<x:listhead sizable="true" width="99%">
				<x:listheader sort="auto" label="第一行" />
				<x:listheader sort="auto" label="第二行" />
				<x:listheader sort="auto" label="第三行" />
				<x:listheader sort="auto" label="第四行" />
				<x:listheader sort="auto" label="第五行" />
			</x:listhead>
			<x:listfoot>
				<x:listfooter label="导出excel" />
				<x:listfooter />
				<x:listfooter />
				<x:listfooter />
				<x:listfooter />
			</x:listfoot>
		</x:listbox>
		<input value="导出Excel" type="button"
			onClick="window.open('<%=request.getContextPath()%>/ExcelServlet','下载','height=200, width=200,top=200, left=200, toolbar=no, menubar=no, scrollbars=no,resizable=no,location=no, status=no ')" />
	</x:vbox>
</x:window>
</body>
</html>

运行截图

发表评论

电子邮件地址不会被公开。

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>