POI对ecxl文件内容进行修改

import java.io.BufferedReader;  
import java.io.File;  
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;  
  
  
public class ChekFile {  
 public static void main(String[] args) {  
        File f = new File("e:/liu");  
        print(f, 0);  

    }  
  
    /** 
     * 遍历文件夹 
     * 
     * @param f 
     * @param len 
     */  
    @SuppressWarnings("unlikely-arg-type")
	public static void print(File f, int len) {  
        File[] file = f.listFiles();  
  
        for (int i = 0; i < file.length; i++) {  
            if (file[i].isDirectory()) { //判断是否文件夹  
                print(file[i], len + 1);  
            }  
  
            // 为防止输出文件覆盖源文件,所以更改输出盘路径 也可自行设置其他路径  
//            File outPath = new File(file[i].getParent().replace("F:", "E:"));  
            File readfile = new File(file[i].getAbsolutePath());  
  
            if (!readfile.isDirectory()) {  
                String filename = readfile.getName(); // 读到的文件名  
                String absolutepath = readfile.getAbsolutePath(); // 文件的绝对路径  
                //一些特殊字符分割要加【】
                String[] fileType = absolutepath.split("[.]");
                if(fileType[1].equals("xlsx")||fileType[1].equals("xls")) {
                	
                	 writeExcel3(absolutepath);
                	 System.out.println(absolutepath);
                }
                
               
            }  
        }  
    }  
  
  //修改excel表格,path为excel修改前路径(D:\\test.xlsx)
	  public static void writeExcel3(String path) {
	    try {
	      //传入的文件
	      FileInputStream fileInput = new FileInputStream(path);
	      //poi包下的类读取excel文件

	      // 创建一个webbook,对应一个Excel文件
	      @SuppressWarnings("resource")
		Workbook workbook = null;
		try {
			workbook = WorkbookFactory.create(fileInput);
		} catch (EncryptedDocumentException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (InvalidFormatException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	      //对应Excel文件中的sheet,0代表第一个
	      Sheet sh = workbook.getSheetAt(0);
	      //修改excle表的第5行,从第三列开始的数据
	      
	      //获取总行数
	      int rowNum = sh.getLastRowNum();
	      //获取总行数
	      int coloumNum=sh.getRow(0).getPhysicalNumberOfCells();
	      for(int i = 0;i<=rowNum;i++) {
	    	  for(int j = 0;j<=coloumNum;j++) {
	    		  try {
//	    			  System.out.println(sh.getRow(i).getCell((short)j).getStringCellValue()+i+""+j);
////		    		  System.out.println("ddd");
//		    		  if(sh.getRow(i).getCell((short)j).getStringCellValue().equals("福瑞博得")) {
//		    			  sh.getRow(i).getCell((short) j).setCellValue("信必忧");
//		    			  System.out.println("d");
//		    		  }
	    			  	//获取单元格内容的类型
	    			  switch(sh.getRow(i).getCell((short)j).getCellTypeEnum()) {
	    			  case STRING://字符串
	    				  //获取单元格的内容
	    				  if(sh.getRow(i).getCell((short)j).getStringCellValue().contains("福瑞博得")) {
	    					  //设置单元格的内容
	    					  sh.getRow(i).getCell((short) j).setCellValue(sh.getRow(i).getCell((short)j).getStringCellValue().replaceAll("福瑞博得", "信必忧"));
			    		  }
	    				  if(sh.getRow(i).getCell((short)j).getStringCellValue().contains("福瑞博得")) {
	    					  
	    					  sh.getRow(i).getCell((short) j).setCellValue(sh.getRow(i).getCell((short)j).getStringCellValue().replaceAll("福瑞博得", "symbio"));
			    		  }
	    				  break;
	    			  case NUMERIC:
	    				  break;
	    			  case BOOLEAN:
	    				  break;
	    			  case FORMULA:
	    				  break;
	    			  case BLANK:
	    				  break;
	    			  case ERROR:
	    				  break;
					default:
						break;
	    			  
	    			  }
				} catch (Exception e) {
//					System.out.println(sh.getRow(i).getCell((short)j).getStringCellValue()+i+""+j+"为空");
					
					continue;
				}
	    		  
	    	  }
	      }
	      
	      FileOutputStream os = new FileOutputStream(path);
	      // FileOutputStream os = new FileOutputStream("D:\\test.xlsx");//此路径也可写修改前的路径,相当于在原来excel文档上修改
	      os.flush();
	      //将Excel写出
	      workbook.write(os);
	      //关闭流
	      fileInput.close();
	      os.close();
	    } catch (IOException e) {
	      e.printStackTrace();
	    }
	  }
pom.xml文件
<dependency>
 <groupId>org.apache.poi</groupId>
	<artifactId>poi</artifactId>
	<version>3.17</version>
</dependency>
   	
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
 <groupId>org.apache.poi</groupId>
 <artifactId>poi-ooxml-schemas</artifactId>
 <version>3.17</version>
</dependency>
   	
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>openxml4j</artifactId>
    <version>1.0-beta</version>
</dependency>
  
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>3.17</version>
</dependency>




猜你喜欢

转载自blog.csdn.net/bonie_juzi/article/details/79282610
今日推荐