java poi修改excel

来源:互联网 发布:现言推荐知乎 编辑:程序博客网 时间:2024/05/12 07:52
/** * modify excel *  * @param sheetPageid *            start with 1 * @param rowid *            start with 1 * @param cellid *            start with 1 * @param value *            set cell-value */public boolean setSheetValue(int sheetPageid, int rowid, int cellid,String content) {sheetPageid -= 1;rowid -= 1;cellid -= 1;try {sheet = book.getSheetAt(sheetPageid);if (sheet == null) {book.createSheet();}HSSFRow row = sheet.getRow(rowid);if (null == row) {// 如果不做空判断,你必须让你的模板文件画好边框,beginRow和beginCell必须在边框最大值以内// 否则会出现空指针异常row = sheet.createRow(rowid);}HSSFCell cell = row.getCell(cellid);if (null == cell) {cell = row.createCell(cellid);}// 设置存入内容为字符串cell.setCellType(HSSFCell.CELL_TYPE_STRING);// 向单元格中放入值cell.setCellValue(content);closeExcel();return true;} catch (Exception e) {e.printStackTrace();return false;}}