欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java导出Excel通用方法的实例详解

程序员文章站 2023-11-24 12:35:52
java导出excel通用方法的实例详解 java导出excel通用方法,只需要一个list 集合。通用方法改进之处踊跃提出 package oa.comm...

java导出excel通用方法的实例详解

java导出excel通用方法,只需要一个list 集合。通用方法改进之处踊跃提出

package oa.common.utils; 
import java.io.outputstream; 
import java.util.list; 
import javax.servlet.http.httpservletresponse; 
import org.apache.struts2.servletactioncontext; 
import java.lang.reflect.field; 
 
import jxl.workbook; 
import jxl.format.alignment; 
import jxl.format.border; 
import jxl.format.borderlinestyle; 
import jxl.format.verticalalignment; 
import jxl.write.label; 
import jxl.write.writablecellformat; 
import jxl.write.writablefont; 
import jxl.write.writablesheet; 
import jxl.write.writableworkbook; 
/*** 
 * @author lsf 
 */ 
public class exportexcel { 
 /*************************************************************************** 
 * @param filename excel文件名称 
 * @param listtitle excel文件第一行列标题集合 
 * @param listcontent excel文件正文数据集合 
 * @return 
 */ 
 public final static string exportexcel(string filename,string[] title, list<object> listcontent) { 
 string result="系统提示:excel文件导出成功!";  
 // 以下开始输出到excel 
 try {   
  //定义输出流,以便打开保存对话框______________________begin 
  httpservletresponse response=servletactioncontext.getresponse(); 
  outputstream os = response.getoutputstream();// 取得输出流    
  response.reset();// 清空输出流    
  response.setheader("content-disposition", "attachment; filename="+ new string(filename.getbytes("gb2312"),"iso8859-1")); 
// 设定输出文件头    
  response.setcontenttype("application/msexcel");// 定义输出类型   
  //定义输出流,以便打开保存对话框_______________________end 
 
  /** **********创建工作簿************ */ 
  writableworkbook workbook = workbook.createworkbook(os); 
 
  /** **********创建工作表************ */ 
 
  writablesheet sheet = workbook.createsheet("sheet1", 0); 
 
  /** **********设置纵横打印(默认为纵打)、打印纸***************** */ 
  jxl.sheetsettings sheetset = sheet.getsettings(); 
  sheetset.setprotected(false); 
 
 
  /** ************设置单元格字体************** */ 
  writablefont normalfont = new writablefont(writablefont.arial, 10); 
  writablefont boldfont = new writablefont(writablefont.arial, 10,writablefont.bold); 
 
  /** ************以下设置三种单元格样式,灵活备用************ */ 
  // 用于标题居中 
  writablecellformat wcf_center = new writablecellformat(boldfont); 
  wcf_center.setborder(border.all, borderlinestyle.thin); // 线条 
  wcf_center.setverticalalignment(verticalalignment.centre); // 文字垂直对齐 
  wcf_center.setalignment(alignment.centre); // 文字水平对齐 
  wcf_center.setwrap(false); // 文字是否换行 
   
  // 用于正文居左 
  writablecellformat wcf_left = new writablecellformat(normalfont); 
  wcf_left.setborder(border.none, borderlinestyle.thin); // 线条 
  wcf_left.setverticalalignment(verticalalignment.centre); // 文字垂直对齐 
  wcf_left.setalignment(alignment.left); // 文字水平对齐 
  wcf_left.setwrap(false); // 文字是否换行   
  
 
  /** ***************以下是excel开头大标题,暂时省略********************* */ 
  //sheet.mergecells(0, 0, colwidth, 0); 
  //sheet.addcell(new label(0, 0, "xx报表", wcf_center)); 
  /** ***************以下是excel第一行列标题********************* */ 
  for (int i = 0; i < title.length; i++) { 
  sheet.addcell(new label(i, 0,title[i],wcf_center)); 
  }   
  /** ***************以下是excel正文数据********************* */ 
  field[] fields=null; 
  int i=1; 
  for(object obj:listcontent){ 
    fields=obj.getclass().getdeclaredfields(); 
    int j=0; 
    for(field v:fields){ 
      v.setaccessible(true); 
      object va=v.get(obj); 
      if(va==null){ 
        va=""; 
      } 
      sheet.addcell(new label(j, i,va.tostring(),wcf_left)); 
      j++; 
    } 
    i++; 
  } 
  /** **********将以上缓存中的内容写到excel文件中******** */ 
  workbook.write(); 
  /** *********关闭文件************* */ 
  workbook.close();   
 
 } catch (exception e) { 
  result="系统提示:excel文件导出失败,原因:"+ e.tostring(); 
  system.out.println(result);  
  e.printstacktrace(); 
 } 
 return result; 
 } 
} 

测试:

/** 
 * 导出excel 
 * @return 
 */ 
public string excelpage(){ 
  exportexcel excel=new exportexcel(); 
  string str=""; 
  try { 
    str = new string(gethttp.getrequest().getparameter("wineorg.orgname").getbytes("iso8859-1"),"utf-8"); 
  } catch (unsupportedencodingexception e) { 
    // todo auto-generated catch block 
    e.printstacktrace(); 
  } 
  wineorg.setorgname(str); 
  list<object> li=service.exportexcel(wineorg); 
  string[] title={"机构id","会员编号","类别","名称","省id","省名称","城市id","城市名称","详细地址","联系人","性别","联系手机","联系电话","传真","邮箱","qq","生日","积分","客户等级","现金账户余额","结算方式","客户类型","购买次数","购买支数","创建人id","创建人姓名","create_time","del","sts","备注","负责人id","负责人姓名","审核标识","审核人id ","审核人姓名","审核日期","分配人id","分配人姓名","分配日期","修改人id","修改人姓名 ","修改时间"}; 
  excel.exportexcel("客户资料信息.xls",title, li);  
  return success; 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!