日期:2014-05-20 浏览次数:21218 次
public class TestExcel {
/**
* t.xls
* ---------
* | a | b |
* ---------
*
* t2.xls
* ---------
* | a | a |
* ---------
* replace a from b.
*/
public static void main(String[] args) throws Exception {
InputStream in = new FileInputStream("c:/t.xls");
OutputStream out = new FileOutputStream("c:/t2.xls");
Workbook book2 = new HSSFWorkbook();
Workbook book = new HSSFWorkbook(in);
Sheet sheet = book.getSheetAt(0);
Sheet sheet2 = book2.createSheet(sheet.getSheetName());
for(int i = 0; i < sheet.getLastRowNum() + 1; i++) {
Row row = sheet.getRow(i);
if(row != null) {
Row row2 = sheet2.createRow(i);
for(int j = 0; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
if(cell != null) {
Cell cell2 = row2.createCell(j);
if("b".equals(cell.getStringCellValue())) {
cell2.setCellValue("a");
}
else {
cell2.setCellValue(cell.getStringCellValue());
}
}
}
}
}
book2.write(out);
out.close();
}
}
------解决方案--------------------
poi做不到,我估计没人能做了,因为office系列软件都是加密的,都是用16进制写进去的。doc的另外一种形式是rtf,不过不好控制。实在不行就调查下用微软的兼容包在你服务器上自动转换。
------解决方案--------------------
利用POI 最笨的方法..
POI 有两个类.一种处理07 也就是docx 以下简称 07类
一种处理03 也就是doc 以下简称 03类
很久没写记不清类名了,我记得区别两个类名区别不大.
你先用03类取出数据 doc中的数据,
再把取出的数据用 07类生成 新文件 docx.
转回来则反过来.
用 07类取出docx的数据.
再用 03类 把数据放进去生成 doc.
方法比较繁琐.推荐在找不到更佳解决方案的情况下使用.
over
ps:poi 比较适合 处理excel,word还是用其他的吧