怎么实现通过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; } }
[解决办法]
不懂lz的意识,高深。虽然我做过这方面的,还是不懂你的意识。还有值得我说一下的是,你把代码放到上面至少把里面的注释换成中文的啊。或者干脆干掉。