1. 程式人生 > >jxl在web專案中以IO流的形式寫入excel檔案

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

}