急咨询java操作word的解决方案 我们做的应用需要用户在页面填写表单,提交后,服务器端后台把表单数据拿出来填写在一个word文件里,而且各个数据的位置是有要求的,形成一个word文件,保存在服务器,用户通过页面的超链接拿到,用于打印,各位大虾有过类似经验吗,尽量详细告诉小弟,先谢了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我用过一个APACHE的插件叫POI可以进行Office的操作。不过我只用过对Excel的处理 因为ms word和excel的文档都支持html文本格式,因此可以先用word或excel做好模版,另存为Web页,然后将该html改成jsp,将数据部分动态填入即可,不用很辛苦的调整格式 word页面只要在jsp头设置如下指令:@page contentType="application/msword;charset=GBK" %> excel如下:@page contentType="application/vnd.ms-excel;charset=GBK" %>使用这种方式客户端必须安装有office软件,用户访问时将在ie中直接用word或excel打开该页面。 /** * 給料統計Excelファイル書く * @param fileName folderName+fileName * @param value List * @throws IOException */ public static void writeSalaryExcelFile(String fileName, List value, String data, String deptName) throws IOException{ PayRecordForm countResultForm = null; InputStream inputFile = null; FileOutputStream fOut = null; HSSFWorkbook wb = null; try { inputFile = new FileInputStream(fileName); //workbook作成 wb = new HSSFWorkbook(inputFile); //sheet作成 HSSFSheet sheet = wb.getSheetAt(0); //wb.setSheetName(0, "DataCount"); HSSFRow row = null; HSSFCell cell = null; //準備完了 SalaryPropertyRead prop = new SalaryPropertyRead(); short rowStart = OutBean.formatShort(prop.getProperty("start")); short spanRow = OutBean.formatShort(prop.getProperty("rows")); String str = ""; String[] postion = null; HSSFRow tempRow = null; //表数据 int size = value.size(); int currentRow = rowStart; for (int mIndex = 0; mIndex < size ; mIndex++) { countResultForm = (PayRecordForm)value.get(mIndex); currentRow = rowStart+spanRow*mIndex; row = sheet.getRow(currentRow); if(row == null){ row = sheet.createRow(currentRow); } //名前 setSalaryRow(sheet,row,prop.getProperty("employeeName"),currentRow,countResultForm.getEmployeeName()); str = prop.getProperty("employeeName"); //日数 setSalaryRow(sheet,row,prop.getProperty("workDays"),currentRow,countResultForm.getWorkDays()); //出勤日数 setSalaryRow(sheet,row,prop.getProperty("currentDays"),currentRow,countResultForm.getCurrentDays()); //缺勤日数 setSalaryRow(sheet,row,prop.getProperty("noWorkDays"),currentRow,countResultForm.getNoWorkDays()); //遅刻回数 setSalaryRow(sheet,row,prop.getProperty("lateDays"),currentRow,countResultForm.getLateDays()); //早退回数 setSalaryRow(sheet,row,prop.getProperty("exitEarlyDays"),currentRow,countResultForm.getExitEarlyDays()); //残業週末時間 setSalaryRow(sheet,row,prop.getProperty("weekDays"),currentRow,countResultForm.getWeekDays()); //有給休暇日数 setSalaryRow(sheet,row,prop.getProperty("payAppDays"),currentRow,countResultForm.getPayAppDays()); //まきの休暇を取る日数がない setSalaryRow(sheet,row,prop.getProperty("noPayAppDays"),currentRow,countResultForm.getNoPayAppDays()); //基本給料 setSalaryRow(sheet,row,prop.getProperty("base"),currentRow,countResultForm.getBase()); //全勤給料 setSalaryRow(sheet,row,prop.getProperty("allPay"),currentRow,countResultForm.getAllPay()); //保険金額 setSalaryRow(sheet,row,prop.getProperty("insurance"),currentRow,countResultForm.getInsurance()); } //入力完了 //新規出力文件流 fOut = new FileOutputStream(fileName); // 保存する。 wb.write(fOut); fOut.flush(); //作成完了、ファイルを閉じる。 System.out.println("ファイル作成完了!"); } catch (Exception e) { e.printStackTrace(); System.out.println(" xlCreate() : " + e); } finally { try{ wb = null; if(fOut != null){ fOut.close(); } if(inputFile != null){ inputFile.close(); } } catch (Exception e){ } } } 对Excel的处理代码,不知道对你是否有用亚 servlet向jsp传递list的问题! struts 如何修改资源配置文件编码 关于jsp操作数据库语句问题 问一下,怎么能实现从数据库里面取出来的数据分几行几列显示? 看看 disabled的问题 JSP现在常用什么数据库和开发工具 感谢前一段为我出谋献策的Java伙伴们 急,我做的WAP现在在一个ACCEPT。JSP里实现翻页老是有问题,说找不到网页。它的原码如下: 返回值类型的问题! 外行求援:别笑我的问题太简单!! 对于夏昕的《SpringGuide》中,Login例子出错的总结 [菜鸟提问]关于session的错误问题~~
不过我只用过对Excel的处理
word页面只要在jsp头设置如下指令:
@page contentType="application/msword;charset=GBK" %>
excel如下:
@page contentType="application/vnd.ms-excel;charset=GBK" %>
使用这种方式客户端必须安装有office软件,用户访问时将在ie中直接用word或excel打开该页面。
* 給料統計Excelファイル書く
* @param fileName folderName+fileName
* @param value List
* @throws IOException
*/
public static void writeSalaryExcelFile(String fileName, List value, String data, String deptName) throws
IOException{
PayRecordForm countResultForm = null;
InputStream inputFile = null;
FileOutputStream fOut = null;
HSSFWorkbook wb = null;
try {
inputFile = new FileInputStream(fileName);
//workbook作成
wb = new HSSFWorkbook(inputFile);
//sheet作成
HSSFSheet sheet = wb.getSheetAt(0);
//wb.setSheetName(0, "DataCount");
HSSFRow row = null;
HSSFCell cell = null;
//準備完了
SalaryPropertyRead prop = new SalaryPropertyRead();
short rowStart = OutBean.formatShort(prop.getProperty("start"));
short spanRow = OutBean.formatShort(prop.getProperty("rows"));
String str = "";
String[] postion = null;
HSSFRow tempRow = null;
//表数据
int size = value.size();
int currentRow = rowStart;
for (int mIndex = 0; mIndex < size ; mIndex++) {
countResultForm = (PayRecordForm)value.get(mIndex);
currentRow = rowStart+spanRow*mIndex;
row = sheet.getRow(currentRow);
if(row == null){
row = sheet.createRow(currentRow);
}
//名前
setSalaryRow(sheet,row,prop.getProperty("employeeName"),currentRow,countResultForm.getEmployeeName());
str = prop.getProperty("employeeName");
//日数
setSalaryRow(sheet,row,prop.getProperty("workDays"),currentRow,countResultForm.getWorkDays());
//出勤日数
setSalaryRow(sheet,row,prop.getProperty("currentDays"),currentRow,countResultForm.getCurrentDays());
//缺勤日数
setSalaryRow(sheet,row,prop.getProperty("noWorkDays"),currentRow,countResultForm.getNoWorkDays());
//遅刻回数
setSalaryRow(sheet,row,prop.getProperty("lateDays"),currentRow,countResultForm.getLateDays());
//早退回数
setSalaryRow(sheet,row,prop.getProperty("exitEarlyDays"),currentRow,countResultForm.getExitEarlyDays());
//残業週末時間
setSalaryRow(sheet,row,prop.getProperty("weekDays"),currentRow,countResultForm.getWeekDays());
//有給休暇日数
setSalaryRow(sheet,row,prop.getProperty("payAppDays"),currentRow,countResultForm.getPayAppDays());
//まきの休暇を取る日数がない
setSalaryRow(sheet,row,prop.getProperty("noPayAppDays"),currentRow,countResultForm.getNoPayAppDays());
//基本給料
setSalaryRow(sheet,row,prop.getProperty("base"),currentRow,countResultForm.getBase());
//全勤給料
setSalaryRow(sheet,row,prop.getProperty("allPay"),currentRow,countResultForm.getAllPay());
//保険金額
setSalaryRow(sheet,row,prop.getProperty("insurance"),currentRow,countResultForm.getInsurance());
}
//入力完了 //新規出力文件流
fOut = new FileOutputStream(fileName);
// 保存する。
wb.write(fOut);
fOut.flush();
//作成完了、ファイルを閉じる。
System.out.println("ファイル作成完了!"); }
catch (Exception e) {
e.printStackTrace();
System.out.println(" xlCreate() : " + e);
} finally {
try{
wb = null;
if(fOut != null){
fOut.close();
}
if(inputFile != null){
inputFile.close();
}
} catch (Exception e){
}
}
}