guanxf

我的博客:http://blog.sina.com.cn/17learning

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  71 随笔 :: 1 文章 :: 41 评论 :: 0 Trackbacks
1、请求下载地址:
try {
//执行检索
cmsSupSubmitSiteStatBeanList = cmsSupSubmitSiteInfoMngService.govStat(condition);
//根据条件查找
cmsSupSubmitSiteInfoMngBeanList = cmsSupSubmitSiteInfoMngService.findByConditionStat(condition);
//临时文件位置
String path=this.getServletConfig().getServletContext().getRealPath("\\upload\\temp");
File ftemp=new File(path);
if (!ftemp.exists()) {
ftemp.mkdirs();//不存在则创建
}
//生成临时文件名
String saveFilename = DateUtil.formatNowDateTime("yyyyMMddHHmmssSSS")+getNewName()+ ".csv";
WritableWorkbook book = Workbook.createWorkbook(new File(path + "\\"+saveFilename));// 创建excel文件
// 生成名为“第一页”的工作表,参数0表示这是第一页
WritableSheet sheet = book.createSheet("网站信息统计表", 0);
// 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
//标题
String[] title1 = {"单位名称"
,"1月"
,"2月"
,"3月"
,"4月"
,"5月"
,"6月"
,"7月"
,"8月"
,"9月"
,"10月"
,"11月"
,"12月"
,"总报送量"
,"报送率"
,"分数"
,"加减分"
,"总分数"
};
//表头
for(int i=0;i<title1.length;i++){
//第n列第一行标识表头
Label label = new Label(i, 0, title1[i]);
sheet.addCell(label); //将定义好的单元格添加到工作表中 
}
//内容
for (int i = 0; i < cmsSupSubmitSiteStatBeanList.size(); i++) {
CmsSupSubmitSiteStatBean bean = cmsSupSubmitSiteStatBeanList.get(i);
//内容
String[] rs1 = {  bean.getDeptName()
,String.valueOf(bean.getUsed01()) + "/" + String.valueOf(bean.getSup01())
,String.valueOf(bean.getUsed02()) + "/" + String.valueOf(bean.getSup02())
,String.valueOf(bean.getUsed03()) + "/" + String.valueOf(bean.getSup03())
,String.valueOf(bean.getUsed04()) + "/" + String.valueOf(bean.getSup04())
,String.valueOf(bean.getUsed05()) + "/" + String.valueOf(bean.getSup05())
,String.valueOf(bean.getUsed06()) + "/" + String.valueOf(bean.getSup06())
,String.valueOf(bean.getUsed07()) + "/" + String.valueOf(bean.getSup07())
,String.valueOf(bean.getUsed08()) + "/" + String.valueOf(bean.getSup08())
,String.valueOf(bean.getUsed09()) + "/" + String.valueOf(bean.getSup09())
,String.valueOf(bean.getUsed10()) + "/" + String.valueOf(bean.getSup10())
,String.valueOf(bean.getUsed11()) + "/" + String.valueOf(bean.getSup11())
,String.valueOf(bean.getUsed12()) + "/" + String.valueOf(bean.getSup12())
,String.valueOf(bean.getTolUsed()) + "/" + String.valueOf(bean.getTolSup())
,String.valueOf(bean.getUsedRate()) + "%"
,String.valueOf(bean.getPoint())
,String.valueOf(bean.getPmPoint())
,String.valueOf(bean.getTolPoint())
};
//内容从第二行开始打印
for (int j = 0; j < rs1.length; j++) {
Label label = new Label(j, i+1, rs1[j]);
 sheet.addCell(label);
}
}
// 打印详细========================================================================================
String[] stDtl = {  "单位名称"
,"标题"
,"加减分"
,"报送时间"
};
WritableSheet sheet2 = book.createSheet("网站信息采用标题", 0);
// 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
//标题
//表头
for(int i=0;i<stDtl.length;i++){
//第n列第一行标识表头
Label labe2 = new Label(i, 0, stDtl[i]);
sheet2.addCell(labe2);
}
//内容
String titleVar="";
int flagNum=0;
for( int i = 0; i < cmsSupSubmitSiteInfoMngBeanList.size(); i ++ ){
CmsSupSubmitSiteInfoMngBean bean = cmsSupSubmitSiteInfoMngBeanList.get(i);
String[] rs2 = {bean.getSpDeptName()
,bean.getSupTitle()
,String.valueOf(bean.getMsgPmPoint())
,bean.getAddDate()
};
if(!titleVar.equals(rs2[0])){
for (int x =0; x < rs2.length; x++) {
Label labeVar2 = new Label(x, i+1, rs2[x]);
sheet2.addCell(labeVar2);
}
   }else{
    //内容从第二行开始打印
       //sheet.mergeCells(int col1,int row1,int col2,int row2);//左上角到右下角    
        sheet.mergeCells(0,1, 0,flagNum);//左上角到右下角     ,列,行,列,行
for (int j =1; j < rs2.length; j++) {
Label labe2 = new Label(j, i+1, rs2[j]);
sheet2.addCell(labe2);
}
   }
flagNum++;
titleVar=rs2[0];
}
// // 将定义好的单元格添加到工作表中
// /*
// * 生成一个保存数字的单元格 必须使用Number的完整包路径,否则有语法歧义 单元格位置是第二列,第一行,
// 值为789.123
// */
// // jxl.write.Number number = new jxl.write.Number( 1 , 0 , 555.12541 );
// // sheet.addCell(number);
// 写入数据并关闭文件
book.write();
book.close();
// 将生成的文件下载
AttUploadsServlet servlet=new AttUploadsServlet();
servlet.downLoadFile(req, resp, "网站信息统计.csv", path + "\\" + saveFilename);
} catch (Exception e) {
System.out.println(e);
}

