java分割excel文件可用jxl

excel导入是经常使用到的功能,如果文件数据量大的话还是建议分割后导入,java常用的API是poi和jxl,我采用的是jxl,那么让我们来看下怎么用jxl来实现分割。

  • 需要在pom中导入jxl的包 
<!--excel-->
<dependency>
    <groupId>net.sourceforge.jexcelapi</groupId>
    <artifactId>jxl</artifactId>
    <version>2.6.12</version>
</dependency>
  • 我们需要两个实体类 ,分别是excel信息和文件信息

public class FileInfoModel {

    /**
     * 文件在数据库中的Id
     */
    private Long fileId;
    /**
     * 是否上传
     */
    private boolean isUpload;
    /**
     * 存储循环过程中的索引值
     */
    private int forIndex;
    /**
     * 文件全名 xxx.xls
     */
    private String fileFullName;
    /**
     * 文件名 xxx
     */
    private String fileName;
    /**
     * 文件后缀 .xls
     */
    private String fileSuffix;
    /**
     * 文件保存路径 e:\\xx\xx
     */
    private String filePath;
    /**
     * 文件的起始行号
     */
    private int beginRow;
    /**
     * 文件的末尾行号
     */
    private int endRow;

    public FileInfoModel() {
        super();
    }

    public FileInfoModel(String fileFullName) {
        super();
        this.fileFullName = fileFullName;
    }

    public FileInfoModel(String fileName, String fileFullName) {
        super();
        this.fileName = fileName;
        this.fileFullName = fileFullName;
    }

    public FileInfoModel(String fileFullName, String fileName, String fileSuffix) {
        super();
        this.fileFullName = fileFullName;
        this.fileName = fileName;
        this.fileSuffix = fileSuffix;
    }

    public FileInfoModel(String fileFullName, String fileName, String fileSuffix, String filePath) {
        super();
        this.fileFullName = fileFullName;
        this.fileName = fileName;
        this.fileSuffix = fileSuffix;
        this.filePath = filePath;
    }

    public Long getFileId() {
        return fileId;
    }

    public void setFileId(Long fileId) {
        this.fileId = fileId;
    }

    public boolean isUpload() {
        return isUpload;
    }

    public void setUpload(boolean isUpload) {
        this.isUpload = isUpload;
    }

    public int getForIndex() {
        return forIndex;
    }

    public void setForIndex(int forIndex) {
        this.forIndex = forIndex;
    }

    public String getFileFullName() {
        return fileFullName;
    }

    public void setFileFullName(String fileFullName) {
        this.fileFullName = fileFullName;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFileSuffix() {
        return fileSuffix;
    }

    public void setFileSuffix(String fileSuffix) {
        this.fileSuffix = fileSuffix;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public int getBeginRow() {
        return beginRow;
    }

    public void setBeginRow(int beginRow) {
        this.beginRow = beginRow;
    }

    public int getEndRow() {
        return endRow;
    }

    public void setEndRow(int endRow) {
        this.endRow = endRow;
    }
}

public class ExcelModel {

    /*excel 文件路径*/
    private String excelFilePath;
    /*分割后的文件目录*/
    private String excelChildFileDir;
    /*excel 名称*/
    private String excelName;
    /*sheet 名称*/
    private String sheetName = "Sheet1";
    /*excel 总记录数*/
    private int totalRows;
    /*分割文件后每个文件的记录数*/
    private int fileSize = 3000;
    /*分割后的文件数*/
    private int fileCount;
    /*分割后的文件集合*/
    private List<FileInfoModel> files;
    /*计算总行数时是否去表头*/
    private boolean isRemoveHeader = true;
    /*是否去除重复数据*/
    private boolean isCleareRepeat = false;
    /*导入文件过程中是否出错*/
    private boolean isError;

    public boolean isError() {
        return isError;
    }

    public void setError(boolean isError) {
        this.isError = isError;
    }

    public String getExcelFilePath() {
        return excelFilePath;
    }

    public void setExcelFilePath(String excelFilePath) {
        this.excelFilePath = excelFilePath;
    }

    public boolean isCleareRepeat() {
        return isCleareRepeat;
    }

    public void setCleareRepeat(boolean isCleareRepeat) {
        this.isCleareRepeat = isCleareRepeat;
    }

    public String getExcelChildFileDir() {
        return excelChildFileDir;
    }

    public void setExcelChildFileDir(String excelChildFileDir) {
        this.excelChildFileDir = excelChildFileDir;
    }

    public boolean isRemoveHeader() {
        return isRemoveHeader;
    }

    public void setRemoveHeader(boolean isRemoveHeader) {
        this.isRemoveHeader = isRemoveHeader;
    }

    public int getFileCount() {
        //根据总记录数及分割文件的行数计算文件数量
        fileCount = (int) Math.ceil(this.totalRows / this.fileSize) + 1;
        return fileCount;
    }

    public String getExcelName() {
        return excelName;
    }

    public void setExcelName(String excelName) {
        this.excelName = excelName;
    }

    public String getSheetName() {
        return sheetName;
    }

    public void setSheetName(String sheetName) {
        this.sheetName = sheetName;
    }

    public int getTotalRows() {
        if (this.isRemoveHeader) {
            return totalRows - 1;
        } else {
            return totalRows;
        }
    }

    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }

    public int getFileSize() {
        return fileSize;
    }

    public void setFileSize(int fileSize) {
        this.fileSize = fileSize;
    }

    public List<FileInfoModel> getFiles() {
        return files;
    }

    public void setFiles(List<FileInfoModel> files) {
        this.files = files;
    }
}

