POI进行ExcelSheet的拷贝
POI进行ExcelSheet的拷贝
学习了:http://www.360doc.com/content/17/0508/20/42823223_652205632.shtml,这个也需要改改
这个:http://blog.csdn.net/wutbiao/article/details/8696446#有些问题
目前格式还是无法拷贝,如果拷贝格式会导致wookbook为空;
package com.srie.excel.controller;
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.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;
public class POIUtils {
/**
* 拷贝Excel行
*
* @param fromsheet
* @param newsheet
* @param firstrow
* @param lastrow
*/
@SuppressWarnings("deprecation")
public void copyRows( HSSFSheet fromsheet, HSSFSheet newsheet ) {
int firstrow = fromsheet.getFirstRowNum();
int lastrow = fromsheet.getLastRowNum(); if ((firstrow == -1) || (lastrow == -1) || lastrow < firstrow) {
return;
}
// 拷贝合并的单元格
for (int i = 0; i < fromsheet.getNumMergedRegions(); i++) {
CellRangeAddress mergedRegion = fromsheet.getMergedRegion(i);
newsheet.addMergedRegion(mergedRegion);
}
HSSFRow fromRow = null;
HSSFRow newRow = null;
HSSFCell newCell = null;
HSSFCell fromCell = null;
// 设置列宽
for (int i = firstrow; i <= lastrow; i++) {
fromRow = fromsheet.getRow(i);
if (fromRow != null) {
for (int j = fromRow.getLastCellNum(); j >= fromRow.getFirstCellNum(); j--) {
int colnum = fromsheet.getColumnWidth((short) j);
if (colnum > 100) {
newsheet.setColumnWidth((short) j, (short) colnum);
}
if (colnum == 0) {
newsheet.setColumnHidden((short) j, true);
} else {
newsheet.setColumnHidden((short) j, false);
}
}
break;
}
}
// 拷贝行并填充数据
for (int i = 0; i <= lastrow; i++) {
fromRow = fromsheet.getRow(i);
if (fromRow == null) {
continue;
}
newRow = newsheet.createRow(i - firstrow);
newRow.setHeight(fromRow.getHeight());
for (int j = fromRow.getFirstCellNum(); j < fromRow.getPhysicalNumberOfCells(); j++) {
fromCell = fromRow.getCell((short) j);
if (fromCell == null) {
continue;
}
newCell = newRow.createCell((short) j);
HSSFCellStyle cellStyle = fromCell.getCellStyle(); // HSSFCellStyle newStyle = newsheet.getWorkbook().createCellStyle();
// newStyle.cloneStyleFrom(cellStyle);
// newCell.setCellStyle(newStyle); // HSSFCellStyle cellStyle2 = newCell.getCellStyle();
// cellStyle2.setFillForegroundColor(cellStyle.getFillForegroundColor());
// cellStyle2.setFillPattern(cellStyle.getFillPattern());
// cellStyle2.setAlignment(cellStyle.getAlignment());
// cellStyle2.setVerticalAlignment(cellStyle.getVerticalAlignment()); int cType = fromCell.getCellType();
newCell.setCellType(cType);
switch (cType) {
case HSSFCell.CELL_TYPE_STRING:
newCell.setCellValue(fromCell.getRichStringCellValue());
break;
case HSSFCell.CELL_TYPE_NUMERIC:
newCell.setCellValue(fromCell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_FORMULA:
newCell.setCellFormula(fromCell.getCellFormula());
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
newCell.setCellValue(fromCell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_ERROR:
newCell.setCellValue(fromCell.getErrorCellValue());
break;
default:
newCell.setCellValue(fromCell.getRichStringCellValue());
break;
}
}
}
}
public static void main(String[] args) {}
}
POI进行ExcelSheet的拷贝的更多相关文章
- LeftoverDataException,依赖包,apache license 2.0
1. poi3.9 LeftoverDataException org.apache.poi.hssf.record.RecordInputStream$LeftoverDataException: ...
- java 实现 excel sheet 拷贝到另一个Excel文件中 poi
public class CopyExcelSheetToAnotherExcelSheet { public static void main(String[] args) throws FileN ...
- java:POI导出excel
POI是一个开源项目,专用于java平台上操作MS OFFICE,企业应用开发中可用它方便导出Excel. 下面是使用示例: 1.maven中先添加依赖项 <dependency> < ...
- POI导出EXCEL经典实现
1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则 ...
- Java POI 导出EXCEL经典实现 Java导出Excel
转自http://blog.csdn.net/evangel_z/article/details/7332535 在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者 ...
- excel poi 文件导出,支持多sheet、多列自动合并。
参考博客: http://www.oschina.net/code/snippet_565430_15074 增加了多sheet,多列的自动合并. 修改了部分过时方法和导出逻辑. 优化了标题,导出信息 ...
- jxl读写excel, poi读写excel,word, 读取Excel数据到MySQL
这篇blog是介绍: 1. java中的poi技术读取Excel数据,然后保存到MySQL数据中. 2. jxl读写excel 你也可以在 : java的poi技术读取和导入Excel了解到写入Exc ...
- Java中使用poi导入、导出Excel
一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际 ...
- Java使用POI为Excel打水印,调整列宽并设置Excel只读(用户不可编辑)
本文介绍在Java语言环境下,使用POI为Excel打水印的解决方案,具体的代码编写以及相关的注意事项. 需求描述: 要求通过系统下载的Excel都带上公司的水印,列宽调整为合适的宽度,并且设置为不可 ...
随机推荐
- 树状数组 poj2352 Stars
2019-05-20 22:52:07 加油,坚持,加油,坚持 !!! #include<iostream> #include<cstdio> #include<cstr ...
- RAR 5.50 控制台使用记录
copy from WinRAR用户手册,备忘 用户手册 ~~~~~~~~ RAR 5.50 控制台版本 ~~~~~~~~~~~~~~~~~~~ =-=-=-=-=-=-=-=-=-=-=-=-=- ...
- Redis之Ubuntu下Redis集群搭建
安装redis 首先下载redis $ wget http://download.redis.io/releases/redis-4.0.10.tar.gz $ .tar.gz $ cd redis- ...
- 在线word论文生成的方法
下午实验室师妹问我latex转word生成的格式不行咋办.于是找了一下网上,发现了一个可以自动生成word论文的网站 https://typeset.io/formats/bmc/default-te ...
- layui新手使用
1,首先最重要的是引入官方的layui.js layui.css文件 2,在自己的项目中新建一个目录 再在该目录下建一个js文件,js中写入 layui.define(['layer', 'form ...
- Java语言基础(数组)
Java语言基础(数组概述和定义格式说明) A:为什么要有数组(容器) 为了存储同种数据类型的多个值 B:数组概念 数组是存储同一种数据类型多个元素的集合.也可以看成是一个容器. 数组既可以存储基本数 ...
- Spring 获取propertise文件中的值
Spring 获取propertise文件中的值 Spring 获取propertise的方式,除了之前的博文提到的使用@value的注解注入之外,还可以通过编码的方式获取,这里主要说的是要使用Emb ...
- java学习笔记1——继承
通过在类的声明中加入extends子句创建一个子类并继承父类的成员变量和方法.如: class SubClass extends SuperClass{ ... } 若SuperClass是另一个类的 ...
- children ie8下获取错误
ParentNode.children 是一个只读属性,返回 一个Node的子elements,是一个动态更新的 HTMLCollection. Internet Explorer 6 - 8 支持该 ...
- JavaScript数组操作函数
A: 购物车会有这样的情况,购物车是一个数组,每一个商品是一个对象,分别对应一个id,和一个num ,然后改变商品的时候需要和购物车对比,如果购物车中有这个商品的话,就只改变这个商品对应的id的num ...