ITeye博客

poi导出excel清晰 步骤详解

keguan_li · 2016-10-14 14:08 · java · 编程语言 · Markdown

poi导出excel清晰 步骤详解

一、本次生成excel应用场景描述如下: 用户需要下载某个业务大量数据 友情链接(poi解析excel):http://patronli.iteye.com/blog/2330524

二、示例代码如下所示:


package com.patronli.action;

import java.util.ArrayList;
import java.util.List;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.apache.struts2.ServletActionContext;

import com.sssoft.framework.web.report.xls.ExcelSupport;

public class testAction {
	public void doDownLoad() {
		// 根据用户条件找到数据源,例如下所示
		List<patronli> list = new ArrayList<patronli>();
		// 定义excel的名字
		WritableWorkbook workbook = ExcelSupport.getWorkbook("excel模板",
				ServletActionContext.getResponse());
		// 定义当前sheet的名字及位置(在当前workbook创建sheet)
		WritableSheet sheet = workbook.createSheet("sheet1", 0);
		// 定义数据的字体
		WritableFont font1 = new WritableFont(WritableFont.createFont("微软雅黑"),
				10, WritableFont.BOLD);
		WritableCellFormat wcf = new WritableCellFormat(font1);
		// 定义excel的表头
		String[] pjTitles1 = { "序号", "时间", "订单编号" };
		try {
			for (int i = 0; i < pjTitles1.length; i++) {
				// 第几列,第几行,字段值,字体属性等
				sheet.addCell(new Label(i, 0, pjTitles1[i], wcf));
			}
			for (int i = 0; i < list.size(); i++) {
				patronli temp = list.get(i);
				String num = String.valueOf(i + 1);
				sheet.addCell(new Label(0, i + 1, num, wcf));
				sheet.addCell(new Label(2, i + 1, temp.getts(), wcf));
				sheet.addCell(new Label(3, i + 1, temp.getOrder(), wcf));
			}
			ExcelSupport.writeWorkbook(workbook);
		} catch (RowsExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。