  • 添加接口IExcelOperate

public interface IExcelOperate {

    /**
     * 加载excel文件
     * @param fileName
     */
    void load(String fileName) throws Exception;

    /**
     * 读取excel文件数据
     *
     * @throws Exception
     */
    public <T> List<T> readExcel() throws Exception;

    /**
     * 分割excel
     *
     * @throws Exception
     */
    public void splitExcel() throws Exception;

    /**
     * 多线程分割excel
     */
    public void splitExcelThread() throws Exception;
    /**
     * 关闭文件
     */
    void close();
}

  • 让我们来实现接口的方法吧

public class ExcelUtil<T> implements IExcelOperate {
    protected final Log log = LogFactory.getLog(getClass());

    private ExcelModel excelModel;
    private Class tClass;
    private Cell[] titleCell;
    private jxl.Workbook workBook;
    private Sheet sheet;

    public int getRows() {
        if (this.sheet != null) {
            return this.sheet.getRows();
        }
        return 0;
    }

    public ExcelUtil() {
        super();
    }

    public ExcelUtil(ExcelModel excelModel, Class tClass) {
        this.tClass = tClass;
        this.excelModel = excelModel;
    }

    public ExcelModel getExcelModel() {
        return excelModel;
    }

    public void setExcelModel(ExcelModel excelModel) {
        this.excelModel = excelModel;
    }

    @Override
    public void load(String fileName) throws Exception {
        try {
            File file = new File(fileName);

            workBook = Workbook.getWorkbook(file);
            this.excelModel.setFiles(new ArrayList<FileInfoModel>());
            //默认读取文件的路径
            String[] ary = file.getName().split("\\.");
            String suffix = ary[1];
            this.excelModel.getFiles().add(new FileInfoModel(file.getName(), fileName, "." + suffix, file.getParent()));

            if (this.excelModel.isCleareRepeat()) {
                this.clearRepeat(file);
                workBook = Workbook.getWorkbook(file);
            }

            if (workBook == null) {
                throw new Exception("读取excel文件出错!");
            }
            sheet = this.excelModel.getSheetName().trim() == "" ? workBook.getSheet(0) : workBook.getSheet(this.excelModel.getSheetName());
            if (sheet == null) {
                sheet = workBook.getSheet(0);
            }
            if (sheet == null) {
                throw new Exception("读取sheet出错!");
            }

            this.excelModel.setTotalRows(sheet.getRows());
            // 用于存储列标题
            titleCell = new Cell[sheet.getColumns()];

            // 将列标题存储存到一个一维数组中
            for (int i = 0; i < titleCell.length; i++) {
                titleCell[i] = sheet.getCell(i, 0);
            }

        } catch (IOException e) {
            workBook.close();
        }
    }

