fireinjava 发表于 2013-1-15 02:25:32

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.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);//创建第一个sheetws.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)之间的合并为一个单元格
 
 
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 。。。%>出现一次就是一空行....
 

共享、备忘...
页: [1]
查看完整版本: jsp导出excel jxl