jxl在web项目中以IO流的形式写入excel文件

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
  </head>
  
  <body>
    This is my JSP page. <br>
    <form action="servlet/Testjxl" method="post">
       <input type="submit" value="到处excel">
    </form>
  </body>
</html>




package cn.itcast.poi;

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

public class SetCellFormat {
	
	//设置第二行的样式
	public WritableCellFormat getsencondFormat() 
    {
        // 设置字体
        WritableFont wf = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);

        // 创建单元格FORMAT
        WritableCellFormat wcf = new WritableCellFormat(wf);
        try {
			wcf.setAlignment(Alignment.CENTRE);
			wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
			wcf.setLocked(true);
			wcf.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
			wcf.setBackground(Colour.GREY_25_PERCENT);
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return wcf;
    }

	//设置标题样式
	public WritableCellFormat gettitleFormat() 
    {
       
        WritableFont wf = new WritableFont(WritableFont.ARIAL, 18, WritableFont.BOLD);
        // 创建单元格FORMAT
        WritableCellFormat wcf = new WritableCellFormat(wf);
        try {
			wcf.setAlignment(Alignment.CENTRE);
			wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
			wcf.setLocked(true);
			wcf.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        //wcf.setBackground(Colour.GREY_25_PERCENT);
        return wcf;
    }

	// 设置所有行的样式
	public WritableCellFormat getallFormat() 
    {

			WritableFont wf = new WritableFont(WritableFont.ARIAL, 10);
			// 创建单元格FORMAT
			WritableCellFormat wcf = new WritableCellFormat(wf);
			try {
				wcf.setAlignment(Alignment.CENTRE);
				wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
				wcf.setLocked(true);
				wcf.setWrap(true); 
				wcf.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
				//wcf.setBackground(Colour.GREY_25_PERCENT);
			} catch (WriteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return wcf;
		
    }
	
	//设置第五列的样式
	public WritableCellFormat getfirdFormat() 
    {

			WritableFont wf = new WritableFont(WritableFont.ARIAL, 10);
			// 创建单元格FORMAT
			WritableCellFormat wcf = new WritableCellFormat(wf);
			try {
				//wcf.setAlignment(Alignment.CENTRE);
				wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
				wcf.setLocked(true);
				wcf.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
				//wcf.setBackground(Colour.GREY_25_PERCENT);
			} catch (WriteException e) {
				e.printStackTrace();
			}
			return wcf;
		
    }
}



package cn.itcast.poi;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

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

import jxl.Workbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class Testjxl extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		try {
			//获取输出流
			OutputStream out = response.getOutputStream();
			System.out.println(out);
			//重置输出流
			response.reset();
			//设置导出Excel报表的导出形式
			response.setContentType("application/vnd.ms-excel");
			
			jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(out);
			jxl.write.WritableSheet ws = wwb.createSheet("表格 1", 0);
			for (int i = 0; i < 7; i++) {
				ws.setColumnView(i + 1, 14);
				if (i == 4)
					ws.setColumnView(i, 50);
			}
			SetCellFormat format = new SetCellFormat();
		    jxl.write.Number labelo = new jxl.write.Number(0, 0, 0, format.getallFormat());
		    jxl.write.Number labe1 = new jxl.write.Number(0, 1, 11, format.getallFormat());
		    jxl.write.Number labe2 = new jxl.write.Number(0, 2, 22, format.getallFormat());
			
			ws.addCell(labelo);
			ws.addCell(labe1);
			ws.addCell(labe2);
			//设置输出形式
			System.setOut(new PrintStream(out));
			
			wwb.write(); // 关闭Excel工作薄对象
			wwb.close();
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
		
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值