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

xiaoqiangsass 2012-03-02 05:49:49
下面的是我通过行与列赋值代码.太麻烦.

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;
}
}
...全文
235 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoqiangsass 2012-03-21
  • 打赏
  • 举报
回复

/**
* (explain)メソッドの処理内容を記述します。
* @param hb HSSFWorkbook
* @param name Excel表格名字
* @param value Excel表格要填充的内容
*/
public static void setHASSFCellValue(HSSFWorkbook hb, String name, String value) {
try {
HSSFRow row = null;
HSSFCell cell = null;

if (value == null) {
value = "";
}
if (name == null) {
name = "";
}
int nameIndex = hb.getNameIndex(name);
if (nameIndex != -1) {
HSSFName hssfName = hb.getNameAt(nameIndex);
String position = hssfName.getRefersToFormula();

String sheetName = position.substring(0, position.indexOf("!"));
HSSFSheet sheet = hb.getSheet(sheetName);
String pos = position.substring(position.indexOf("!") + 1);
String sxy = ExcelUtil.explain(pos);
row = sheet.getRow(Integer.valueOf(sxy.substring(sxy.indexOf(",") + 1)));
cell = row.getCell(Integer.valueOf(sxy.substring(0, sxy.indexOf(","))));
cell.setCellValue(value);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error name: " + name + " value:" + value);
}
}


呐,写成了一个方法,很简单~
xiaoqiangsass 2012-03-21
  • 打赏
  • 举报
回复

/**
* Excel出力,根据excel单元格名字赋值 (ExcleOutPut)メソッドの処理内容を記述します。
* @param staffCds staffCd
* @throws Exception
* @throws Exception Java异常
*/
@Override
public void excleOutPut(String staffCds) throws Exception {
if (CommonUtils.isEmpty(staffCds)) {
return;
}
HttpServletResponse response = ServletActionContext.getResponse();
// Excelファイル名
java.io.File exportedlFile = null;
final String SPLIT = ",";
if (staffCds.indexOf(SPLIT) == -1) {
exportedlFile = getExportedlFileFromLocal(staffCds);
System.out.println("exportedlFile.getAbsolutePath():"+exportedlFile.getAbsolutePath());
System.out.println(exportedlFile.getPath()+"."+exportedlFile.getName());
} else {
String [] arrayStaffCd = staffCds.split(SPLIT);
java.io.File [] srcFiles = new java.io.File[arrayStaffCd.length];
HttpServletRequest request = ServletActionContext.getRequest();
String tempPath = request.getSession().getServletContext().getRealPath("");
tempPath += "/common/";
String zipName = "resumes";
//  時間格式
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddhhmmss");
String strDate = formatter.format(new java.util.Date());
zipName += "_" + strDate + ".zip";
for (int i = 0; i < arrayStaffCd.length; i++) {
srcFiles[i] = getExportedlFileFromLocal(arrayStaffCd[i]);
}
java.io.File zipFile = new java.io.File(tempPath + zipName);
CommonUtils.zipFiles(srcFiles, zipFile);
exportedlFile = new java.io.File(tempPath + zipName);
for (java.io.File file : srcFiles) {
file.delete();
}
}
String fileName = exportedlFile.getName();
// StringBuilder sw = new StringBuilder();
// sw.append("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=UTF-8\"/>");
// StringBuffer sb = new StringBuffer(sw);

StringBuffer sb = new StringBuffer();
sb.append("attachment; filename=\"");
sb.append(new String(fileName.getBytes("UTF-8"), "iso8859-1"));
sb.append("\"");

// StringBuffer sb = new StringBuffer();
// sb.append("attachment; filename=\"");
// sb.append(ExcelUtil.toUtf8String(fileName));
// sb.append("\"");

// 文件名
// fileName = java.net.URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
// //
// response.setContentType("application/octet-stream");
// String temp = "attachment;filename=\"" + fileName + "\"";
// response.setHeader("Content-disposition", temp);
// osDownload = new BufferedOutputStream(response.getOutputStream());
//
// // 出力数据转换为带BOM的16LE编码
// byte[] BOM_UTF16 = new byte[] { (byte) 0xFF, (byte) 0xFE };
// //context 文件内容:String
// byte[] contentByte = javax.naming context.getBytes("UTF-16LE");
// byte[] allData = new byte[BOM_UTF16.length + contentByte.length];
// System.arraycopy(BOM_UTF16, 0, allData, 0, BOM_UTF16.length);
// System.arraycopy(contentByte, 0, allData, BOM_UTF16.length,
// contentByte.length);
// osDownload.write(allData);
// //文件读取处理:
// int column = 1;
// //文件读取、文件读取指定编码
// InputStreamReader reader = new InputStreamReader(form.getFile()
// .getInputStream(),"UTF-16LE");
// //文件流读取对象创建
// BufferedReader buf = new BufferedReader(reader);
// String line = "";
// while ((line = buf.readLine()) != null) {
// line = line.replace("\"","");
// if(line.length() == 0||line.replaceAll("\t","").length() == 0){
// continue;
// }
// byte utf_16 [] = line.getBytes("UTF-16LE");
// //BOM处理 UTF-16LE编码 1,2值为-1,-2
// if(utf_16[0] == -1 && utf_16[1] == -2){
// line = new String(line.getBytes("UTF-16LE"),2,
// line.getBytes("UTF-16LE").length - 2,"UTF-16LE");
// }
// // 按指定分隔符分割处理
// String[] st = new String(line.getBytes(),"UTF-8").split("\t");
// // 列数,内容等等处理
// if (st.length == 15)
// //文件内容CHECK
// count++;
// }
// buf.close();

response.reset();
response.setContentType("text/html;charset=UTF-8");
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", sb.toString());
CommonUtils.exportFile(exportedlFile , response.getOutputStream());
exportedlFile.delete();
}


hezhao4444 2012-03-03
  • 打赏
  • 举报
回复
不懂lz的意识,高深。虽然我做过这方面的,还是不懂你的意识。还有值得我说一下的是,你把代码放到上面至少把里面的注释换成中文的啊。或者干脆干掉。

67,516

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