如何在java xssf poi中移动列 [英] how to shift column in java xssf poi

查看:1606
本文介绍了如何在java xssf poi中移动列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将现有列数据和格式复制到Apache POI中的下一列,并将下一列移至右侧。

How to copy an existing column data and formatting into next column in Apache POI and shift the next column to right.

我试过这个。
假设我的代码是这个......

I tried this. Let say my code is this...

XSSFCell oldCell = worksheet.getRow(0).getCell(1);
XSSFCell newCell =  worksheet.getRow(0).getCell(2);

if(styleMap != null) {   
        if(oldCell.getSheet().getWorkbook() == newCell.getSheet().getWorkbook()){   
            newCell.setCellStyle(oldCell.getCellStyle());   
        } else{   
            int stHashCode = oldCell.getCellStyle().hashCode();   
            XSSFCellStyle newCellStyle = styleMap.get(stHashCode);   
            if(newCellStyle == null){   
                newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();   
                newCellStyle.cloneStyleFrom(oldCell.getCellStyle());   
                styleMap.put(stHashCode, newCellStyle);   
            }   
            newCell.setCellStyle(newCellStyle);   
        }   

    }

我能够复制旧的价值单元格到新单元格,但它不会将现有列向右移动。

I able to copy value from old cell to new cell but it doesn't shift the existing column to right.

提前感谢您的帮助。

推荐答案

几年后我没有使用过POI,但是如果我没记错的话,你必须遍历一行中的所有单元并更新每个<$ c $中的列号。 c> Cell 是你想要的。没有神奇的插入列方法。请记住从右到左执行此操作以避免完全破坏工作表: - )

I haven't worked with POI in several years, but if I remember correctly you must iterate over all the cells in a row and update the column number in each Cell to be what you want. There's no magic "insert column" method. Remember to do this from right to left to avoid completely trashing the worksheet :-)

这篇关于如何在java xssf poi中移动列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