怎么实现通过excel模板里面的名字给excel赋值

xiaoqiangsass xiaoqiangsass 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
下面的是我通过行与列赋值代码.太麻烦.

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;
}
}
...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
xiaoqiangsass
Java EE 67,535 成员 225,843 内容