jsp导出excel jxl

ExcelUtil .java

 

import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WriteException;

public class ExcelUtil {
	/**
	 * 返回大标题格式
	 */
	public static WritableCellFormat createWcfTitle() {
		WritableFont wfc_big = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD, false,
				UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
		WritableCellFormat wcf_title = new WritableCellFormat(wfc_big);
		try {
			wcf_title.setBorder(Border.ALL, BorderLineStyle.THIN);
			wcf_title.setAlignment(Alignment.CENTRE);
			wcf_title.setVerticalAlignment(VerticalAlignment.CENTRE);
			wcf_title.setWrap(true);

		} catch (WriteException e) {
			e.printStackTrace();
		}
		return wcf_title;
	}

	/**
	 * 返回正文格式
	 */
	public static WritableCellFormat createWcfText() {
		WritableFont wfc_small = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false,
				UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
		WritableCellFormat wcf_text = new WritableCellFormat(wfc_small);
		try {
			wcf_text.setBorder(Border.ALL, BorderLineStyle.THIN);
			wcf_text.setAlignment(Alignment.LEFT);
                        wcf_text.setWrap(true);
			wcf_text.setVerticalAlignment(VerticalAlignment.CENTRE);
		} catch (WriteException e) {
			e.printStackTrace();
		}
		return wcf_text;
	}
}

 

exportExcel.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@page import="java.io.*"%>
<%@page import="jxl.*"%>
<%@page import="com.xxx.ExcelUtil"%>

<%
	response.reset();
	response.setContentType("application/vnd.ms-excel;charset=UTF-8");
	response.setHeader("Content-Disposition", "attachment;filename=" + new String("Excel文件.xls".getBytes("gb2312"), "iso8859-1"));

	OutputStream os = response.getOutputStream();

	WritableWorkbook wwb = null;
	WritableCellFormat wcf_title = ExcelUtil.createWcfTitle();
	WritableCellFormat wcf_text = ExcelUtil.createWcfText();
	wwb = Workbook.createWorkbook(os);//将 WritableWorkbook 写入到输出流
	WritableSheet ws = wwb.createSheet("sheet1", 0);//创建第一个sheet
	ws.mergeCells(0, 0, 3, 0);//合并单元格

	Label lbl = null;
	lbl = new Label(0, 0, "大标题" + sumMonth, wcf_title);
	ws.addCell(lbl);

	lbl = new Label(0, 1, "列标题1", wcf_text);
	ws.addCell(lbl);
	lbl = new Label(1, 1, "列标题2", wcf_text);
	ws.addCell(lbl);
	lbl = new Label(2, 1, "列标题3", wcf_text);
	ws.addCell(lbl);
	lbl = new Label(3, 1, "列标题4", wcf_text);
	ws.addCell(lbl);

	for (int i = 0; i < 3; i++) //设置宽度
		ws.setColumnView(i, 15);

	os.flush();
	wwb.write();
	wwb.close();
	os.close();
%>

 

补充一重要的用法

合并单元格:

WritableSheet.mergeCells(int col1, int row1, int col2, int row2)

   单元格(col1,row1)到对角单元格(col2,row2)之间的合并为一个单元格

 单元格换行

用\n换行( 前置条件是wcf_text.setWrap(true);)
Label lbl = new Label(1, 3, "星\n期", wcf_text);

 

 

 

jsp导出txt

方法1同上用response.getOutputStream (跳行用\r\n)

response.setContentType("text/html;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + new String("文件名_".getBytes("gb2312"), "iso8859-1") + ".txt");
OutputStream os = response.getOutputStream();
os.write("abc".getBytes());

 

方法2

 response.setContentType("application/octet-stream");
 response.addHeader("Content-Disposition","attachment; filename=1.txt");
 out.print("First line text.\r\n");
 out.print("Second line.\r\n");

    <%@ page 。。。%>出现一次就是一空行....

 

共享、备忘...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值