日期:2014-05-20  浏览次数:20700 次

Service层怎么写导出Excel方法???
已经跟dao连接:private HardwareDao dao;
导出方法:
public String exportExcel() {
HSSFWorkbook wb=new HSSFWorkbook();
  HSSFSheet sheet=wb.createSheet("计算机信息表");
  HSSFRow row=sheet.createRow((int)0);
  HSSFCellStyle style=wb.createCellStyle();
  style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
 
  HSSFCell cell=row.createCell((short)0);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("计算机名");
  cell.setCellStyle(style);
  cell=row.createCell((short)1);
  cell.setCellValue("IP");
  cell.setCellStyle(style);
  cell=row.createCell((short)2);
  cell.setCellValue("MAC");
  cell.setCellStyle(style);
  cell=row.createCell((short)3);
  cell.setCellValue("操作系统版本");
  cell.setCellStyle(style);
  cell=row.createCell((short)4);
  cell.setCellValue("网卡");
  cell.setCellStyle(style);
  cell=row.createCell((short)5);
  cell.setCellValue("内存");
  cell.setCellStyle(style);
  cell=row.createCell((short)6);
  cell.setCellValue("硬盘");
  cell.setCellStyle(style);
  cell=row.createCell((short)7);
  cell.setCellValue("CPU");
  cell.setCellStyle(style);
  cell=row.createCell((short)8);
  cell.setCellValue("显卡");
  cell.setCellStyle(style);
   
  List<PcHardwareInfo> list=dao.getAllPCInfo();
   
  PcHardwareInfo phw;
  row=sheet.createRow((int)1);
  phw=(PcHardwareInfo) list.get(1);
  row.createCell((short)0).setCellValue(phw.getPcname());
  row.createCell((short)1).setCellValue(phw.getIp());
  row.createCell((short)2).setCellValue(phw.getMac());
  row.createCell((short)3).setCellValue(phw.getOs());
  row.createCell((short)4).setCellValue(phw.getNet());
  row.createCell((short)5).setCellValue(phw.getMemory());
  row.createCell((short)6).setCellValue(phw.getHarddisk());
  row.createCell((short)7).setCellValue(phw.getCpu());
  row.createCell((short)8).setCellValue(phw.getGraphics());
   
  try{
  FileOutputStream fout=new FileOutputStream("F:/计算机设备信息");
  wb.write(fout);
  fout.close();
  }catch(Exception e){
  e.printStackTrace();
  }
  return "";
}

dao.getAllPCInfo();这一行是取数据库PC所有信息

请问这样写代码有什么问题吗?dao要实例化吗?如果要应该怎么做??

------解决方案--------------------
比如这样写
Java code

public class People
{
   // private  Templeate  temp;
    
    public  People(){
        System.out.println("--PEOPLE----实例化了----------");
    }
    private ManagerDao  managedao;

    public ManagerDao getManagedao()
    {
        return managedao;
    }

    public void setManagedao(ManagerDao managedao) //这个方法必须有,因为采用的是set方式注入
    {   
        
        System.out.println("======");
        this.managedao = managedao;
    }
   public class ManagerDao// extends HibernateDaoSupport
{  
    
    
     public  String save(){
         System.out.println("==============save============");
         return  "hhhh";
     }
     
     public void saveOrUpdateObject(Object obj) {
         //getHibernateTemplate().saveOrUpdate(obj);
           return ;
     }
     
     
  
}
}