部分代码如下(POI的版本为3.2)
public void insertRow() throws IOException {
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("workbook.xls"));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheet("new sheet");
int starRow = 4;
int rows = 5;
sheet.shiftRows(starRow + 1, sheet.getLastRowNum(), rows,true,false);
//Parameters:
//startRow - the row to start shifting
//endRow - the row to end shifting
//n - the number of rows to shift
//copyRowHeight - whether to copy the row height during the shift
//resetOriginalRowHeight - whether to set the original row's height to the default
starRow = starRow - 1;
for (int i = 0; i < rows; i++) {
HSSFRow sourceRow = null;
HSSFRow targetRow = null;
HSSFCell sourceCell = null;
HSSFCell targetCell = null;
short m;
starRow = starRow + 1;
sourceRow = sheet.getRow(starRow);
targetRow = sheet.createRow(starRow + 1);
targetRow.setHeight(sourceRow.getHeight());
for (m = sourceRow.getFirstCellNum(); m < sourceRow.getLastCellNum(); m++) {
sourceCell = sourceRow.getCell(m);
targetCell = targetRow.createCell(m);
//targetCell.setEncoding(sourceCell.getEncoding());
targetCell.setCellStyle(sourceCell.getCellStyle());
targetCell.setCellType(sourceCell.getCellType());
targetCell.setCellValue(i);//設置值
}
}
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
参考了http://blog.csdn.net/daemon_boy/archive/2007/09/15/1786384.aspx的代码
posted on 2009-04-16 16:43
Ke 阅读(10791)
评论(0) 编辑 收藏 所属分类:
java excel