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+"/";
%>



  
    
    
    My JSP 'index.jsp' starting page
	
	
	    
	
	
  
  
  
    This is my JSP page. 




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);
	}

}


你可能感兴趣的:(Java基础)