songsong_b 发表于 2013-1-15 03:00:53

Export Excel

package com.embraiz.web;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.*;
import java.io.*;
import jxl.*;
import jxl.write.*;

import com.embraiz.bo.sales.ObjClientCompany;
import com.embraiz.common.util.SmartDate;

import java.sql.SQLException;
import java.util.*;

import com.embraiz.bo.core.ObjUser;
import com.embraiz.common.sql.SQLBridge;

public class ExcelForClientCompany extends HttpServlet {

/**
* Constructor of the object.
*/
private SQLBridge sqlBridge;
public ExcelForClientCompany() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

try
{
sqlBridge = new SQLBridge();
sqlBridge.setConnectionName("proxool.erppool");
sqlBridge.setConnectionSwitch("on");
response.setContentType("text/html");
String userId=request.getParameter("userId");
String objKeyword=request.getParameter("objKeyword");
    String clientCompanyName=request.getParameter("clientCompanyName");
    String assignedTo=request.getParameter("assignedTo");
    String generalLine=request.getParameter("generalLine");
    String fax=request.getParameter("fax");
    String brand=request.getParameter("brand");
    String filterLetter=request.getParameter("filterLetter");
   
ObjClientCompany searchPO=new ObjClientCompany();
searchPO.setObjTitle(clientCompanyName);
      searchPO.setBrandName(brand);
      searchPO.setTelephone(generalLine);
      searchPO.setFax(fax);
      if(assignedTo!=null)
      {
      searchPO.setAllocation(new Integer(assignedTo));
      
      }
      searchPO.setObjKeyword(objKeyword);
      searchPO.setFilterLetter(filterLetter);



List<ObjClientCompany> list=searchPO.list(sqlBridge,userId);


response.setContentType("application/vnd.ms-excel;charset=gb2312");
response.setHeader("Content-disposition",   "attachment;   filename=\"ClientCompany_Form.xls\"");
OutputStream os = response.getOutputStream();
WritableWorkbook wwb = Workbook.createWorkbook(os);
WritableCellFormat bColor = new WritableCellFormat();
bColor.setBackground(jxl.format.Colour.ICE_BLUE);   
try
{
WritableSheet wsheet = wwb.createSheet("page1",0);
Label label=new Label(0,0,"Client company",bColor);
wsheet.addCell(label);
label=new Label(1,0,"Assigned to",bColor);
wsheet.addCell(label);
label=new Label(2,0,"Brand",bColor);
wsheet.addCell(label);
label=new Label(3,0,"General Line",bColor);
wsheet.addCell(label);
label=new Label(4,0,"Create Date",bColor);
wsheet.addCell(label);
int i;
int j;

for(i=1;i<=list.size();i++){
j=0;
label=new Label(j,i,list.get(i-1).getObjTitle());
wsheet.addCell(label);
j++;
label=new Label(j,i,list.get(i-1).getAssignUserName());
wsheet.addCell(label);
j++;
label=new Label(j,i,list.get(i-1).getBrandName());
wsheet.addCell(label);
j++;
label=new Label(j,i,list.get(i-1).getTelephone());
wsheet.addCell(label);
j++;
label=new Label(j,i,SmartDate.DateToStr(list.get(i-1).getCreateDate(),"yyyy-MM-dd HH:mm").toString());
wsheet.addCell(label);
j++;
}

wsheet.setRowView(0,400);
wsheet.setColumnView(0,20);//设置列宽
wsheet.setColumnView(1,20);
wsheet.setColumnView(2,20);
wsheet.setColumnView(3,20);
wsheet.setColumnView(4,20);
wwb.write();
wwb.close();
os.close();
response.flushBuffer();
}catch(Exception e){
   System.out.println(e);
}}catch(Exception e)
{

}finally
{
try {
sqlBridge.setConnectionSwitch("off");
} catch (Exception e) {
e.printStackTrace();
}
}
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println("<HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println("<BODY>");
out.print("    This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println("</BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}

}
页: [1]
查看完整版本: Export Excel