要做个报表,数据库用的是ORACLE,还要改EXCEL的文件名字内容都是从网页上取的!求高人指点!最好有代码!

解决方案 »

  1.   

    我举例说明,你看一下能不能修改使用
    public class ProjectInformCSVAction extends ActionSupport{ private static Logger log = Logger.getLogger(ProjectInformCSVAction.class);
    /**
     * 项目名称
     */
    private String ipoName;
    /**
     * 项目管理Service
     */
    private ProjectManagerService projectManagerService;


    public String getIpoName() {
    return ipoName;
    }
    public void setIpoName(String ipoName) {
    this.ipoName = ipoName;
    }
    public void setProjectManagerService(ProjectManagerService projectManagerService) {
    this.projectManagerService = projectManagerService;
    }
    public InputStream getInputStream(){

    FileInputStream fis=null;
    /**
     * 判断项目参数是否为空
     */
    try{
    if(ipoName!=null&&!ipoName.equals("")){
    try {
    ipoName=java.net.URLDecoder.decode(ipoName,"UTF-8");
    } catch (UnsupportedEncodingException e) {
    log.error(e.getMessage());
    }
    }
    List<ProjectManageVO> projectManageVos=new ArrayList<ProjectManageVO>();
    try{
    /**
     * 获取所有数据list
     */
    projectManageVos=projectManagerService.getProjectsBylikeipoName(ipoName);

    /**
     * 把二维数组写成csv文件导出
     */
    String[][] csvReport;
    if(projectManageVos!=null){
    csvReport=ConvertListtoArrayString(projectManageVos);
    }else{
    csvReport=new String[][]{{"序号","ipo公司名称","创建日期","当前状态","保荐机构","关联文件名"}};
    }
    String filepath = projectManagerService.getRefreshFilePath("getrootdirectory","csvRootDirectory");
    File tempFile = new File(filepath+File.separator+"projectlist.csv");
    CSVWriter writer=null;
                 try {
                  //writer= new CSVWriter(new FileWriter(tempFile));
                  writer= new CSVWriter(new OutputStreamWriter(new FileOutputStream(tempFile),"gbk"));
    for (int i = 0; i < csvReport.length; i++) {
         writer.writeNext(csvReport[i]);
     }
    fis=new FileInputStream(tempFile);

    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
     try {
     if(writer!=null){
     writer.close();
     }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } }catch(IPOException e){
    log.error(e.getMessage());
    }

    }catch (IPOException e) {
    log.error(e.getMessage());
    }

    return fis;
    }
    /**
     * 把数据转为二维数据
     * @param list
     * @return
     */
    public String[][] ConvertListtoArrayString(List<ProjectManageVO> list){
    int a = list.size();
    String[] array0=new String[]{"序号","ipo公司名称","创建日期","当前状态","保荐机构","关联文件名"};
    String[][] array = new String[a+1][6];

    for (int i = 0;i<=list.size(); i++) {
    if(i==0){
    array[i][0]="序号";
    array[i][1]="ipo公司名称";
    array[i][2]="创建日期";
    array[i][3]="当前状态";
    array[i][4]="保荐机构";
    array[i][5]="关联用户名";
    }else{
    array[i][0]=i+"";
    array[i][1]=list.get(i-1).getIpoName();
    array[i][2]=list.get(i-1).getOperTime();
    if(list.get(i-1).getState().startsWith("0")){
    array[i][3]="没有完成项目";
    }else if(list.get(i-1).getState().startsWith("1")){
    array[i][3]="已完成项目";
    }else{
    array[i][3]="撤销项目";
    }
    array[i][4]=list.get(i-1).getUwName();
    array[i][5]=list.get(i-1).getUserName();
    }

    return array;
    }
    public String getDownloadFileName() {

    /**
     * 返回给前台下载框中的文件名
     */
    String downloadFileName="项目列表.csv";
    try {
    HttpServletResponse response=ServletActionContext.getResponse();
    /**
     * 设置文件头类型
     */
    //response.setContentType("application/x-msdownload");
    response.setContentType("text/csv");
    /**
     * 转码文件名
     */
    downloadFileName=java.net.URLEncoder.encode(downloadFileName,"UTF-8");  } catch (UnsupportedEncodingException e) { e.printStackTrace(); }
    return downloadFileName; }
    }