当前位置: 代码迷 >> J2EE >> 如何实现通过excel模板里面的名字给excel赋值
  详细解决方案

如何实现通过excel模板里面的名字给excel赋值

热度:323   发布时间:2016-04-22 01:37:15.0
怎么实现通过excel模板里面的名字给excel赋值
下面的是我通过行与列赋值代码.太麻烦.
Java code
    String templeFile = path + "/common/example/staffresume.xls";                   String outputPath = path + "/common";             // テンポレトを取得             // 新規Workbook             HSSFWorkbook hb = null;             try {                 POIFSFileSystem pfs = new POIFSFileSystem(new FileInputStream(templeFile));                  hb = new HSSFWorkbook(pfs);             } catch (FileNotFoundException e1) {                 e1.printStackTrace();                 System.out.println("THE FileNotFoundException ERROR,156 lines in the StaffResumeAction");             } catch (IOException e1) {                 System.out.println("THE IOException ERROR,159 lines in the system");                 e1.printStackTrace();             }             HSSFSheet sheet = hb.getSheetAt(0);             HSSFRow row = null;             HSSFCell cell = null;       /** 姓 名 **/             row = sheet.getRow(4);             cell = row.getCell(7);             if (staff.getStaffName() == null) {                 cell.setCellValue(new HSSFRichTextString(""));             } else {                 cell.setCellValue(new HSSFRichTextString(staff.getStaffName()));             }       response.getOutputStream();             // 新規ファイルの輸出流、輸入流             File file = new File(path);             // 指定パースがない場合 生成パス             if (!file.exists()) {                 file.mkdirs();             }             FileOutputStream fos = null;             try {                  // 生成するファイルパス                 fos = new FileOutputStream(outputPath + "/" + fileName + ".xls");                 hb.write(fos);                 // 指定パースに新excelファイルを生成                 fos.flush();             } catch (IOException ie) {                 ie.printStackTrace();                 System.out.println("the IOException error,1081 lines in the system");             } finally {                 if (fos != null) {                     //  ファイルの輸出流を閉める                     fos.close();                     fos = null;                 }                 // 以前生成した lzh圧縮ファイルを削除                 // deleteFile(outputPath, "lzh", dateString);                 // lzh圧縮ファイル生成                 // getZipFile(outputPath, fileName + ".xls", outputPath, fileName+ ".lzh");                 // 生成した excelファイルを削除                 // deleteFile(outputPath, "xls", dateString);             }             //  lzh圧縮ファイル名             /*             * fileName = fileName+".lzh"; // lzh圧縮ファイルパス String filepath = outputPath; // response設定とダウンロード保存のファイル名             * response.reset();             */                   // Excelファイル名             fileName = fileName + ".xls";                   StringBuffer sb = new StringBuffer();             sb.append("attachment; filename=\"");             sb.append(new String(fileName.getBytes("SJIS"), "UTF-8"));             sb.append("\"");             response.reset();             response.setContentType("text/html;charset=UTF-8");             response.setContentType("application/x-msdownload");             response.setHeader("Content-Disposition", sb.toString());                   // ファイルの輸出流、輸入流             OutputStream output = null;             FileInputStream fis = null;             try {                 File f = new File(outputPath + File.separator + fileName);                 // ファイルの輸出流                 output = response.getOutputStream();                 // ファイルの輸入流                 fis = new FileInputStream(f);                 byte[] b = new byte[(int) f.length()];                 // 画面で表示する                 int i = 0;                 while ((i = fis.read(b)) > 0) {                     output.write(b, 0, i);                 }                 output.flush();             } catch (Exception e) {                 e.printStackTrace();                 System.out.println("the Exception error,1132 lines in the system");             } finally {                 if (fis != null) {                     //  ファイルの輸入流を閉める                     fis.close();                     fis = null;                 }                 if (output != null) {                     //  ファイルの輸出流を閉める                     output.close();                     output = null;                 }             }
  相关解决方案