加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

java poi 操作excel,xssf 读excel 2007,将某些单元格为固定值

发布时间:2020-12-13 20:41:34 所属栏目:PHP教程 来源:网络整理
导读:本来想看1下java IO,NIO,发现这块知识体系还挺大。暂时写1个操作excel的demo。由于时间关系,完成了功能,后期继续完善。 功能:读取excel表(该表为测试结果表,共10几列,第1行是标题),将第0列标记为id(递增),第9列标记为结果(默许是PASS),第10

本来想看1下java IO,NIO,发现这块知识体系还挺大。暂时写1个操作excel的demo。由于时间关系,完成了功能,后期继续完善。

功能:读取excel表格(该表格为测试结果表格,共10几列,第1行是标题),将第0列标记为id(递增),第9列标记为结果(默许是PASS),第10列标记为姓名。

本可使用excel的拖拽功能,但由于excel中的内容和样式常常需要修改,因此致使重复工作。暂写这个小demo,期待完善后功能更详实。

import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; //import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.*; /** * Created by n on 2015/4/29. */ public class InsertInfoToExcel { //该方法判断excel版本 static Workbook openWorkbook(InputStream in,String filename) throws IOException { Workbook wb = null; if (filename.endsWith(".xlsx")) { wb = new XSSFWorkbook(in);//Excel 2007 } else { wb = (Workbook) new HSSFWorkbook(in);//Excel 2003 } return wb; } //该方法处理excel的数据,把第1列标记为id(递增),第9列标记为结果(默许是PASS),第10列标记为姓名 public static void setExcelData(String fileName) throws Exception { InputStream in = new FileInputStream(fileName); //创建输入流 Workbook wb = openWorkbook(in,fileName);// 获得Excel文件对象 Sheet sheet = wb.getSheetAt(0);// 获得文件的指定工作表m 默许的第1个Row row = null; int totalRows = sheet.getLastRowNum(); // 总行数 int totalCells = sheet.getRow(0).getLastCellNum();//总列数,根据第1行得来的 System.out.println("列数:" + totalCells + " 行数:" + totalRows); //顺次获得每行 for (int i = 1; i <= sheet.getLastRowNum(); i++) { XSSFRow row = (XSSFRow) sheet.getRow(i);// 获得行对象 if (row == null) {// 如果为空,不处理 continue; } //将第0列的标记为id,递增。遇到空的先不管,跳过 if (row.getCell(0) != null) { Cell cellIndex = row.getCell(0); System.out.print(cellIndex.getNumericCellValue()); cellIndex.setCellValue(i); } else { XSSFCell cellIndex = row.createCell(0); cellIndex.setCellValue(i); } //将第9列标记为测试结果,遇到空的就标记为PASS,非空的不管。 if (row.getCell(9) == null) { XSSFCell cellResult = row.createCell(9); System.out.print(cellResult.getStringCellValue()); cellResult.setCellValue("PASS"); } //将第10列的标记为测试人员的名字。不论是不是空都标记为名字。 if (row.getCell(10) != null) { XSSFCell cellName = row.getCell(10); // System.out.print(cellName.getStringCellValue()); cellName.setCellValue("aashen"); } else { XSSFCell cellName = row.createCell(10); cellName.setCellValue("aashen"); } } //写入数据,关闭 OutputStream out = new FileOutputStream(fileName); wb.write(out); in.close(); out.close(); } public static void main(String[] args) throws Exception { // String fileName="E:"+ File.separator+"hello.txt"; String fileName = "E://hi.xlsx"; setExcelData(fileName); // File f=new File(fileName); // if(f.exists()) // System.out.println("new file successfully"); // Writer out =new FileWriter(f); // String str="hello"; // out.write(str); // out.close(); } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读