2、下载附件:
/**
* 文档下载
* @param request 
* @param response
* @param fileName 文件名
* @param attachment -文件路径
* @return
*/
public boolean downLoadFile(HttpServletRequest request,HttpServletResponse response
,String fileName,String attachment) {
try
{
String filepath =attachment;
File file = new File(filepath);
if(!file.exists())
{
return false;
//throw new Exception(filepath+"文件未找到!");
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[1024];
int len = 0;
response.reset();                                            //非常重要
//纯下载方式
response.setContentType("application/x-msdownload"); 
response.setHeader("Content-Disposition", "attachment; filename=" 
+ (new String(fileName.getBytes("gb2312"),"ISO-8859-1"))); 
OutputStream out = response.getOutputStream();
while((len = br.read(buf)) >0)
out.write(buf,0,len);
out.flush();
br.close();
return true;
}
catch(Exception ex)
{
log.info(ex.getMessage());
return false;
}
}

多学一点:划服务器下载附件
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.*"%>
<%@page import="java.io.File"%>
<%@page import="java.io.OutputStream"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.net.URL"%>
<%@page import="java.net.URLConnection"%>
<!-- 以上这行设定本网页为Word格式的网页 -->  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<%
   String refFilePath= request.getRealPath(new String(request.getParameter("fileSrc").getBytes("ISO-8859-1"),"UTF-8"));
   //String docName = new String(request.getParameter("fileName").getBytes("ISO-8859-1"),"UTF-8");
  request.setCharacterEncoding("UTF-8");
  String docName = request.getParameter("fileName");
  try{
        /* 创建输入流 */  
         InputStream is = this.getClass().getClassLoader().getResourceAsStream("project.properties"); 
        Properties p = new Properties();
      try {
     p.load(is);       //Properties 对象已生成,包括文件中的数据
      }catch(IOException e){
       e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
      }
      
      String refFp=p.getProperty("xzql.refFilePath");
        URL ul=new URL(refFp+new String(request.getParameter("fileSrc").getBytes("ISO-8859-1"),"UTF-8"));
        URLConnection conn=ul.openConnection();
        InputStream inStream = conn.getInputStream();
        String disName = java.net.URLEncoder.encode(docName, "UTF-8");  
        response.reset();  
        response.setContentType("application/x-msdownload");  
        response.addHeader("Content-Disposition",  
                "attachment; filename=\"" + disName + "\"");  
         
        
        byte[] buf = new byte[4096];  
        /* 创建输出流 */  
        ServletOutputStream servletOS = response.getOutputStream();  
        int readLength;
        int alllength=0;
        while (((readLength = inStream.read(buf)) != -1)) {  
            servletOS.write(buf, 0, readLength); 
            alllength+= readLength;
        }
        response.setContentLength(alllength); 
        inStream.close();  
        servletOS.flush();  
        servletOS.close();  
   }catch(Exception e){
  out.print("文件不存在! ");
  e.printStackTrace();
  %> 
  </html>

2).struts2下载Excel:
http://blog.csdn.net/weinianjie1/article/details/5941042





posted on 2013-10-14 01:46 管先飞 阅读(496) 评论(0)  编辑  收藏 所属分类: Java技术

只有注册用户登录后才能发表评论。


网站导航: