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都带上公司的水印,列宽调整为合适的宽度,并且设置为不可 ...
随机推荐
- java中与运算,或运算,异或运算,取反运算
//与运算 & 规则 :都为1时才为1 System.out.println( 7 & 9); /* * 7二进制 0111 ...
- 我的spark python 决策树实例
from numpy import array from pyspark.mllib.regression import LabeledPoint from pyspark.mllib.tree im ...
- div向右偏移设置 css让div靠右移一定距离
转自:https://www.thinkcss.com/shili/1372.shtml div对象盒子向右偏移设置,使用css让div靠右一定距离-div向右移教程实例篇 div向右偏移一定距离,可 ...
- linux git保存用户名密码(避免每次push输用户名密码)
Linux/Unix/Mac 系统 新建一个 ~/.netrc 文件, 将 git 服务器, 用户名以及密码记录在这个文件, 如下所示: machine your-git-server log ...
- 学习java的方式
- Elasticsearch之cur查询索引
前提, Elasticsearch之curl创建索引库 Elasticsearch之curl创建索引 Elasticsearch之curl创建索引库和索引时注意事项 Elasticsearch之cur ...
- Linux运维最佳实践之网站调优
高性能静态网站: 1.静态页面中针对图片进行浏览器(客户端)缓存,如公共JavaScript(jQuery,jQuery-1.12.1.min.js)进行缓存 2.对网站输入内容压缩(gzip) 3. ...
- wppay免登录付费查看隐藏内容/付费资源下载
WPPAY是一款模板兔开发的免登录的付费查看内容/付费下载资源WordPress插件,WPPAY不需要用户注册登录即可支付查看隐藏内容,把整个流程做到极简.发布文章时要隐藏的内容可以利用短代码: [w ...
- 关于KO信息
最近写大论文查到KO也是可以用于分类的一种信息. 如何使用KEGG进行通路富集http://blog.sciencenet.cn/blog-364884-779116.html kegg 数据库学习笔 ...
- Android学习——碎片Fragment的使用
一.碎片的简单用法(实现在一个活动中添加两个碎片,并让这两个碎片平分活动空间) 1.新建一个FragmentTest项目: 新建一个左侧碎片布局left_fragment.xml,代码如下:(只放置一 ...