导出word文档 我现在要做,把我查询到的结果导出word文档 本来用脚本语言写好的,但是跳转到后台太过频繁,我的头叫我把生成word文档在后台实现。。那位高手能帮帮忙啊。。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private void exportExcel(ActionContext ctx) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{ HttpServletRequest request=(HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST); //从请求行中获得标题行的名称 String[] titles=(String[])request.getAttribute("titles"); //从请求行中获得数据名称 List dataList=(List)request.getAttribute("dataList"); //创建工作薄实例 workbook=new HSSFWorkbook(); //创建工作表实例 HSSFSheet sheet=workbook.createSheet("sheet1"); if(dataList!=null){ //创建标题行 HSSFRow titleRow=sheet.createRow(0); for(int i=0;i<titles.length;i++){ HSSFCell cell=titleRow.createCell((short)i);//创建数据列 cell.setCellValue(titles[i]);//给单元格赋值 } //填充表格 for(int i=0;i<dataList.size();i++){ HSSFRow dataRow=sheet.createRow(i+1);//创建数据行 Object obj=dataList.get(i);//获得datalist中的第i个值 Method[] methods=obj.getClass().getMethods(); int j=0; for(Method method:methods){ if(method.getName().startsWith("get")&&!method.getName().equals("getClass")){ HSSFCell cell=dataRow.createCell((short)j++); Object value=method.invoke(obj); cell.setCellValue(value.toString()); } } } } } //实现文件下载的方法 @Override protected void doExecute(String arg0, ActionInvocation invocation) throws Exception { //获取ActionContext对象实例 ActionContext ctx=invocation.getInvocationContext(); //通过actioncontext对象实例,获取response对象 HttpServletResponse response=(HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE); OutputStream out=response.getOutputStream(); response.setHeader("Content-disposition", "attachment;filename=Data.xls"); response.setContentType("application/msexcel;charset=UTF-8"); exportExcel(ctx); workbook.write(out); out.flush(); out.close(); } 调用doExecute 这是我当时做的时候一个模版,我也是借花献佛,这个根据需求还得改,但主题不用改,HSSFCell cell=dataRow.createCell((short)j++);估计你得需要改,具体怎么改你自己掂量着改吧 可是你这个是导出Execute的啊。。可是我要导出的是Word啊。。能再那个基础上改吗? 我现在已经有了一个word模板了,我主要是引用那个模板,把我查询到的数据放入Word文档里面。。 http://jeady.blog.hexun.com/13957703_d.html 做过一些Word导出的,如果需要的话,楼主可加我QQ 158115719 关于java 如何控制table中的的每列的宽度,各列总宽度大于table宽度,想用div定一个带滚动条 套打怎么实现 Java面试归来好郁闷ing...请教了!! 看不懂啊 谁来给我讲讲 SpringMVC中导出Excel文件,希望能弹出一个选择框 下面的例子总提示4:无法访问HttpServlet.class,可我已经把HttpServlet.class拷贝到当前文件夹,请问各位高手,问题出在哪里? 请教怎么用button打开一个网页呢 ? 大家都来说说:好的java学习站点(参与有分) 也许是个异想天开的问题…… (急)报表brio无法加载存储过程 关于tomcat中 导入css的问题
HttpServletRequest request=(HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
//从请求行中获得标题行的名称
String[] titles=(String[])request.getAttribute("titles");
//从请求行中获得数据名称
List dataList=(List)request.getAttribute("dataList");
//创建工作薄实例
workbook=new HSSFWorkbook();
//创建工作表实例
HSSFSheet sheet=workbook.createSheet("sheet1");
if(dataList!=null){
//创建标题行
HSSFRow titleRow=sheet.createRow(0);
for(int i=0;i<titles.length;i++){
HSSFCell cell=titleRow.createCell((short)i);//创建数据列
cell.setCellValue(titles[i]);//给单元格赋值
}
//填充表格
for(int i=0;i<dataList.size();i++){
HSSFRow dataRow=sheet.createRow(i+1);//创建数据行
Object obj=dataList.get(i);//获得datalist中的第i个值
Method[] methods=obj.getClass().getMethods();
int j=0;
for(Method method:methods){
if(method.getName().startsWith("get")&&!method.getName().equals("getClass")){
HSSFCell cell=dataRow.createCell((short)j++);
Object value=method.invoke(obj);
cell.setCellValue(value.toString());
}
}
}
}
}
//实现文件下载的方法
@Override
protected void doExecute(String arg0, ActionInvocation invocation)
throws Exception {
//获取ActionContext对象实例
ActionContext ctx=invocation.getInvocationContext();
//通过actioncontext对象实例,获取response对象
HttpServletResponse response=(HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);
OutputStream out=response.getOutputStream();
response.setHeader("Content-disposition", "attachment;filename=Data.xls");
response.setContentType("application/msexcel;charset=UTF-8");
exportExcel(ctx);
workbook.write(out);
out.flush();
out.close();
}
估计你得需要改,具体怎么改你自己掂量着改吧