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的拷贝的更多相关文章

  1. LeftoverDataException,依赖包,apache license 2.0

    1. poi3.9 LeftoverDataException org.apache.poi.hssf.record.RecordInputStream$LeftoverDataException: ...

  2. java 实现 excel sheet 拷贝到另一个Excel文件中 poi

    public class CopyExcelSheetToAnotherExcelSheet { public static void main(String[] args) throws FileN ...

  3. java:POI导出excel

    POI是一个开源项目,专用于java平台上操作MS OFFICE,企业应用开发中可用它方便导出Excel. 下面是使用示例: 1.maven中先添加依赖项 <dependency> < ...

  4. POI导出EXCEL经典实现

    1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则 ...

  5. Java POI 导出EXCEL经典实现 Java导出Excel

    转自http://blog.csdn.net/evangel_z/article/details/7332535 在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者 ...

  6. excel poi 文件导出,支持多sheet、多列自动合并。

    参考博客: http://www.oschina.net/code/snippet_565430_15074 增加了多sheet,多列的自动合并. 修改了部分过时方法和导出逻辑. 优化了标题,导出信息 ...

  7. jxl读写excel, poi读写excel,word, 读取Excel数据到MySQL

    这篇blog是介绍: 1. java中的poi技术读取Excel数据,然后保存到MySQL数据中. 2. jxl读写excel 你也可以在 : java的poi技术读取和导入Excel了解到写入Exc ...

  8. Java中使用poi导入、导出Excel

    一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际 ...

  9. Java使用POI为Excel打水印,调整列宽并设置Excel只读(用户不可编辑)

    本文介绍在Java语言环境下,使用POI为Excel打水印的解决方案,具体的代码编写以及相关的注意事项. 需求描述: 要求通过系统下载的Excel都带上公司的水印,列宽调整为合适的宽度,并且设置为不可 ...

随机推荐

  1. hdoj--2151--Worm(dp)

    Worm Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  2. curl ,post,get (原创)

    curl get: 1)直接输出 $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,"http://testopen.api.yaolan.com/a ...

  3. BZOJ 1441 裴蜀定理

    思路: 若a,b是整数,且(a,b)=d,那么对于任意的整数x,y,ax+by都一定是d的倍数,特别地,一定存在整数x,y,使ax+by=d成立. 它的一个重要推论是:a,b互质的充要条件是存在整数x ...

  4. C#操作QQ邮箱发送电子邮件原来这么简单。。。。

    在贴代码之前,首先需要给QQ邮箱开服务IMAP/SMTP服务,详细开通方法见 "开通方法"(可能需要发送收费短信,所以只要开通这一个服务就好了). 这边主要就是为了一个服务的授权码 ...

  5. WinForm——操作word文档

    解决方案资源管理器——引用——(右击)添加引用——COM 1. 安装Office,添加引用COM里面的 Microsoft Word 14.0 Object. Library 2. 导命名空间 usi ...

  6. Linux目录结构(二)

    Linux文件系统结的结构是树形结构,其入口从/开始,了解Linux文件系统的结构,对于我们需要掌握的基础知识点之一. 2.文件系统的组织结构简说: 当您使用Linux的时候,如果您通过ls -la ...

  7. Codeforces Round #448

    Pizza Serparation #include<stdio.h> #include<string.h> #include<stdlib.h> #include ...

  8. parseint和isNaN用法

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  9. CSS 三栏布局入门

    首先,我是CSS盲[只听说过box model],没动手实践过,关于margin padding只知名称,不明细节.刚看过一叶斋大哥关于css布局的博文,再动手实践,动手记录下点滴积累以备后用. &l ...

  10. 【Oracle】删除undo表空间时,表空间被占用:ORA-30042: Cannot offline the undo tablespace

    特别注意:此办法只用于实在没有办法的时候,因为需要加入oracle中的隐含参数,慎用!!! 1. 先查一下是什么在占用undo SYS@ENMOEDU>select segment_name,o ...