Java 操作POI 之复制sheet页

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

来点干货直接上代码,就不细说了

package com.qs.web.tools.core.excel;

import java.util.HashMap;

import java.util.Map;

import java.util.Set;

import java.util.TreeSet;


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.ss.util.CellRangeAddress;


public   class ExcelCopySheetUtil {  

  

    public ExcelCopySheetUtil() {  

    }  

  

    public static void copySheets(HSSFSheet newSheet, HSSFSheet sheet) {  

        copySheets(newSheet, sheet, true);  

    }  

  

    public static void copySheets(HSSFSheet newSheet, HSSFSheet sheet,  

            boolean copyStyle) {  

        int maxColumnNum = 0;  

        Map<Integer, HSSFCellStyle> styleMap = (copyStyle) ? new HashMap<Integer, HSSFCellStyle>()  

                : null;  

        for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {  

            HSSFRow srcRow = sheet.getRow(i);  

            HSSFRow destRow = newSheet.createRow(i);  

            if (srcRow != null) {  

                ExcelCopySheetUtil.copyRow(sheet, newSheet, srcRow, destRow,  

                        styleMap);  

                if (srcRow.getLastCellNum() > maxColumnNum) {  

                    maxColumnNum = srcRow.getLastCellNum();  

                }  

            }  

        }  

        for (int i = 0; i <= maxColumnNum; i++) {    //设置列宽  

            newSheet.setColumnWidth(i, sheet.getColumnWidth(i));  

        }  

    }  

  

    /** 

     * 复制并合并单元格 

     * @param  newSheet 

     * @param  sheet 

     * @param  copyStyle 

     */  

    public static void copyRow(HSSFSheet srcSheet, HSSFSheet destSheet,  

            HSSFRow srcRow, HSSFRow destRow,  

            Map<Integer, HSSFCellStyle> styleMap) {  

        Set<CellRangeAddressWrapper> mergedRegions = new TreeSet<CellRangeAddressWrapper>();  

        destRow.setHeight(srcRow.getHeight());  

        int deltaRows = destRow.getRowNum() - srcRow.getRowNum(); //如果copy到另一个sheet的起始行数不同  

        for (int j = srcRow.getFirstCellNum(); j <= srcRow.getLastCellNum(); j++) {  

            HSSFCell oldCell = srcRow.getCell(j); // old cell  

            HSSFCell newCell = destRow.getCell(j); // new cell  

            if (oldCell != null) {  

                if (newCell == null) {  

                    newCell = destRow.createCell(j);  

                }  

                copyCell(oldCell, newCell, styleMap);  

                CellRangeAddress mergedRegion = getMergedRegion(srcSheet,  

                        srcRow.getRowNum(), (short) oldCell.getColumnIndex());  

                if (mergedRegion != null) {  

                    CellRangeAddress newMergedRegion = new CellRangeAddress(  

                            mergedRegion.getFirstRow() + deltaRows,  

                            mergedRegion.getLastRow() + deltaRows, mergedRegion  

                                    .getFirstColumn(), mergedRegion  

                                    .getLastColumn());  

                    CellRangeAddressWrapper wrapper = new CellRangeAddressWrapper(  

                            newMergedRegion);  

                    if (isNewMergedRegion(wrapper, mergedRegions)) {  

                        mergedRegions.add(wrapper);  

                        destSheet.addMergedRegion(wrapper.range);  

                    }  

                }  

            }  

        }  

    } 

  

    /** 

     * 把原来的Sheet中cell(列)的样式和数据类型复制到新的sheet的cell(列)中      *  

     * @param  oldCell 

     * @param  newCell 

     * @param styleMap 

     */  

    public static void copyCell(HSSFCell oldCell, HSSFCell newCell,  

            Map<Integer, HSSFCellStyle> styleMap) {  

        if (styleMap != null) {  

            if (oldCell.getSheet().getWorkbook() == newCell.getSheet()  

                    .getWorkbook()) {  

                newCell.setCellStyle(oldCell.getCellStyle());  

            } else {  

                int stHashCode = oldCell.getCellStyle().hashCode();  

                HSSFCellStyle newCellStyle = styleMap.get(stHashCode);  

                if (newCellStyle == null) {  

                    newCellStyle = newCell.getSheet().getWorkbook()  

                            .createCellStyle();  

                    newCellStyle.cloneStyleFrom(oldCell.getCellStyle());  

                    styleMap.put(stHashCode, newCellStyle);  

                }  

                newCellStyle.setLocked(false);

                newCell.setCellStyle(newCellStyle);  

            }  

        }  

        switch (oldCell.getCellType()) {  

        case HSSFCell.CELL_TYPE_STRING:  

            newCell.setCellValue(oldCell.getStringCellValue());  

            break;  

        case HSSFCell.CELL_TYPE_NUMERIC:  

            newCell.setCellValue(oldCell.getNumericCellValue());  

            break;  

        case HSSFCell.CELL_TYPE_BLANK:  

            newCell.setCellType(HSSFCell.CELL_TYPE_BLANK);  

            break;  

        case HSSFCell.CELL_TYPE_BOOLEAN:  

            newCell.setCellValue(oldCell.getBooleanCellValue());  

            break;  

        case HSSFCell.CELL_TYPE_ERROR:  

            newCell.setCellErrorValue(oldCell.getErrorCellValue());  

            break;  

        case HSSFCell.CELL_TYPE_FORMULA:  

            newCell.setCellFormula(oldCell.getCellFormula());  

            break;  

        default:  

            break;  

        }  

  

    }  

  

