首页 > 编程语言 > 详细

java poi 处理excel表格数据

时间:2019-07-18 22:39:56      阅读:83      评论:0      收藏:0      [点我收藏+]

目的:获取本地excel表格数据,修改其中的某个值,存入新的excel。

String filepath = "D:\\software\\t.xlsx";
FileInputStream excelFileInputStream = new FileInputStream(filepath);
XSSFWorkbook workbook = new XSSFWorkbook(excelFileInputStream);//拿到文件转化为javapoi可操纵类型
excelFileInputStream.close();
XSSFSheet sheet = workbook.getSheetAt(0);
// 获取行数
int rows = sheet.getLastRowNum();
// 获取列数
int clos = sheet.getRow(0).getPhysicalNumberOfCells();

XSSFWorkbook work = new XSSFWorkbook();
XSSFSheet she = work.createSheet("tt3");
for (int i = 1; i < rows; i++) {
     XSSFRow row = sheet.getRow(i);//得到行
     // 创建行
     XSSFRow nowRow = she.createRow(i);
     for(int j=0; j<clos; j++){
          XSSFCell cell = row.getCell(j);//得到列
          cell.setCellType(XSSFCell.CELL_TYPE_STRING);
          //System.out.println(cell.getStringCellValue());
          // 创建列
          XSSFCell nowCell = nowRow.createCell(j);
          nowCell.setCellType(XSSFCell.CELL_TYPE_STRING);
          nowCell.setCellValue(cell.getStringCellValue());
      }
      for(int j=0; j<clos; j++){
            XSSFCell cell = nowRow.getCell(j);//得到列
            cell.setCellType(XSSFCell.CELL_TYPE_STRING);
            System.out.print(cell.getStringCellValue()+" ");
      }
      System.out.println();
}
//保存
FileOutputStream excelFileOutPutStream = new FileOutputStream("D:\\software\\tx.xlsx");//写数据到这个路径上
work.write(excelFileOutPutStream);
excelFileOutPutStream.flush();
excelFileOutPutStream.close();
System.out.println("done");

总结:

XSSFWorkbook,HSSFWorkbook每一个对应的版本都不一样,且对应的导出数据都有限制,详情参考:https://www.cnblogs.com/skyislimit/articles/10514719.html。

 

java poi 处理excel表格数据

原文:https://www.cnblogs.com/CherishZeng/p/11209710.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!