首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java上载文件以及DOM输出为EXCEL

2012-09-17 
Java下载文件以及DOM输出为EXCELpublic void doGet( HttpServletRequest request, HttpServletResponse re

Java下载文件以及DOM输出为EXCEL

public void doGet( HttpServletRequest request, HttpServletResponse response )
        throws ServletException, IOException
    {
     try
     {

   String filename=domtoExcel(dom,hs); 
   //response.setContentType("application/vnd.ms-excel"); //该头直接在IE中打开Excel文件

   response.setContentType("application/octet-stream;charset=GB2312");
   String downloadname="Detail.xls";
   response.setHeader("Content-Disposition","attachment; filename="+downloadname);

   if (filename!=null)
   {
    File file1=new File(filename);
    FileInputStream is=new FileInputStream(file1);
    byte []bbb=new byte[(int)file1.length()];
   
    System.out.println("------bbb.length:"+bbb.length);
    is.read(bbb);
    is.close();
    OutputStream out = response.getOutputStream();
    out.write(bbb);
    out.close();
    response.setStatus(response.SC_OK);
    file1.delete();
   }
   else
   {
    System.out.println("----createExcelFile error!");
   }
  }
  catch (Exception e)
  {
   System.out.println("----------ex in servlet:"+e);
   e.printStackTrace();
  }
    }

 


 public String domtoExcel(Document dom,Hashtable hs)
     {
    String title=(String)hs.get("title");

      String filename=getFilename();
      if (filename==null) return null;
      filename="data/"+filename;
      boolean isOK=false;
      try
      {
       if(dom==null)
       {
        System.out.println("dom is null");
       }
    dom.normalize();
    Node root=(Node)dom.getDocumentElement();
    NodeList nodeRowList=root.getChildNodes();
   

    WritableWorkbook workbook=Workbook.createWorkbook(new File(filename));
    /************创建工作表*************/  
   

    WritableSheet sheet=workbook.createSheet(title,0);    
    //setHeaderFooter(sheet2,hs);
    setHeaderFooter(sheet,hs);  
    WritableFont   normalFont= new WritableFont(WritableFont.ARIAL,10);  
    WritableCellFormat wcf_left=new WritableCellFormat(normalFont);  
    wcf_left.setBorder(Border.ALL,BorderLineStyle.THIN);   //线条  
    wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE);   //垂直对齐  
    wcf_left.setAlignment(Alignment.LEFT);  
//    wcf_left.setWrap(false);   //是否换行  
   
    //添加行宽设置
    Map mapWidth=(Map)hs.get("widthMap");
    if(mapWidth!=null&&mapWidth.size()>0){
     Iterator it = mapWidth.keySet().iterator();
     while(it.hasNext()){
      String str = (String)it.next();
      sheet.setColumnView(Integer.parseInt(str), Integer.parseInt((String)mapWidth.get(str)));
     }
    }

    if (nodeRowList.getLength()>0) //dom is not null
    {
     Node nodeRowTitle=nodeRowList.item(0);
     NodeList nodeItemList=nodeRowTitle.getChildNodes();
     for (int j=0;j<nodeItemList.getLength();j++)
     {
      Node nodeItem=nodeItemList.item(j);
      sheet.addCell(new  Label(j,0,nodeItem.getChildNodes().item(0).getChildNodes().item(0).getNodeValue(),
       wcf_left));    
     }
    }
   
    for (int i=0;i<nodeRowList.getLength();i++)
    {
     Node nodeRow=nodeRowList.item(i);
     NodeList nodeItemList=nodeRow.getChildNodes();
     for (int j=0;j<nodeItemList.getLength();j++)
     {
      Node nodeItem=nodeItemList.item(j);
      String itemtype=nodeItem.getChildNodes().item(2).getChildNodes().item(0).getNodeValue(); //itemtype
      itemtype=itemtype.trim();
      NodeList temp=nodeItem.getChildNodes().item(1).getChildNodes();
      String str1 = "";
      if(nodeItem.getChildNodes().item(1).getChildNodes().item(0)==null)
      {
       str1="";
      }
      else
      {
        str1=nodeItem.getChildNodes().item(1).getChildNodes().item(0).getNodeValue();
      }
     
      sheet.addCell(new  Label(j,i+1,str1==null?"":str1,wcf_left));


         
     }
    }
    String footerLeft=(String)hs.get("footerLeft");
    String footerCenter=(String)hs.get("footerCenter");
    String footerRight=(String)hs.get("footerRight");

    int row=nodeRowList.getLength()+1;
       WritableCellFormat wcf_none = new WritableCellFormat();
       wcf_none.setBorder(Border.NONE, BorderLineStyle.THIN);
    sheet.addCell(new Label(0,row,footerLeft==null ? "":footerLeft,wcf_none));
    sheet.addCell(new Label(2,row,footerCenter==null ? "":footerCenter,wcf_none));
    sheet.addCell(new Label(4,row,footerRight==null ? "":footerRight,wcf_none));

    workbook.write();
    workbook.close();
    isOK=true;
    System.out.println("--------dom export ok-------");
      }
      catch (Exception e)
      {
       System.out.println("------exception in dom2Csv:"+e);
       e.printStackTrace();
      }
     
      if (isOK)
      {
       return filename;
      }
      else
      {
       return null;
      }
     
     }

 

热点排行