使用java操作Office用如下几种选择
[b]1 jacob [/b]
通过COM操作Office,试了一下似乎不好用
出错如下:
com.jacob.com.ComFailException: A COM exception has been encountered:
At Invoke of: Quit Description: An unknown COM error has occured.
dll文件copy到windows/system32下之后
仍然不能操作office
[b]2 jxl[/b]
可读取excel,创建excel,使用简便。
[b]3 java2word[/b]
比较好用,可以对word进行插入操作。
[b]4 POI,Apache的项目[/b]
[url] http://poi.apache.org/[/url]
向已有excel文件中设定值的方法:
private boolean PutExcelData(String argFile,
int argSheet,
int argRow,
int argCol,
String argVal){
boolean bolRtn = false;
POIFSFileSystem fs;
try {
fs = new POIFSFileSystem(new FileInputStream(argFile));
HSSFWorkbook wb = new HSSFWorkbook(fs);
wb.getAllPictures();
HSSFSheet sheet = wb.getSheetAt(argSheet);
HSSFRow row = sheet.getRow(argRow);
HSSFCell cell = row.getCell((short)argCol);
if (cell == null)
cell = row.createCell((short)0);
//cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(argVal);
FileOutputStream fileOut = new FileOutputStream(argFile);
wb.write(fileOut);
fileOut.close();
bolRtn = true;
} catch (Exception e) {
logger.warn("ERROR------------------------------>" + e.toString());
}finally{
return bolRtn;
}
}
[b]1 jacob [/b]
通过COM操作Office,试了一下似乎不好用
出错如下:
com.jacob.com.ComFailException: A COM exception has been encountered:
At Invoke of: Quit Description: An unknown COM error has occured.
dll文件copy到windows/system32下之后
仍然不能操作office
[b]2 jxl[/b]
可读取excel,创建excel,使用简便。
[b]3 java2word[/b]
比较好用,可以对word进行插入操作。
[b]4 POI,Apache的项目[/b]
[url] http://poi.apache.org/[/url]
向已有excel文件中设定值的方法:
private boolean PutExcelData(String argFile,
int argSheet,
int argRow,
int argCol,
String argVal){
boolean bolRtn = false;
POIFSFileSystem fs;
try {
fs = new POIFSFileSystem(new FileInputStream(argFile));
HSSFWorkbook wb = new HSSFWorkbook(fs);
wb.getAllPictures();
HSSFSheet sheet = wb.getSheetAt(argSheet);
HSSFRow row = sheet.getRow(argRow);
HSSFCell cell = row.getCell((short)argCol);
if (cell == null)
cell = row.createCell((short)0);
//cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(argVal);
FileOutputStream fileOut = new FileOutputStream(argFile);
wb.write(fileOut);
fileOut.close();
bolRtn = true;
} catch (Exception e) {
logger.warn("ERROR------------------------------>" + e.toString());
}finally{
return bolRtn;
}
}