国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > php教程 > java poi 操作excel,xssf 读excel 2007,将某些单元格为固定值

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

来源:程序员人生   发布时间:2015-05-28 09:10:57 阅读次数:4206次

本来想看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(); } }

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生