zoukankan      html  css  js  c++  java
  • Java操作excel-POI

    添加依赖项

    这里直接给出Maven配置文件

     <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>
       <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.9</version>
    </dependency> 
    <dependency>
        <groupId>org.apache.xmlbeans</groupId>
        <artifactId>xmlbeans</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.9</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.9</version>
    </dependency>
    

    读取excel

    static void Write(String path) throws InvalidFormatException, IOException 
        {
        	FileInputStream file = new FileInputStream(new File(path));
    
            Workbook workbook = WorkbookFactory.create(file);//读取excel文件
            Sheet sheet = workbook.getSheetAt(0);//获取第一个sheet页
            //遍历行
            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                //遍历列
                Iterator<Cell> cellIterator = row.cellIterator();
                while (cellIterator.hasNext()) {
    
                    Cell cell = cellIterator.next();
    
                    switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_BOOLEAN:
                            System.out.print(cell.getBooleanCellValue() + "		");//输出bool类型值
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "		");//输出Int类型值
                            break;
                        case Cell.CELL_TYPE_STRING:
                            System.out.print(cell.getStringCellValue() + "		");//输出String类型值
                            break;
                    }
                }
                System.out.println("");
            }
    		file.close();
    		FileOutputStream outFile =new FileOutputStream(new File(path));
    		workbook.write(outFile);
    		outFile.close();
        }
    

    更新excel

    static void Update(String path) throws InvalidFormatException, IOException 
        {
        	FileInputStream file = new FileInputStream(new File(path));
    
            Workbook workbook = WorkbookFactory.create(file);
            Sheet sheet = workbook.getSheetAt(0);
            
    		Cell cell = sheet.getRow(1).getCell(2);//读取第2行第3个
    		cell.setCellValue(12554);//将值改成12554
    		
    		file.close();
    		
    		FileOutputStream outFile =new FileOutputStream(new File(path));
    		workbook.write(outFile);
    		outFile.close();
        }
    

    创建一个新的excel

      static void Create(String path)
        {
        	HSSFWorkbook workbook = new HSSFWorkbook();
        	HSSFSheet sheet = workbook.createSheet("伊泽rl");
        	
        	Row row = sheet.createRow(0);
        	
        	Cell cell = row.createCell(0);
        	
        	cell.setCellValue("伊泽rl");
        	
        	try {
        	    FileOutputStream out =new FileOutputStream(new File(path));
        	    workbook.write(out);
        	    out.close();
    
        	} catch (FileNotFoundException e) {
        	    e.printStackTrace();
        	} catch (IOException e) {
        	    e.printStackTrace();
        	}
        }
    

    添加公式

    cell.setCellFormula("A1*B2*C3");
    

    添加背景颜色、单元格样式等操作大家可以查看poi的API

    https://poi.apache.org/apidocs/4.0/

  • 相关阅读:
    [Java]用递归判断是否为递减数组
    android欢迎界面的编程实现[手相评分软件实例]
    工作两周年的感想
    最新基于adtbundlewindowsx86的android开发环境搭建
    新浪微博Failed to receive access token
    朋友
    房产中介
    房产中介
    与 Josh Bloch 探讨 Java 未来
    关于英语学习的一点思考
  • 原文地址:https://www.cnblogs.com/ezrealli/p/13033389.html
Copyright © 2011-2022 走看看