`
madbluesky
  • 浏览: 84017 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

poiutil

    博客分类:
  • java
阅读更多

package com.miaozhen.sitedna.poi;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.contrib.HSSFCellUtil;
import org.apache.poi.hssf.util.Region;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class PoiUtil {
    public static void setRegionStyle(HSSFSheet sheet, Region region, HSSFCellStyle cs) {
        int toprowNum = region.getRowFrom();
        for (int i = region.getRowFrom(); i <= region.getRowTo(); i++) {
            HSSFRow row = HSSFCellUtil.getRow(i, sheet);
            for (int j = region.getColumnFrom(); j <= region.getColumnTo(); j++) {
                HSSFCell cell = HSSFCellUtil.getCell(row, (short) j);
                cell.setCellStyle(cs);
            }
        }
    }

    public static void colSpan(HSSFSheet sheet, short rownum) {
        short fromIndex = 0, toIndex = 0;
        HSSFRow row = sheet.getRow(rownum);
        String v = "";
        for (short i = 0; i < row.getLastCellNum(); i++) {
            HSSFCell cell = row.getCell(i);
            if (cell != null
                    && (cell.getCellType() != HSSFCell.CELL_TYPE_STRING || !cell.getStringCellValue().equals(v))) {
                if (toIndex > fromIndex) {
                    Region region = new Region(rownum, fromIndex, rownum, toIndex);
                    sheet.addMergedRegion(region);
                }
                fromIndex = i;
                v = cell.getStringCellValue();
            } else {
                toIndex = i;
            }
        }
        if (toIndex > fromIndex) {
            Region region = new Region(rownum, fromIndex, rownum, toIndex);
            sheet.addMergedRegion(region);
        }
    }

    public static void rowSpan(HSSFSheet sheet, short column) {
        short fromIndex = 0, toIndex = 0;
        String v = "";
        for (short i = 0; i <= sheet.getLastRowNum(); i++) {
            HSSFRow row = sheet.getRow(i);
            HSSFCell cell = row != null ? row.getCell(column) : null;
            if (cell != null
                    && (cell.getCellType() != HSSFCell.CELL_TYPE_STRING || !cell.getStringCellValue().equals(v))) {
                if (toIndex > fromIndex) {
                    Region region = new Region(fromIndex, column, toIndex, column);
                    sheet.addMergedRegion(region);
                }
                fromIndex = i;
                v = cell.getStringCellValue();
            } else {
                toIndex = i;
            }
        }
        if (toIndex > fromIndex) {
            Region region = new Region(fromIndex, column, toIndex, column);
            sheet.addMergedRegion(region);
        }
    }

    // toColumn设置为255则表示到最后一列
    public static void colSpan(HSSFSheet sheet, int rownum, short startColumn, short toColumn) {
        short fromIndex = 0, toIndex = 0;
        HSSFRow row = sheet.getRow(rownum);
        if (toColumn > row.getLastCellNum()) {
            toColumn = row.getLastCellNum();
        }
        String v = "";
        for (short i = 0; i < toColumn; i++) {
            HSSFCell cell = row.getCell(i);
            if (cell != null
                    && (cell.getCellType() != HSSFCell.CELL_TYPE_STRING || !cell.getStringCellValue().equals(v))) {
                if (toIndex > fromIndex) {
                    Region region = new Region(rownum, fromIndex, rownum, toIndex);
                    sheet.addMergedRegion(region);
                }
                fromIndex = i;
                v = cell.getStringCellValue();
            } else {
                toIndex = i;
            }
        }
        if (toIndex > fromIndex) {
            Region region = new Region(rownum, fromIndex, rownum, toIndex);
            sheet.addMergedRegion(region);
        }
    }

    public static void rowSpan(HSSFSheet sheet, short column, int startRow, int toRow) {
        int fromIndex = 0, toIndex = 0;
        if (toRow > sheet.getLastRowNum()) {
            toRow = sheet.getLastRowNum();
        }
        String v = "";
        for (int i = startRow; i <= toRow; i++) {
            HSSFRow row = sheet.getRow(i);
            HSSFCell cell = row != null ? row.getCell(column) : null;
            if (cell != null
                    && (cell.getCellType() != HSSFCell.CELL_TYPE_STRING || !cell.getStringCellValue().equals(v))) {
                if (toIndex > fromIndex) {
                    Region region = new Region(fromIndex, column, toIndex, column);
                    sheet.addMergedRegion(region);
                }
                fromIndex = i;
                v = cell.getStringCellValue();
            } else {
                toIndex = i;
            }
        }
        if (toIndex > fromIndex) {
            Region region = new Region(fromIndex, column, toIndex, column);
            sheet.addMergedRegion(region);
        }
    }

    public static void clearSheetBody(HSSFSheet sheet, int fromrow) {
        // 清除模板中的数据
        for (int i = fromrow; i <= sheet.getLastRowNum(); i++) {
            if (sheet.getRow(i) != null) {
                sheet.removeRow(sheet.getRow(i));
            }
        }
    }

    public static void clearSheetBody(HSSFSheet sheet, int fromRow, int toRow) {
        // 清除模板中的数据
        for (int i = fromRow; i <= toRow; i++) {
            if (sheet.getRow(i) != null) {
                sheet.removeRow(sheet.getRow(i));
            }
        }
    }

    // 清除所有的区域
    public static void clearAllRegion(HSSFSheet sheet) {
        int n = sheet.getNumMergedRegions();
        for (int i = n; i >= 0; i--) {
            sheet.removeMergedRegion(i);
        }
    }
/**
 *
 * @param sheet 区域所在的sheet
 * @param rownum 指定单元格所在行
 * @param column 指定单元格所在列   
 *  删除指定单元格所在的区域
 */
    public static void clearRegionAssign(HSSFSheet sheet, int rownum, int column) {
        int n = sheet.getNumMergedRegions();
        for (int i = n; i >= 0; i--) {
            Region region = sheet.getMergedRegionAt(i);
            if (region.getRowFrom() <= rownum && region.getRowTo() >= rownum && region.getColumnFrom() <= column
                    && region.getColumnTo() >= column) {
                sheet.removeMergedRegion(i);
                break;
            }
        }
    }

    public static void main(String[] args) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("输入需要操作的excel文件路径:");
            String filePath = br.readLine();
            System.out.print("输入需要操作的来源sheet名称:");
            String sourceSheetName = br.readLine();
            System.out.print("输入来源的起始行号(从0开始):");
            int startRow = Integer.parseInt(br.readLine());
            System.out.print("输入来源的终止行号(从0开始):");
            int endRow = Integer.parseInt(br.readLine());
            System.out.print("输入需要操作的目标sheet名称:");
            String targetSheetName = br.readLine();
            System.out.print("输入目标的行号偏移量:");
            int position = Integer.parseInt(br.readLine());

            POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filePath));
            HSSFWorkbook wb = new HSSFWorkbook(fs);

            // copyRows(wb, sourceSheetName, targetSheetName, startRow, endRow, position);
            FileOutputStream fileOut = new FileOutputStream(filePath);
            wb.write(fileOut);
            fileOut.flush();
            fileOut.close();
            System.out.println("操作完成!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void clearAllComment(HSSFSheet sheet) {
        /*
         * int rowIndex = sheet.getLastRowNum(); int colIndex = sheet.getRow(0).getLastCellNum(); for(int i=0;i<=rowIndex;i++){
         * for(int j=0;j<=colIndex;j++){ HSSFComment comment = sheet.getCellComment(i, j); if(comment!=null){
         * sheet.getRow(i).getCell(j).removeCellComment(); } } }
         */
    }

    public static void copyRows(HSSFSheet sourceSheet, HSSFSheet targetSheet, int pStartRow, int pEndRow, int pPosition) {
        HSSFRow sourceRow = null;
        HSSFRow targetRow = null;
        HSSFCell sourceCell = null;
        HSSFCell targetCell = null;
        // HSSFSheet sourceSheet = null;
        // HSSFSheet targetSheet = null;
        Region region = null;

        if ((pStartRow == -1) || (pEndRow == -1)) {
            return;
        }
        // sourceSheet = wb.getSheetAt(pSourceSheetId);
        // targetSheet = wb.getSheetAt(pTargetSheetId);
        // 拷贝合并的单元格
        for (int i = 0; i < sourceSheet.getNumMergedRegions(); i++) {
            region = sourceSheet.getMergedRegionAt(i);
            if ((region.getRowFrom() >= pStartRow) && (region.getRowTo() <= pEndRow)) {
                int targetRowFrom = region.getRowFrom() - pStartRow + pPosition;
                int targetRowTo = region.getRowTo() - pStartRow + pPosition;
                region.setRowFrom(targetRowFrom);
                region.setRowTo(targetRowTo);
                targetSheet.addMergedRegion(region);
            }
        }
        // 拷贝行并填充数据
        for (int i = pStartRow; i <= pEndRow; i++) {
            sourceRow = sourceSheet.getRow(i);
            if (sourceRow == null) {
                continue;
            }
            targetRow = targetSheet.createRow(i - pStartRow + pPosition);
            targetRow.setHeight(sourceRow.getHeight());
            for (short j = sourceRow.getFirstCellNum(); j < sourceRow.getPhysicalNumberOfCells(); j++) {
                sourceCell = sourceRow.getCell(j);
                if (sourceCell == null) {
                    continue;
                }
                targetCell = targetRow.createCell(j);
                targetCell.setCellStyle(sourceCell.getCellStyle());
                int cType = sourceCell.getCellType();
                targetCell.setCellType(cType);
                switch (cType) {
                case HSSFCell.CELL_TYPE_BOOLEAN:
                    targetCell.setCellValue(sourceCell.getBooleanCellValue());
                    // System.out.println("--------TYPE_BOOLEAN:" + targetCell.getBooleanCellValue());
                    break;
                case HSSFCell.CELL_TYPE_ERROR:
                    targetCell.setCellErrorValue(sourceCell.getErrorCellValue());
                    // System.out.println("--------TYPE_ERROR:" + targetCell.getErrorCellValue());
                    break;
                case HSSFCell.CELL_TYPE_FORMULA:
                    targetCell.setCellFormula(sourceCell.getCellFormula());
                    // System.out.println("--------TYPE_FORMULA:" + targetCell.getCellFormula());
                    break;
                case HSSFCell.CELL_TYPE_NUMERIC:
                    targetCell.setCellValue(sourceCell.getNumericCellValue());
                    // System.out.println("--------TYPE_NUMERIC:" + targetCell.getNumericCellValue());
                    break;
                case HSSFCell.CELL_TYPE_STRING:
                    targetCell.setCellValue(sourceCell.getStringCellValue());
                    // System.out.println("--------TYPE_STRING:" + targetCell.getStringCellValue());
                    break;
                }
            }
        }
    }
}

分享到:
评论

相关推荐

    PoiUtil.zip

    《PoiUtil:Java利用Apache POI处理Excel的实用工具》 在Java开发中,处理Excel数据是一项常见的任务,尤其在数据导入导出、报表生成等场景。Apache POI是一个强大的库,允许开发者读写Microsoft Office格式的文件...

    POIUtil.java

    Excel导入解析工具,可以接收前台导入的.xls和.xlsx文件进行读取,然后存储到List集合,方便向数据库中储存,欢迎下载使用

    poiUtil.rar

    工具:jdk1.8+maven 知识点:反射+自定义注解+poi使用+使用了一点guava编程(膜拜下谷歌大哥) 功能:可以解析任意List对象-excel表格;解析任意的Excel的表格-》list对象 在 poi包里面

    PoiUtil.rar

    《PoiUtil:Java利用Apache POI处理Excel的利器》 在Java开发中,处理Excel文件是一项常见的任务,尤其在数据导入导出、数据分析等领域。Apache POI 是一个流行的开源库,专门用于读写Microsoft Office格式的文件,...

    PoiUtil.java

    针对poi导入导出基本使用的工具类

    POIutil.rar

    在实际应用中,开发者可能会从数据库或其他数据源获取数据,然后通过`PoiUtil`的`writeDataToSheet(List&lt;DataRow&gt; dataList, Sheet sheet)`方法将数据写入工作表。`DataRow`是一个假设的类,表示一行的数据,可能...

    poi导出和导入Excle使用文档

    ### 使用POI进行Excel的导出与导入 #### 1. POI介绍及结构描述 ##### 1.1 POI简介 Apache POI是Apache软件基金会的Jakarta项目中的一个子项目,它提供了一组API来处理Microsoft Office格式的文件。POI的主要目标是...

    POI和JXL读取EXCEL数据的工具类(POI支持2007)

    本主题将详细讲解如何使用Apache POI和JExcelApi(JXL)这两个Java库来读取Excel文件,以及提供的工具类`PoiUtil.java`和`JxlUtil.java`的使用方法。 Apache POI是一个流行的开源库,它允许开发者创建、修改和显示...

    java poi导出Excel上传实例 可直接使用

    Apache POI是Java领域中广泛使用的库,它允许开发者创建、修改和展示Microsoft Office格式的文件,特别是Excel工作簿。本实例将详细讲解如何利用Apache POI库在Java项目中导出Excel文件。 首先,我们需要引入Apache...

    POI-Excel.rar

    `PoiUtil.java`可能是工具类,封装了一些通用的POI操作方法,比如创建工作簿、工作表,以及处理数据行和单元格等。其中可能包含了一个方法,用于读取图片文件(例如从硬盘路径或资源流中),并将其添加到工作簿的...

    通用的POI导入Excel解决方案-ExcelUtilTest

    在项目中,经常免不了要导入、导出Excel,导出Excel稍微简单点,制作一张模板,然后将查询之后的数据写入到模板中即可,导出程序可以做到一次写好,基本上就可以通用。 但导入Excel相对麻烦点,一般情况下,导入的...

    poi复制excel的sheet页

    在提供的`PoiUtil.java`文件中,可能包含了实现上述步骤的实用工具类。这个类可能会包含如下的静态方法: ```java public class PoiUtil { public static void copySheetAndSave(String sourceFilePath, String ...

    java各种工具类:精确的数学运算、将String进行base64编码解码、树的递归、判断是否为空等

    java各种工具类:精确的数学运算、将String进行base64编码解码、树的递归等 ...KmlUtil、LogUtils、NumberUtil、PageUtil、PathUtility、PermissionUtils、PoiUtil、R、RedisUtil、SplitUnitUtil、SpringBeanFact

    poi-util-0.0.1-SNAPSHOT.jar

    poi-util-0.0.1-SNAPSHOT.jar

    邮件发送 poi 二维码 条形码等java常用的工具类

    POIUtil:poi工具类,excel导出 QrCodeUtil:二维码操作工具, 包括生成和读取 ShellUtil:shell命令操作工具,包括linux登陆,命令执行...... 较为简单,具体需要自行扩充 SignUtil:签名工具,包括MD5 位运算 ...

    POI和JXL读取EXCEL数据的工具类(POI支持2007)

    例如,`PoiUtil.java`工具类可能包含以下代码片段: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class PoiUtil { public static List...

    解析Excel2007相关代码和jar包

    总的来说,解析Excel 2007文件,尤其是XLSX格式,涉及到Apache POI库的使用,理解`XSSFWorkbook`、`XSSFSheet`、`XSSFRow`和`XSSFCell`等核心类的功能,以及如何通过`POIUtil.java`这样的代码实现文件的读写操作。...

    poi实现EXCEL保护工作表实例代码.zip

    `PoiUtil.java`文件很可能是这个例子的核心,它包含了使用POI保护Excel工作表的逻辑。以下是一些可能包含在该文件中的关键知识点: 1. **导入必要的库**:首先,你需要导入Apache POI相关的库,如`org.apache.poi....

Global site tag (gtag.js) - Google Analytics