       @Override
    public void splitExcel() throws Exception {
        //根据文件数分割 excel,重置文件集合
        this.excelModel.setFiles(new ArrayList<FileInfoModel>());
        //生成Guid作为文件前缀
        UUID uuid = UUID.randomUUID();
        FileInfoModel fileInfoModel = new FileInfoModel();
        for (int i = 1; i <= this.excelModel.getFileCount(); i++) {
            fileInfoModel = getFileInfoModelBySplit(i, uuid);
            this.excelModel.getFiles().add(this.split(fileInfoModel));
        }
    }

    @Override
    public void splitExcelThread() throws Exception {
        //根据文件数分割 excel,重置文件集合
        this.excelModel.setFiles(new ArrayList<FileInfoModel>());
        //生成Guid作为文件前缀
        UUID uuid = UUID.randomUUID();
        //根据文件数分割 excel,重置文件集合
        this.excelModel.setFiles(new ArrayList<FileInfoModel>());
        //工作线程
        ExecutorService executorService = Executors.newFixedThreadPool(this.excelModel.getFileCount());
        FileInfoModel fileInfoModel = new FileInfoModel();
        for (int i = 1; i <= this.excelModel.getFileCount(); i++) {
            fileInfoModel = getFileInfoModelBySplit(i, uuid);
            ExcelThread thread = new ExcelThread(fileInfoModel);
            executorService.execute(thread);
            this.excelModel.getFiles().add(thread.getReturnFile());
        }
        executorService.shutdown();
        while (!executorService.isTerminated()) {
            //检查所有线程都执行完成
        }
    }

