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) {}
}
复制代码

 

posted @   stono  阅读(6866)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2015-05-08 jackson - 生成jason工具-简单示例
点击右上角即可分享
微信分享提示
主题色彩