    // 获取merge对象  

    public static CellRangeAddress getMergedRegion(HSSFSheet sheet, int rowNum,  

            short cellNum) {  

        for (int i = 0; i < sheet.getNumMergedRegions(); i++) {  

            CellRangeAddress merged = sheet.getMergedRegion(i);  

            if (merged.isInRange(rowNum, cellNum)) {  

                return merged;  

            }  

        }  

        return null;  

    }  

  

    private static boolean isNewMergedRegion(  

            CellRangeAddressWrapper newMergedRegion,  

            Set<CellRangeAddressWrapper> mergedRegions) {  

        boolean bool = mergedRegions.contains(newMergedRegion);  

        return !bool;  

    }  

  

}  


转载于:https://my.oschina.net/zhangdayue/blog/419354

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/297339.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Serverless 落地实践

.Net Core Serverless初体验什么是ServerlessServerless 是一个当今软件世界中比较新的话题。它并没有一个普遍公认的权威定义&#xff0c;每个人每个企业对它的解释可能都有不同&#xff0c;而 Serverless 正是在这种情况下不断发发展的。但是就算如此&#xff0c;有一些 Serv…

KOFLive Beta Daily-Scrum 9

组 员今天的工作进 度问 题明天的计划田 飞Work Item 38911&#xff1a;角色四照片拍摄 Work Item 38897:键盘输入人物四的图片已经导入&#xff0c;键盘三个连键检测完成还剩最后一个连键的检测Work Item 38911:拍摄人物角色五付 浩Work Item 37741:游戏主模块 Work Item 3889…

出这样的题,出题人的良心确定不会痛吗?

全世界只有3.14 % 的人关注了爆炸吧知识“那些年&#xff0c;让我们气到吐血的题目。”数学篇

mysql怎么查看索引情况_mysql 查看索引使用情况

mysql 查看索引使用情况这是以读为主的线上库rootread 02:28:07>show status like ‘Handler_read%’;———————–——-| Variable_name | Value |———————–——-| Handler_read_first | 0 || Handler_read_key | 0 || Handler_read_ne…

iTextSharp应用,生成pdf