  /**
     * 分割excel时获取文件信息
     *
     * @param i
     * @return
     */
    private FileInfoModel getFileInfoModelBySplit(int i, UUID uuid) {

        /*结束行*/
        int endRow = i * this.excelModel.getFileSize();
                 /*起始行*/
        int beginRow = (endRow - this.excelModel.getFileSize()) + 1;
                /*如果结束行超出总记录数,结束行就等于总记录数*/
        if (endRow >= this.excelModel.getTotalRows()) {
            endRow = this.excelModel.getTotalRows();
        }
        //获取文件路径
        String filePath = MessageFormat.format("{0}/{1}_{2}.xls", this.excelModel.getExcelChildFileDir(), uuid, i);
        FileInfoModel fileInfoModel = new FileInfoModel();
        fileInfoModel.setFilePath(filePath);
        fileInfoModel.setBeginRow(beginRow);
        fileInfoModel.setEndRow(endRow);
        fileInfoModel.setForIndex(i - 1);
        return fileInfoModel;
    }

/**
     * 分割excel
     *
     * @param fileInfoModel
     * @return
     */
    private FileInfoModel split(FileInfoModel fileInfoModel) {
        File file = new File(fileInfoModel.getFilePath());
        try {
            jxl.write.WritableWorkbook ww = Workbook.createWorkbook(file);
            WritableSheet ws = ww.createSheet(this.excelModel.getSheetName(), 0);

            //添加表头
            for (int iColumn = 0; iColumn < this.titleCell.length; iColumn++) {
                ws.addCell(new Label(iColumn, 0, this.titleCell[iColumn].getContents()));
            }

            //添加数据到excel中
            int rowIndex = 1;
            for (int iRow = fileInfoModel.getBeginRow(); iRow <= fileInfoModel.getEndRow(); iRow++, rowIndex++) {
                Cell[] cells = this.sheet.getRow(iRow);
                for (int iCell = 0; iCell < cells.length; iCell++) {
                    Cell cell = cells[iCell];
                    //excel 行的索引需要计算
                    ws.addCell(new Label(iCell, rowIndex, cell.getContents()));
                }
            }

            ww.write();
            ww.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RowsExceededException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (WriteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String[] ary = file.getName().split("\\.");
        String fileName = ary[0];
        String suffix = ary[1];

        fileInfoModel.setFileFullName(file.getName());
        fileInfoModel.setFileName(fileName);
        fileInfoModel.setFileSuffix("." + suffix);
        return fileInfoModel;
    }

 class ExcelThread implements Runnable {// 任务接口
        FileInfoModel returnFile;

        public FileInfoModel getReturnFile() {
            return returnFile;
        }

        ExcelThread(FileInfoModel fileInfoModel) {
            this.returnFile = fileInfoModel;
        }

        public void run() {
            long beginTime = System.currentTimeMillis();
            this.returnFile = split(this.returnFile);
            System.out.println(MessageFormat.format("分割文件{0},执行耗时{1}秒", this.returnFile.getForIndex(), (System.currentTimeMillis() - beginTime) / 1000f));
        }
    }
}

时间: 2024-11-08 07:26:44

java分割excel文件可用jxl的相关文章

java读写excel文件

需求:利用Java读写excel文件 利用jexcelapi实现Java读写excel文件的功能 首先下载并安装jexcelapi JExcelApi v2.6.12 (1911kbytes) 解压后把jxl.jar文件添加到Java Build Path中 Java读取excel文件 Java写入excel文件

java生成excel文件工具类实例

import java.io.File; import java.io.IOException; import jxl.Workbook; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import jxl.write.biff.RowsExceededException; import org.

java 后台 Excel 文件生成后转化为字节流

Java 后台 Excel 文件生成后转化为字节流 java excel 使用 poi组件, HSSFWorkbook workBook = new HSSFWorkbook(); 对于workBook生成字节流,很容易发现有个workBook .getBytes(),但是,是不可用的,下载以后打不开, 如果下载,正确的写法为 workBook.write(response.getOutputStream()); 如果转化为字节流: ByteArrayOutputStream os = new

Java编程:使用Java读取Excel文件内容

微软的ODBC驱动程序把工作表中的第一行作为列名(译者注:即字段名),工作表名作为数据库表名. 要通过JDBC访问工作表,我们还必须创建一个新的ODBC数据源,在Windows 2000系统上创建数据源的过程如下: 进入“控制面板” --> “管理工具” --> “数据源(ODBC)”,(译者注:打开后选择系统DSN),点击添加,在弹出窗口中选择“Driver do Microsoft Excel(*.xls)” 然后在数据源名处输入一个名字myexcel(译者注:相当于数据库名),然后点击“

Java读取excel文件,并存入MySQL数据库

2019,刚毕业入职,需要更新数据库某表内容,就写了个Java读取excel文件的代码,代码尚存问题较大,过往阅者看看即可,以此记录小白点滴 初学Java,还没学到io流,jdbc等操作 代码用到poi 一些jar,数据库jar import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.s

JExcel入门,JAVA读写Excel文件

(本人下的是jexcelapi_2_6_12.tar.gz,解压后将里面的jxl.jar复制到WEB-INF/lib目录下面即可) Java Excel API的jar包可以通过以下URL获得:(推荐) http://sourceforge.net/projects/jexcelapi/files/jexcelapi/2.6.6/jexcelapi_2_6_6.zip/download (包括所有版本):http://sourceforge.net/projects/jexcelapi/file

java读取Excel文件

package 读取excel; /* *导入jxl包,注意不能读取最新版本的Excel文件 */ import java.io.File;    import java.io.FileInputStream;     import java.io.InputStream;    import jxl.Cell; import jxl.CellType;    import jxl.Sheet;    import jxl.Workbook;    import jxl.write.Label;

Java生成Excel文件

1.设计源码 /** * * @title:ExcelUtils.java * @Package:com.you.utils * @Description:<h3>一句话描述功能</h3> * @author:游海东 * @date:2015-3-21下午10:17:34 * @version V1.0 * */ package com.you.utils; import java.io.File; import java.io.IOException; import jxl.Wo

java 操作excel 文件

JAVA EXCEL API:是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件.使用该API非Windows操作系统也可以通过纯Java应用来处理Excel数据表.因为它是使用Java编写的,所以我们在Web应用中可以通过JSP.Servlet来调用API实现对Excel数据表的访问. 下载: 官方网站 http://www.andykhan.com/jexcelapi/ 下载最新版本(本人下的是jexcelapi_2_6