public void writer_Excel(String sSheetName,int iRow,String sColumn,String Data) {
InputStream is=null;
OutputStream os=null;
iRow=iRow-1;
try{
is=new FileInputStream(file);
workbook=new HSSFWorkbook(is);
Sheet sheet=workbook.getSheet(sSheetName);
int Column=Excel_ReturnColumnIndex(sheet,iRow,sColumn);
Cell cell=sheet.getRow(iRow).getCell(Column);
System.out.println(getCellValue(cell));
cell.setCellType(cell.CELL_TYPE_STRING);
cell.setCellValue(Data);
os=new FileOutputStream(file);
workbook.write(os);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
os.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
workbook.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
网上查了很久,发现大家都是用POI直接创建文件,设置格式、字体,以及写出文件。但是总是没有看到如何修改文件后进行保存。
按照创建文件的方式去修改了文件,没有进行保存的话,代码只会修改内存中的副本,并不会修改物理文件,需要写出一下即可!
代码如下:
这就是写出的代码 file 是一个文件对象
os=new FileOutputStream(file);
workbook.write(os);