java 读取Excel 实现 按表格内容拷贝文件到指定目录

package com.wisec.main;

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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.DecimalFormat;

/**
 * caobin
 * 2019/1/21
 **/
public class Classify {

        public static void main(String[] args) throws Exception{

            String excel = args[0];
            String face="";
            String fileType = excel.substring(excel.lastIndexOf(".") + 1);
            File faceDir = new File(face);
            //读取excel文件
            InputStream is  = new FileInputStream(excel);
            //获取工作薄
            Workbook wb = null;
            if (fileType.equals("xls")) {
                wb = new HSSFWorkbook(is);
            } else if (fileType.equals("xlsx")) {
                wb = new XSSFWorkbook(is);
            } else {
            }
            //读取第一个工作页sheet
            Sheet sheet = wb.getSheetAt(0);

//            //遍历所有sheet
//            Iterator si = wb.sheetIterator();
//            while (si.hasNext()){
//                Sheet next = si.next();
//                //主逻辑
//            }


            //第一行为标题
            boolean flag = true;
            //DecimalFormat df = new DecimalFormat("##################");
            for (Row row : sheet) {
            	//过滤标题行
                if(flag){
                    flag = false;
                    continue;
                }
                int p = 1;
                String photoName = "";
                String classify = "";
                String name = "";
                String id = "";
                for (Cell cell : row) {
                	//根据需求拿到文件名组成
                    //根据不同类型转化成字符串
                    if (p == 1) {
                        name = cell.getStringCellValue();
                    }
                    if (p == 2) {
                        id = cell.getStringCellValue();
                    }
                    if (p == 4){
                        classify = cell.getStringCellValue();
                    }
                    p++;
                }
                //组装文件名
                photoName = id + "_" + name + ".jpg";
                String newName =   name + id + ".jpg";
                int bytesum = 0;
                int byteread = 0;
                File oldfile = new File(args[1] ,"/"+photoName);
                File newfile = new File("./分类/" +classify+"/"+newName);
                String newDir = "./分类/"+classify;
                if (oldfile.exists()) { //文件存在时
                    FileInputStream inStream = new FileInputStream(oldfile); //读入原文件
                    System.out.println("原路径:" + oldfile.getAbsolutePath());
                    if(! new File(newDir).exists()){
                        new File(newDir).mkdirs();
                    }
                    FileOutputStream fs = new FileOutputStream(new File(newDir,newfile.getName()));

                    System.out.println("新路径:" + newfile.getAbsolutePath());

                    byte[] buffer = new byte[5120];
                    while ( (byteread = inStream.read(buffer)) != -1) {
                        bytesum += byteread; //字节数 文件大小
                        fs.write(buffer, 0, byteread);
                    }
                    fs.close();
                    inStream.close();
                }else {
                    System.out.println("没找到指定文件:" + oldfile.getAbsolutePath());
                }
            }
            is.close();
        }
}

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.wisecgroupId>
    <artifactId>ClassifyFileartifactId>
    <version>1.0-SNAPSHOTversion>

    <dependencies>
        <dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poiartifactId>
            <version>3.17version>
        dependency>
        <dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poi-excelantartifactId>
            <version>3.17version>
        dependency>
    dependencies>

project>

你可能感兴趣的:(java)