导出excel
今天准备做报表但找了一些网上的资料也比较零碎。今天特意整理一下。现在开始吧 需要的文件有DateUtil.java ,里面有个方法要调用。 /** * 获取指定格式的当前日期字符串 * @param patten 格式化字符串 * @return */ public static String getDate(String patten) { SimpleDateFormat sf = new SimpleDateFormat(patten); return sf.format(new Date()); } public static String getTime() {
return DateUtil.getDate("yyyyMMddhhmmss");
}
然后就是我的exporting.jsp文件
<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
<%@ page import="com.kysc.kyjh.plotManage.comm.DateUtil"%> <jsp:useBean id="excel" scope="request" class="com.kysc.kyjh.planManage.action.InsertPlanInfoAction"/> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
<title>My JSP 'exporting.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
if(excel.export(request,application,timeName)){
%>
<input type="hidden" name="filename" id="filename" value="${pageContext.request.contextPath}/<%=timeName%>.xls"/>
<script language="JavaScript" type="text/javascript">
document.location=document.getElementById("filename").value;
<% }
else{
%>
<%} %>
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.sql.ResultSet; import java.sql.SQLException;
import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import org.apache.struts.upload.FormFile;
import com.kysc.kyjh.planManage.DAO.InsertPlanInfoDAO; import com.kysc.kyjh.planManage.DAO.SelectPlanInfoDAO; import com.kysc.kyjh.planManage.DAO.UpdatePlanInfoDAO; import com.kysc.kyjh.planManage.form.PlanForm; import com.kysc.kyjh.plotManage.DAO.InsertPlotInfoDAO; import com.kysc.kyjh.plotManage.DAO.SelectPlotInfoDAO; import com.kysc.kyjh.plotManage.comm.DateUtil; import com.kysc.kyjh.plotManage.comm.StringUtil; import com.kysc.kyjh.plotManage.form.DepartUserForm; import com.kysc.kyjh.plotManage.form.PlotForm; import java.util.*;
import jxl.Workbook; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook;
/**
-
@author Administrator * */ public class InsertPlanInfoAction extends DispatchAction { private Vector content=null;//用于存放所要查询的记录
private ResultSet rs=null; private String title={"序号","项目名称","计划名称","项目类型","项目密级","开始时间","结束时间"}; private String targetFile=""; public Vector getContent(){ Vector outter=new Vector(); SelectPlanInfoDAO select =new SelectPlanInfoDAO(); SelectPlotInfoDAO selectplot=new SelectPlotInfoDAO(); List<PlanForm> plans= select.selectPlanInfo(); Iterator<PlanForm> it=plans.iterator(); while(it.hasNext()){ PlanForm pf=it.next(); Vector inner=new Vector(); String sql="select * from kyjh_plot_info where plot_id="+pf.getPlot_id(); List<PlotForm> list= selectplot.selectPlotInfoByCondition(sql); if(list.size()!=0){ PlotForm plot=list.get(0); pf.setItem_name(plot.getItem_name()); pf.setItem_type(plot.getItem_type()); pf.setItem_secret(plot.getItem_secret()); pf.setStart_date(plot.getStart_date()); pf.setEnd_date(plot.getEnd_date()); inner.add(plot.getItem_name()); inner.add(pf.getPlan_name()); inner.add(""+plot.getItem_type()); inner.add(""+plot.getItem_secret()); inner.add(plot.getStart_date()); inner.add(plot.getEnd_date()); outter.add(inner); } } return outter; } public boolean export(HttpServletRequest request,ServletContext context,String timename){ targetFile+="/"+timename+".xls"; content=this.getContent();//以向量的形式存放所有的记录 String path=context.getRealPath(targetFile); request.getSession().setAttribute("filepath",path); try{ Vector inner=null; String value="";//存放在cell中的文本值 int num=0;//存放在cell中的数字值 OutputStream os=new FileOutputStream(path); WritableWorkbook workbook=Workbook.createWorkbook(os);//创建工作薄 WritableSheet worksheet=workbook.createSheet("record",0);//创建第一个工作表,name:工作表名称 Label label=null;//用于写入文本内容到工作表中去 jxl.write.Number nmb=null;//用于写入数值到工作表中去 //开始写入第一行,即标题栏 for(int i=0;i<title.length;i++){ label=new Label(i,0,title*);//参数依次代表列数、行数、内容 worksheet.addCell(label);//写入单元格 } //开始写入内容 for(int i=0;i<content.size();i++){ inner=(Vector)content.get(i);//获取一条记录 label=new Label(0,i+1,""+(i+1)); worksheet.addCell(label); for(int j=0;j<inner.size();j++){ //一个一个字段的放入excel中去 value=(String)inner.get(j); label=new Label(j+1,i+1,value); worksheet.addCell(label); } } workbook.write(); workbook.close(); } catch(Exception e){ e.printStackTrace(); return false; } return true; } public void export(OutputStream os){ content=this.getContent();//以向量的形式存放所有的记录 try{ Vector inner=null; String value="";//存放在cell中的文本值 int num=0;//存放在cell中的数字值 WritableWorkbook workbook=Workbook.createWorkbook(os);//创建工作薄 WritableSheet worksheet=workbook.createSheet("record",0);//创建第一个工作表,name:工作表名称 //WritableSheet worksheet=workbook.createSheet(name,0);//创建第一个工作表,name:工作表名称 Label label=null;//用于写入文本内容到工作表中去 jxl.write.Number nmb=null;//用于写入数值到工作表中去 //开始写入第一行,即标题栏 for(int i=0;i<title.length;i++){ label=new Label(i,0,title*);//参数依次代表列数、行数、内容 worksheet.addCell(label);//写入单元格 } //开始写入内容 for(int i=0;i<content.size();i++){ inner=(Vector)content.get(i);//获取一条记录 for(int j=0;j<inner.size();j++){ //一个一个字段的放入excel中去 if(j==inner.size()-1){//插入的数值 Integer num_=(Integer)inner.get(j); num=num_.intValue(); nmb=new jxl.write.Number(j,i+1,num); worksheet.addCell(nmb); } else{ value=(String)inner.get(j); label=new Label(j,i+1,value); worksheet.addCell(label); } } } workbook.write(); workbook.close(); } catch(Exception e){ e.printStackTrace(); //return false; } //return true; }public ActionForward PlanInfoExportExcel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
System.out.println("PlanInfoExportExcel OK"); targetFile+="/"+DateUtil.getTime()+".xls"; String path=request.getRealPath(targetFile); content=this.getContent(); try{ Vector inner=null; String value="";//存放在cell中的文本值 int num=0;//存放在cell中的数字值 OutputStream os=new FileOutputStream(path); WritableWorkbook workbook=Workbook.createWorkbook(os);//创建工作薄 WritableSheet worksheet=workbook.createSheet("record",0);//创建第一个工作表,name:工作表名称 Label label=null;//用于写入文本内容到工作表中去 jxl.write.Number nmb=null;//用于写入数值到工作表中去 //开始写入第一行,即标题栏 for(int i=0;i<title.length;i++){ label=new Label(i,0,title*);//参数依次代表列数、行数、内容 worksheet.addCell(label);//写入单元格 } //开始写入内容 for(int i=0;i<content.size();i++){ inner=(Vector)content.get(i);//获取一条记录 for(int j=0;j<inner.size();j++){ //一个一个字段的放入excel中去 if(j==inner.size()-1){//插入的数值 Integer num_=(Integer)inner.get(j); num=num_.intValue(); nmb=new jxl.write.Number(j,i+1,num); worksheet.addCell(nmb); } else{ value=(String)inner.get(j); label=new Label(j,i+1,value); worksheet.addCell(label); } } } workbook.write(); workbook.close(); }catch(Exception e){ System.out.println(e.getMessage()); } return null;} }
还没整理,的确没有考虑。
繁琐,你的大数据量的问题考虑了吗?
整得太复杂了。
spring不是号称有excel试图,换个试图view看看。
这个Action整的太复杂了!
有什么特别地方?前段时间有讨论过导出的数量大的情况.