(2) 如何用Apache POI操作Excel文件-----如何在已有的Excel文件中插入一行新的数据?
2015-05-28 23:15
579 查看
在POI的第一节入门中,我们提供了两个简单的例子,一个是如何用Apache POI新建一个工作薄,另外一个例子是,如果用Apache POI新建一个工作表。那么在这个章节里面,我将会给大家演示一下,如何用Apache POI在已有的Excel文件中插入一行新的数据。具体代码,请看下面的例子。
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class CreatRowTest { //当前文件已经存在 private String excelPath = "D:\\exceltest\\comments.xlsx"; //从第几行插入进去 private int insertStartPointer = 3; //在当前工作薄的那个工作表单中插入这行数据 private String sheetName = "Sheet1"; /** * 总的入口方法 */ public static void main(String[] args) { CreatRowTest crt = new CreatRowTest(); crt.insertRows(); } /** * 在已有的Excel文件中插入一行新的数据的入口方法 */ public void insertRows() { XSSFWorkbook wb = returnWorkBookGivenFileHandle(); XSSFSheet sheet1 = wb.getSheet(sheetName); XSSFRow row = createRow(sheet1, insertStartPointer); createCell(row); saveExcel(wb); } /** * 保存工作薄 * @param wb */ private void saveExcel(XSSFWorkbook wb) { FileOutputStream fileOut; try { fileOut = new FileOutputStream(excelPath); wb.write(fileOut); fileOut.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 创建要出入的行中单元格 * @param row * @return */ private XSSFCell createCell(XSSFRow row) { XSSFCell cell = row.createCell((short) 0); cell.setCellValue(999999); row.createCell(1).setCellValue(1.2); row.createCell(2).setCellValue("This is a string cell"); return cell; } /** * 得到一个已有的工作薄的POI对象 * @return */ private XSSFWorkbook returnWorkBookGivenFileHandle() { XSSFWorkbook wb = null; FileInputStream fis = null; File f = new File(excelPath); try { if (f != null) { fis = new FileInputStream(f); wb = new XSSFWorkbook(fis); } } catch (Exception e) { return null; } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } return wb; } /** * 找到需要插入的行数,并新建一个POI的row对象 * @param sheet * @param rowIndex * @return */ private XSSFRow createRow(XSSFSheet sheet, Integer rowIndex) { XSSFRow row = null; if (sheet.getRow(rowIndex) != null) { int lastRowNo = sheet.getLastRowNum(); sheet.shiftRows(rowIndex, lastRowNo, 1); } row = sheet.createRow(rowIndex); return row; } }
相关文章推荐
- (1) 如何用Apache POI操作Excel文件-----入门
- APache PDFbox API使用(3)----如何得到一个带表单的PDF文件的表单结构
- apache 访问控制
- apache 日志切割
- 解决Apache服务器无法启动
- 64位系统下一键安装apache+php服务
- Apache Spark源码分析-- Job的提交与运行
- The type org.apache.commons.lang.exception.NestableRuntimeException cannot be resolved. 问题解决
- APACHE COMMONS LOGGING
- apache的源码安装详细过程全纪录
- apache、mysql安装过程遇到的问题
- Apache DirectMemory介绍
- apache mod_rewrite 应用举例
- CentOS 6.3下源码安装LAMP(Linux+Apache+Mysql+Php)环境
- 如何在Ubuntu中设置Apache Web 服务器支持 UserDir 特性
- CentOS Linux系统下更改Apache默认网站目录
- centos下如何卸载apache
- centos下如何卸载apache
- Centos6.5源码搭建SVN+Apache
- Apache Storm环境配置