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<Sheet> 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

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>

    <groupId>com.wisec</groupId>
    <artifactId>ClassifyFile</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-excelant</artifactId>
            <version>3.17</version>
        </dependency>
    </dependencies>

</project>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值