XSSFWorkbook操作Excel文件总结

程序需求:

1. 读取Gatesummary.xlsx中A列,拼接成TIFvsPDFIn_2018022701.csv, 只有红色部分需要拼接替换。


2. 在下面路径中去找到1里面对应的文件, 如果第一个路径没有就去第二个找。

\\XXXXXXXXX\360Miyyyyy\ImageVerifier\Summary.20180507

\\XXXXXXXXX\360Miyyyyy\ImageVerifier\Summary

3. 如果文件H列是N, copy出来B列和E列指定 的路径文件,其中文件路径D:\Case360Migration需要替换成\\XXXXXXXXX\Case360yyyyy  Copy to 的文件夹以Gatesummary.xlsx中A列 命名。


4. 生成一个list, 把所有H列是N的需要copy的file路径和名称记录下来,并记录下是否成功。

    

    第二步的两个目录是leader的share folder,程序中为了方便测试,改成了本地目录。

    程序写的很烂,很乱。就先记录下,之后再调优。


Main.java

package file.operation;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;
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.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;




/**
 * @author bsnpc4x
 */
public class Main {

	private static Logger logger = Logger.getLogger(Main.class);
	public static void main(String[] args) {

		try {
			//1.find file Gatesummary.xlsx 
			String path = "D:\\Summary\\";
			SearchFile sf = new SearchFile();
			String realPath = sf.dosearch(path,"Gate");
			System.out.println("-------------------------");
			
			//2.read excel Gatesummary.xlsx column A
			ReadExcel re = new ReadExcel();
			List<String> li = re.readexcel(realPath,0);
			System.out.println("A列的值:-------"+li);
			
			for (String dir : li) {
				StringBuffer sb = new StringBuffer();
				//The generated folder is named dir
				String topath = "D:\\copyfile\\"+dir;
				WriteExcel.judeDirExists(topath);
				System.out.println("Generating the file path with the A column:"+topath);
				
				//Stitching the file path to find
				sb.append("TIFvsPDFIn_").append(dir).append(".csv");
				System.out.println("csv Path:-----"+sb);
//				String TIFPath = "\\SGDCWPWSTG010\\Case360Migration\\ImageVerifier\\Summary\\";
				String TIFPath = "D:\\Summary\\";
				//Go to the path to the file
				String csvpath = sf.dosearch(TIFPath,sb.toString());
				if(csvpath == null){
					System.out.println("The file was not found:"+csvpath);
					System.out.println("-----------------------------");
					continue;
				}
				System.out.println("-----------------------------");
				
				//Determine whether the H column of the file is N
				Map<String,Object> map = re.csvfile(csvpath,7);
				System.out.println("map size :"+map.size());
				//Get the data of the A column,the B column,the E column of the file
				List<String> A = (List<String>) map.get("listA");
				List<String> B = (List<String>) map.get("listB");
				List<String> E = (List<String>) map.get("listE");

				
				StringBuffer sbB = null;
				StringBuffer sbE = null;
				
				WriteExcel we = new WriteExcel();
				
				//Create an empty log file first
				File file = new File("D:\\filelog.xlsx");
				if (!file.exists()) {
					 file.createNewFile();
					we.setExcelHeader("D:\\filelog.xlsx");
				}
				/**
				 * Copy the two files into the folder dir respectively, determine 
				 * whether the copy is successful and return to Y successfully, 
				 * or return to N as a successful copy of the B column and the E column file, respectively.
				 * 
				 * Print the information of A column, B column, Y\N, E row and Y\N as log to log.excel.
				 */
				
					for (String str : A) {
						Map<String,Object> mapA= new ConcurrentHashMap<String, Object>();
						List<Map<String,Object>> listmapA = new ArrayList<Map<String,Object>>();
						mapA.put("Object Id", str);
						listmapA.add(mapA);
						we.writeExcel(listmapA, "D:\\filelog.xlsx");
					}
					//Splicing the B column, the file path of the E column
					for (String str : B) {
						Map<String,Object> mapB= new ConcurrentHashMap<String, Object>();
						List<Map<String,Object>> listmapB = new ArrayList<Map<String,Object>>();
						List<String> listB = new ArrayList<String>(); 
					
						String s1 = str.replaceAll("\\\\", "\\\\\\\\");
						sbB = new StringBuffer();

						int index = s1.indexOf("\\");
						System.out.println("index:"+index);
						int index1 = s1.indexOf("\\", index+1);
						System.out.println("index1:"+index1);
						String BPath = s1.substring(index1, s1.length());
	//					sbB.append("\\SGDCWPWSTG010\\Case360Migration").append(BPath);
						sbB.append("D:\\").append(BPath);
						System.out.println("sbB Path:-----"+sbB);
						
						if(new File(sbB.toString()).exists()){
							listB.add(sbB.toString());
							System.out.println("listB----"+listB);
							mapB.put("TIF File Path", listB);
							System.out.println("mapB----"+mapB);
						
							if(we.copyfile(sbB.toString(), topath)){
								mapB.put("Pass(Y/N)", "Y");
							}else{
								mapB.put("Pass(Y/N)", "N");
							}
						}else{
							System.out.println("The file does not exist:"+sbB.toString());
							continue;
						}
						listmapB.add(mapB);
						we.writeExcel(listmapB, "D:\\filelog.xlsx");
					}
					for (String str : E) {
						Map<String,Object> mapE= new ConcurrentHashMap<String, Object>();
						List<Map<String,Object>> listmapE = new ArrayList<Map<String,Object>>();
						List<String> listE = new ArrayList<String>(); 
						
						sbE = new StringBuffer();
						String s2 = str.replaceAll("\\\\", "\\\\\\\\");

						int index = s2.indexOf("\\");
						int index1 = s2.indexOf("\\", index+1);
						String EPath = s2.substring(index1, s2.length());
	//					sbE.append("\\SGDCWPWSTG010\\Case360Migration").append(EPath);
						sbE.append("D:\\").append(EPath);
						System.out.println("sbE Path:-----"+sbE);
						
						if(new File(sbE.toString()).exists()){
							listE.add(sbE.toString());
							mapE.put("PDF File Path", listE);
							if(we.copyfile(sbE.toString(), topath)){
								mapE.put("Pass(Y/N)", "Y");
							}else{
								mapE.put("Pass(Y/N)", "N");
							}
						}else {
							System.out.println("The file does not exist:"+sbE.toString());
							continue;
						}
						listmapE.add(mapE);
						we.writeExcel(listmapE, "D:\\filelog.xlsx");
					}
					
				}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}

在文件夹里据文件名查找文件:SearchFile.java

package file.operation;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;
 
/**
 * Find the file of the specified file name in the directory
 * @author bsnpc4x
 *
 */
public class SearchFile {
    static int countFiles = 0;// A variable that declares the number of statistical files
    static int countFolders = 0;// Declarations of variables in statistical folders
 
    public static File[] searchFile(File folder, final String keyWord) {// Recursively lookup files containing keywords
 
        File[] subFolders = folder.listFiles(new FileFilter() {// Using an internal anonymous class to obtain a file
            @Override
            public boolean accept(File pathname) {// Accept method for implementing FileFilter class
                if (pathname.isFile())// If it is a file
                    countFiles++;
                else
                    // If it's a directory
                    countFolders++;
                if (pathname.isDirectory()
                        || (pathname.isFile() && pathname.getName().toLowerCase().contains(keyWord.toLowerCase())))// 目录或文件包含关键字
                    return true;
                return false;
            }
        });
 
        List<File> result = new ArrayList<File>();// Declare a set
        for (int i = 0; i < subFolders.length; i++) {// Loop display folders or files
            if (subFolders[i].isFile()) {// If it is a file, add the file to the result list.
                result.add(subFolders[i]);
            } else {// If it is a folder, the method is called recursively, and all the files are added to the result list.
                File[] foldResult = searchFile(subFolders[i], keyWord);
                for (int j = 0; j < foldResult.length; j++) {// Circular display file
                    result.add(foldResult[j]);// Files are saved to the collection
                }
            }
        }
 
        File files[] = new File[result.size()];// An array of files, length as the length of a set
        result.toArray(files);// Set array
        return files;
    }
 
    public static String dosearch(String path,String keyword) {
    	
       
    	File folder = new File(path);// default directory
        
        if (!folder.exists()) {// If the folder does not exist
            String s = "directory does not exist:" + folder.getAbsolutePath();
            System.out.println("directory does not exist:" + folder.getAbsolutePath());
            
            return s;
        }
        File[] result = searchFile(folder, keyword);// Invocation method gets the array of files
        System.out.println("在 " + folder + " 以及所有子文件时查找对象" + keyword);
        System.out.println("查找了" + countFiles + " 个文件," + countFolders + " 个文件夹,共找到 " + result.length + " 个符合条件的文件:");
        if(result.length==0){
//        	dosearch("\\SGDCWPWSTG010\\Case360Migration\\ImageVerifier\\Summary.20180507\\",keyword);
//        	return dosearch("D:\\Summary.20180507\\",keyword);
        	folder = new File("D:\\Summary.20180507\\"); 
        	File[] result1 = searchFile(folder, keyword);
        	System.out.println("在另个目录查:D:\\Summary.20180507\\");
        	System.out.println("在 " + folder + " 以及所有子文件时查找对象" + keyword);
        	System.out.println("查找了" + countFiles + " 个文件," + countFolders + " 个文件夹,共找到 " + result1.length + " 个符合条件的文件:");
        	for (int i = 0; i < result1.length; i++) {// Circular display file
        		File file = result1[i];
        		System.out.println(file.getAbsolutePath() + " ");// Display the absolute path of the file
        		return file.getAbsolutePath().toString();
        	}
        }
        for (int i = 0; i < result.length; i++) {//Circular display file
            File file = result[i];
            System.out.println(file.getAbsolutePath() + " ");// Display the absolute path of the file
            return file.getAbsolutePath().toString();
        }
		return null;
    }
}


读Excel内容:ReadExcel.java

package file.operation;

import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

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.xssf.usermodel.XSSFWorkbook;

//import au.com.bytecode.opencsv.CSVReader;
import com.csvreader.CsvReader;

public class ReadExcel {

	public List<String> readexcel(String path, Integer column) throws Exception {
		InputStream iStream = new FileInputStream(path);
		// .xlsx
		XSSFWorkbook workbook = new XSSFWorkbook(iStream);
		// Cycle each page and process the current page
		for (Sheet xssfSheet : workbook) {
			// Processing the current page loop to read each line
			if (xssfSheet == null) {
				continue;
			}
			for (int runNum = 0; runNum < xssfSheet.getLastRowNum(); runNum++) {
				Row row = xssfSheet.getRow(runNum);
				int minColIx = row.getFirstCellNum();
				int maxColIx = row.getLastCellNum();
				List<String> list = new ArrayList<String>();
				String value = null;
				// Traversing the row to get each cell
				// for (int colIx = minColIx; colIx < maxColIx; colIx++) {
				// Cell cell = row.getCell(colIx);
				// cell.setCellType(Cell.CELL_TYPE_STRING);
				// // Get a specified column

				for (int runNum1 = 1; runNum1 <= xssfSheet.getLastRowNum(); runNum1++) {
					Row row1 = xssfSheet.getRow(runNum1); // Get the specified line
					// Traversing the row ,get the specifity column
					Cell cell2 = row1.getCell(column); // 获取指定列
					cell2.setCellType(Cell.CELL_TYPE_STRING);
					System.out.println(cell2.getStringCellValue());
					value = cell2.getStringCellValue();
					list.add(value); // Get all the contents of the column and deposit it in the list
				}

				return list;
			}
		}
		// }
		return null;
	}

	public Map<String, Object> csvfile(String path, Integer column)
			throws Exception {		
		
		CsvReader csvReader = new CsvReader(path, ',',  Charset.forName("UTF-8"));
	
		// Cycle each page and process the current page
		
		Map<String, Object> map = new ConcurrentHashMap<String, Object>();
		List<String> listA = new ArrayList<String>();
		List<String> listB = new ArrayList<String>();
		List<String> listE = new ArrayList<String>();

		List<String[]> listR = new ArrayList<String[]>();   
		while(csvReader.readRecord()){//readAll();
			listR.add(csvReader.getValues());   
        }   
        Object[][] datas = new String[listR.size()][];   
        for (int i = 0; i < listR.size(); i++) {   
            datas[i] = listR.get(i);   
        } 
        for (int i = 1; i < listR.size(); i++) {
        	Object[] data = datas[i];
        	String column7 = data[column].toString();
        	System.out.println("column7====="+column7);
        	if (column7.equals("N")) {
				listA.add(data[0].toString());
				listB.add(data[1].toString());
				listE.add(data[4].toString());
				map.put("listA", listA);
				map.put("listB", listB);
				map.put("listE", listE);
				System.out.println("map===" + map);
				
			} else {
				continue;
			}  
        }   
        System.out.println(""); 
        csvReader.close();
        return map;
		}


	}


找到文件后,复制文件到指定目录:WriteExcel.java  & CopyFile

package file.operation;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
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.XSSFWorkbook;

public class WriteExcel {

	static int j = 0;
	static int q = 0;
	static int m = 0;
	public  void writeExcel(List<Map<String,Object>> dataList,String finalXlsxPath) {
		 

		OutputStream out = null;
		FileInputStream fileInputStream = null;
		try {
			// 读取Excel文档
			fileInputStream = new FileInputStream(finalXlsxPath);
			XSSFWorkbook workBook = new XSSFWorkbook(fileInputStream);
			// sheet 对应一个工作页
			Sheet sheet = workBook.getSheetAt(0);
			if(sheet==null){//如果不存在sheet1,建立sheet1
				sheet=workBook.createSheet("sheet1");
			}
			
			/**
			 * 往Excel中写新数据
			 */
//			for (int j = 0; j < dataList.size(); j++) {
				// 创建一行:从第二行开始,跳过属性列
				// 得到要插入的每一条记录
				Map<String, Object> dataMap = dataList.get(0);
				String ObjectId = dataMap.get("Object Id") == null ? "":dataMap.get("Object Id").toString().trim();
				String TIFPath = dataMap.get("TIF File Path") == null ? "":dataMap.get("TIF File Path") .toString().trim();
				String PDFPath = dataMap.get("PDF File Path") == null ? "":dataMap.get("PDF File Path").toString().trim();
				String pass = dataMap.get("Pass(Y/N)") == null? "": dataMap.get("Pass(Y/N)").toString().trim();
				if(!"".equals(ObjectId)){
					Row row = sheet.createRow(++j);
					Cell A = row.createCell(0);
					A.setCellValue(ObjectId);
					System.out.println("log写入:"+ObjectId+"j======="+j);
					
				}
//				String stringCellValue = row.getCell(1).getStringCellValue();
				
//				for (int p=++q; p <= sheet.getLastRowNum(); p++) {
//					int p = ++q;
//					Row row1 = sheet.getRow(p); // 获取指定行
					// 遍历该行,获取每个cell元素
////					Cell cell2 = row1.getCell(p); // 获取指定列
//					System.out.println("q========"+q);
//					System.out.println("p========"+p);

						// 在一行内循环
						if(!"".equals(TIFPath) && !"".equals(pass)){
							int p = ++q;
							Row row1 = sheet.getRow(p);
							System.out.println("q========"+q);
							System.out.println("p========"+p);
							
							Cell B = row1.createCell(1);
							Cell C = row1.createCell(2);
							B.setCellValue(TIFPath);
							C.setCellValue(pass);
							System.out.println("log写入:"+TIFPath+"---"+pass);
//							break;
						}
//						break;
//					}
				if(!"".equals(PDFPath) && !"".equals(pass)){
					int n = ++m;
					Row row2 = sheet.getRow(n); 
					System.out.println("m========"+m);
					System.out.println("n========"+n);
					Cell D = row2.createCell(3);
					Cell E = row2.createCell(4);
					D.setCellValue(PDFPath);
					E.setCellValue(pass);
					System.out.println("log写入:"+PDFPath+"---"+pass);
				}
				
//			}
			// 创建文件输出流,准备输出电子表格:这个必须有,否则你在sheet上做的任何操作都不会有效
			out = new FileOutputStream(finalXlsxPath);
			workBook.write(out);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fileInputStream.close();
				if (out != null) {
					out.flush();
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		System.out.println("数据导出成功");
	}

	public boolean copyExcel(String fromPath,String toPath) throws IOException {
		//String fromPath = "D:\\share\\jiemu_new\\"; excel存放路径
		//String toPath = "c:\\ok\\"; 保存新EXCEL路径
		// 创建新的excel
		Workbook wbCreat = new XSSFWorkbook();
		File file = new File(fromPath);
			// 打开已有的excel
			InputStream in = new FileInputStream(file);
			XSSFWorkbook wb = new XSSFWorkbook(in);
				Sheet sheet = wb.getSheetAt(0);
				Sheet sheetCreat = wbCreat.createSheet(sheet.getSheetName());
				int firstRow = sheet.getFirstRowNum();
				int lastRow = sheet.getLastRowNum();
				for (int i = firstRow; i <= lastRow; i++) {
					// 创建新建excel Sheet的行
					Row rowCreat = sheetCreat.createRow(i);
					// 取得源有excel Sheet的行
					Row row = sheet.getRow(i);
					// 单元格式样
					int firstCell = row.getFirstCellNum();
					int lastCell = row.getLastCellNum();
					for (int j = firstCell; j < lastCell; j++) {
						System.out.println(row.getCell(j));
						rowCreat.createCell(j);
						String strVal = "";
						if (row.getCell(j) == null) {

						} else {
							strVal = row.getCell(j).getStringCellValue();
						}
						rowCreat.getCell(j).setCellValue(strVal);
					}
				}
		FileOutputStream fileOut = new FileOutputStream(toPath);
		wbCreat.write(fileOut);
		fileOut.close();
		return false;
	}
	
	public  boolean copyfile(String fromPath,String toPath){
		try { 
			FileInputStream fileInputStream=new FileInputStream(fromPath); //文件全路径
			
			File file = new File(fromPath);
			String absolutePath = file.getAbsolutePath();
			int index = absolutePath.lastIndexOf("\\");
			String filename = absolutePath.substring(index);
			String toPath1 = toPath+"\\"+filename;
			File tofile = new File(toPath1);
			WriteExcel.judeFileExists(tofile);
			
			FileOutputStream fileOutputStream=new FileOutputStream(toPath+"\\"+filename); //路径
			int len=0; 
			byte temp []=new byte[1024*8];; 
			while((len=fileInputStream.read(temp))!=-1){ 
				System.out.println("len="+len); 
				//It is right 
				fileOutputStream.write(temp,0,len); 
				//It is wrong 
				//fileOutputStream.write(temp); 
			}
			fileOutputStream.close(); 
			fileInputStream.close(); 
			return true;
		} catch (Exception e) {
				e.printStackTrace();
		} 

		return false;
		
	}
	

	public void setExcelHeader(String filePath){
		try {
			
			
			// 创建Excel的工作书册 Workbook,对应到一个excel文档
			System.out.println("开始执行test。");
			XSSFWorkbook wb = new XSSFWorkbook();
			
			Sheet sheet = wb.createSheet("sheet1");
			/*Sheet sheet = wb.getSheetAt(0);
			if(sheet==null){//如果不存在sheet1,建立sheet1
				sheet=wb.createSheet("sheet1");
			}*/
			FileOutputStream out = null; 
			
			/**
			 * 往Excel中写新数据
			 */
			Row row = sheet.getRow(0);
			if(row == null){//如果行不存在,建立行
				row = sheet.createRow(0); 
			}
			for (int i = 0; i < 5; i++) {
				Cell cell = row.getCell((short) i);
				if(cell==null){
					cell = row.createCell(i);
				}
				switch (i){
				case 0:
					cell.setCellValue("Object Id");break;
				case 1:
					cell.setCellValue("TIF File Path");break;
				case 2:
					cell.setCellValue("Pass(Y/N)");break;
				case 3:
					cell.setCellValue("PDF File Path");break;
				case 4:
					cell.setCellValue("Pass(Y/N)");break;
				}
			}
			
			out = new FileOutputStream(filePath);
			System.out.println("数据写入excel。");
			wb.write(out);
			out.close();
			System.out.println("end。。");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	 // 判断文件是否存在
    public static void judeFileExists(File file) {

        if (file.exists()) {
            System.out.println("file exists");
        } else {
            System.out.println("file not exists, create it ...");
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    // 判断文件夹是否存在
    public static void judeDirExists(String path) {
    	File file = new File(path);
        if (file.exists()) {
            if (file.isDirectory()) {
                System.out.println("dir exists");
            } else {
                System.out.println("the same name file exists, can not create dir");
            }
        } else {
            System.out.println("dir not exists, create it ...");
            file.mkdirs();
        }

    }
}


期间遇到很多bug,忘了记录下来。


参考资料链接:

Java 读取excel指定行列数据以及将数据保存到txt文件中https://blog.csdn.net/xiaoxun2802/article/details/70748389

Java中POI操作Excel常用方法:https://blog.csdn.net/zxh66/article/details/48494699

OpenCSV正确处理反斜线:https://blog.csdn.net/chszs/article/details/79546866

POI使用详解https://www.cnblogs.com/huajiezh/p/5467821.html



猜你喜欢

转载自blog.csdn.net/weixin_42126947/article/details/80597358
今日推荐