using iTextSharp.text; using iTextSharp.text.pdf; using System.IO; public void PDF(string html) { Document dom new Document(); //创建文档 PdfWriter.GetInstance(dom, new FileStream(Server.MapPath("test") Guid.NewGuid() "…

python垃圾回收机制为什么标记能解决循环引用问题_Python 垃圾回收机制和如何解决循环引用...

引用计数&#xff1a;是一种垃圾收集机制&#xff0c;而且也是一种最直观&#xff0c;最简单的垃圾收集技术, 当一个对象的引用被创建或者复制时&#xff0c;对象的引用计数加 1&#xff1b;当一个对象的引用被销毁时&#xff0c;对象的引用计数减 1&#xff1b;当对象的引用计…

Dapr + .NET 实战(十-终篇)K8S运行Dapr

工作原理为了实现在k8s上安装Dapr&#xff0c;Dapr需要部署dapr-sidecar-injector、dapr-operator、dapr-placement和dapr-sentry服务。 dapr-operator: 管理组件(state stores, pub/subs, etc.)dapr-sidecar-injector: 将 Dapr 注入 annotated pods&#xff0c;并添加环境变量…

mvc control 请求两次问题

今天在做项目时&#xff0c;突然发现一个mvc 的control中action被执行了两次&#xff0c;最终发现是由于favicon.ico导致的。问题代码&#xff1a; <link rel"shortcut icon" href"Images/wangyi.ico" type"image/x-icon" />,最终改成了 &…

双系统卸载

xp卸载vista 1,xp我的电脑右键&#xff0c;属性&#xff0c;磁盘管理。 2&#xff0c;右击vista磁盘&#xff0c;格式化&#xff0c;设在格式化选项&#xff0c;确定。 3&#xff0c;在运行了输入msconfig,确定。 4&#xff0c;在系统配置实用程序对话框中切换到boot.ini选项卡…

为什么老师批改完试卷,分数下要画两条横线?今天终于知道了!

全世界只有3.14 % 的人关注了爆炸吧知识总有考试分数下画两条横线的习惯&#xff0c;原来是这样形成的&#xff01;为什么老师改卷之后会在分数下画两条横线呢&#xff1f;你要去问老师&#xff1f;别问了&#xff0c;可能连老师都不知道……来看看这位当了老师的网友怎么说的&…

mysql数据库已连接数据库_001. 【已解决】Java连接MYSQL 数据库的连接步骤

这篇文章主要以MySQL为例讲下Java如何连接到数据库的。当然&#xff0c;首先要安装有JDK(一般是JDK1.5.X)。然后安装MySQL&#xff0c;这些都比较简单&#xff0c;具体过程就不说了。配置好这两个环境后&#xff0c;下载JDBC驱动mysql-connector-java-5.0.5.zip(这个是最新版的…

Windows 11 小技巧- WSL开启Linux桌面应用

WSL 经过⼏年的发展&#xff0c;已经是⼀个Windows下不少开发⼈员做云原⽣应⽤的必备环境&#xff0c;你可以在上⾯写各种语⾔&#xff0c;各种后端框架&#xff0c;也可以完成容器和k8s的部署&#xff0c;在Windows11下更进⼀步&#xff0c;直接⽀持Linux桌⾯应⽤&#xff0c;…

lcd屏幕抖动_电视屏幕面板大科普!买电视之前必看!

BOE知识酷 ?显示技术|显示资讯| PPT|知识管理第856篇推文导读&#xff1a;经常看到什么LCD、LED、OLED、ULED、QLED、SLED、GLED……之类的名词&#xff0c;花里胡哨。别看那么多名词&#xff0c;但从显示技术角度看&#xff0c;现在市场上主流的电视机其实就两种&#xff1a;…

【转】Ubuntu 修改hosts

原文网址&#xff1a;http://l.14551.org/2009/12/2166 Ubuntu系统的Hosts只需修改/etc/hosts文件&#xff0c;在目录中还有一个hosts.conf文件&#xff0c;刚开始还以为只需要修改这个就可以了&#xff0c;结果发现是需要修改hosts。修改完之后要重启网络。具体过程如下&#…

一击即中的表白方式,学会了吗?

1 自己吃自己的&#xff0c;敢抢我吃的&#xff0c;吃我的小拳拳&#xff01;2 承受了太多哈哈哈3 狗狗在你不知道的时候会默默守护你4 眼前的方不是方。。5 一击即中的表白方式&#xff0c;学会了吗&#xff1f;&#xff1f; 图自甜星球日报社6 哦…了解了图自迷影心生 7 当90…

BIND的安装

ind建立步骤 1.解压bind安装文件tar zxvf bind-9.5.0.tar.gz2.进入bind安装目录cd bind-9.5.03.配置bind安装文件./configure --prefix/usr/local --enable-threads4.编译和安装bind文件make && make install5.建立主配置文件vi /etc/named.confoptions {directory &qu…

mysql2012用户名_SQL Server 登录名、服务器角色、用户名和数据库角色 --- 解释

1.一个数据库用户可以对应多个架构(架构是表容器)。架构里面包含的是数据库表。2.一个数据库角色有可能涉及多个架构。数据库角色对应的是权限。3.一个用户对应一个数据库角色。4.登录名与数据库用户在服务器级别是一对多的&#xff1b;在数据库级别是一对一的。服务器登录名&a…

有关转换问题

Convert.ChangeType 的运用 JOBEntity jobnew JOBEntity(); Type t job.GetType(); PropertyInfo[] info t.GetProperties(); foreach(PropertyInfo i in info){ //value 必须保证有效性&#xff0c;否则要对 PropertyType 进行 typeof 判断 if(i.PropertyType.IsGene…

Windows 11 正式版 ISO 镜像下载大全

Windows 11 ISO 镜像规格1.Windows 11 中文版文件大小&#xff1a;5.13 GB系统类型&#xff1a;64 位操作系统版本号&#xff1a;Windows 11 21H2发布日期&#xff1a;2021 年 10 月 5 日MD5: 68FB0CAD069CF82A9BA0996A6D3D112BSHA1: 9F602662FFAB0DF3B3A66FC6A42849BFFD3315A7…

怎样安装python在桌面_在Windows上安装和配置 Jupyter Lab 作为桌面级应用程序教程...

什么是 Jupyter LabJupyter Lab 是 Jupyter notebook 的升级版&#xff0c;优点这里不作赘述。一、安装 Jupyter Lab如果你安装了 Anaconda&#xff0c;最新版的 Anaconda 自带 Lab&#xff0c;可跳过下面这一步。pip install jupyterpip install jupyterlab安装完后&#xff0…