import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import java.util.ArrayList;
import java.util.List;
public class POIUtil {
private POIUtil() {}
public static void copySheet(HSSFWorkbook fromWorkbook, HSSFWorkbook toWorkbook, int fromSheetIndex, int toSheetIndex) {
HSSFSheet fromSheet = fromWorkbook.getSheetAt(fromSheetIndex);
for (int i = fromSheet.getFirstRowNum(); i <= fromSheet.getLastRowNum(); i++) {
copyRows(fromWorkbook, toWorkbook, fromSheetIndex, toSheetIndex, i, i, i);
}
}
public static void copyRows(HSSFWorkbook fromWorkbook, HSSFWorkbook toWorkbook, int fromSheetIndex, int toSheetIndex, int startRow, int endRow, int position) {
HSSFSheet fromSheet = fromWorkbook.getSheetAt(fromSheetIndex);
HSSFSheet toSheet = toWorkbook.getSheetAt(toSheetIndex);
int i;
int j;
if ((startRow == -1 ) || (endRow == -1 )) {
return ;
}
List <CellRangeAddress> oldRanges = new ArrayList<CellRangeAddress>();
for (i = 0 ; i < fromSheet.getNumMergedRegions(); i++) {
oldRanges.add(fromSheet.getMergedRegion(i));
}
for (CellRangeAddress oldRange : oldRanges) {
CellRangeAddress newRange = new CellRangeAddress(oldRange.getFirstRow(), oldRange.getLastRow(),
oldRange.getFirstColumn(), oldRange.getLastColumn());
if (oldRange.getFirstRow() >= startRow && oldRange.getLastRow() <= endRow) {
int targetRowFrom = oldRange.getFirstRow() - startRow + position;
int targetRowTo = oldRange.getLastRow() - startRow + position;
oldRange.setFirstRow(targetRowFrom);
oldRange.setLastRow(targetRowTo);
toSheet.addMergedRegion(oldRange);
fromSheet.addMergedRegion(newRange);
}
}
for (i = startRow; i <= endRow; i++) {
HSSFRow fromRow = fromSheet.getRow(i);
if (fromRow != null ) {
for (j = fromRow.getLastCellNum(); j >= fromRow.getFirstCellNum(); j--) {
toSheet.setColumnWidth(j, fromSheet.getColumnWidth(j));
toSheet.setColumnHidden(j, false );
}
break ;
}
}
for (; i <= endRow; i++) {
HSSFRow fromRow = fromSheet.getRow(i);
if (fromRow == null ) {
continue ;
}
HSSFRow toRow = toSheet.createRow(i - startRow + position);
toRow.setHeight(fromRow.getHeight());
for (j = fromRow.getFirstCellNum(); j <= fromRow.getPhysicalNumberOfCells(); j++) {
HSSFCell fromCell = fromRow.getCell(j);
if (fromCell == null ) {
continue ;
}
HSSFCell toCell = toRow.createCell(j);
HSSFCellStyle toStyle = toWorkbook.createCellStyle();
copyCellStyle(fromWorkbook, toWorkbook, fromCell.getCellStyle(), toStyle);
toCell.setCellStyle(toStyle);
int cType = fromCell.getCellType();
toCell.setCellType(cType);
switch (cType) {
case HSSFCell.CELL_TYPE_BOOLEAN:
toCell.setCellValue(fromCell.getBooleanCellValue());
break ;
case HSSFCell.CELL_TYPE_ERROR:
toCell.setCellErrorValue(fromCell.getErrorCellValue());
break ;
case HSSFCell.CELL_TYPE_FORMULA:
toCell.setCellFormula(parseFormula(fromCell.getCellFormula()));
break ;
case HSSFCell.CELL_TYPE_NUMERIC:
toCell.setCellValue(fromCell.getNumericCellValue());
break ;
case HSSFCell.CELL_TYPE_STRING:
toCell.setCellValue(fromCell.getRichStringCellValue());
break ;
}
}
}
}
public static void copyRows(HSSFWorkbook workbook, int fromSheetIndex, int toSheetIndex, int startRow, int endRow, int position) {
HSSFSheet fromSheet = workbook.getSheetAt(fromSheetIndex);
HSSFSheet toSheet = workbook.getSheetAt(toSheetIndex);
int i;
int j;
if ((startRow == -1 ) || (endRow == -1 )) {
return ;
}
List <CellRangeAddress> oldRanges = new ArrayList<CellRangeAddress>();
for (i = 0 ; i < fromSheet.getNumMergedRegions(); i++) {
oldRanges.add(fromSheet.getMergedRegion(i));
}
for (CellRangeAddress oldRange : oldRanges) {
CellRangeAddress newRange = new CellRangeAddress(oldRange.getFirstRow(), oldRange.getLastRow(),
oldRange.getFirstColumn(), oldRange.getLastColumn());
if (oldRange.getFirstRow() >= startRow && oldRange.getLastRow() <= endRow) {
int targetRowFrom = oldRange.getFirstRow() - startRow + position;
int targetRowTo = oldRange.getLastRow() - startRow + position;
oldRange.setFirstRow(targetRowFrom);
oldRange.setLastRow(targetRowTo);
toSheet.addMergedRegion(oldRange);
fromSheet.addMergedRegion(newRange);
}
}
for (i = startRow; i <= endRow; i++) {
HSSFRow fromRow = fromSheet.getRow(i);
if (fromRow != null ) {
for (j = fromRow.getLastCellNum(); j >= fromRow.getFirstCellNum(); j--) {
toSheet.setColumnWidth(j, fromSheet.getColumnWidth(j));
toSheet.setColumnHidden(j, false );
}
break ;
}
}
for (; i <= endRow; i++) {
HSSFRow fromRow = fromSheet.getRow(i);
if (fromRow == null ) {
continue ;
}
HSSFRow toRow = toSheet.createRow(i - startRow + position);
toRow.setHeight(fromRow.getHeight());
for (j = fromRow.getFirstCellNum(); j <= fromRow.getPhysicalNumberOfCells(); j++) {
HSSFCell fromCell = fromRow.getCell(j);
if (fromCell == null ) {
continue ;
}
HSSFCell toCell = toRow.createCell(j);
toCell.setCellStyle(fromCell.getCellStyle());
int cType = fromCell.getCellType();
toCell.setCellType(cType);
switch (cType) {
case HSSFCell.CELL_TYPE_BOOLEAN:
toCell.setCellValue(fromCell.getBooleanCellValue());
break ;
case HSSFCell.CELL_TYPE_ERROR:
toCell.setCellErrorValue(fromCell.getErrorCellValue());
break ;
case HSSFCell.CELL_TYPE_FORMULA:
toCell.setCellFormula(parseFormula(fromCell.getCellFormula()));
break ;
case HSSFCell.CELL_TYPE_NUMERIC:
toCell.setCellValue(fromCell.getNumericCellValue());
break ;
case HSSFCell.CELL_TYPE_STRING:
toCell.setCellValue(fromCell.getRichStringCellValue());
break ;
}
}
}
}
public static void copyCellStyle(HSSFWorkbook fromWorkbook, HSSFWorkbook toWorkbook, HSSFCellStyle fromStyle, HSSFCellStyle toStyle) {
toStyle.setAlignment(fromStyle.getAlignment());
toStyle.setBorderBottom(fromStyle.getBorderBottom());
toStyle.setBorderLeft(fromStyle.getBorderLeft());
toStyle.setBorderRight(fromStyle.getBorderRight());
toStyle.setBorderTop(fromStyle.getBorderTop());
toStyle.setTopBorderColor(fromStyle.getTopBorderColor());
toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor());
toStyle.setRightBorderColor(fromStyle.getRightBorderColor());
toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor());
HSSFFont tofont = toWorkbook.createFont();
copyFont(fromStyle.getFont(fromWorkbook), tofont);
toStyle.setFont(tofont);
toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor());
toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor());
toStyle.setDataFormat(fromStyle.getDataFormat());
toStyle.setFillPattern(fromStyle.getFillPattern());
toStyle.setHidden(fromStyle.getHidden());
toStyle.setIndention(fromStyle.getIndention());
toStyle.setLocked(fromStyle.getLocked());
toStyle.setRotation(fromStyle.getRotation());
toStyle.setVerticalAlignment(fromStyle.getVerticalAlignment());
toStyle.setWrapText(fromStyle.getWrapText());
}
public static void copyFont(HSSFFont fromFont, HSSFFont toFont) {
toFont.setBoldweight(fromFont.getBoldweight());
toFont.setCharSet(fromFont.getCharSet());
toFont.setColor(fromFont.getColor());
toFont.setFontHeight(fromFont.getFontHeight());
toFont.setFontHeightInPoints(fromFont.getFontHeightInPoints());
toFont.setFontName(fromFont.getFontName());
toFont.setItalic(fromFont.getItalic());
toFont.setStrikeout(fromFont.getStrikeout());
toFont.setTypeOffset(fromFont.getTypeOffset());
toFont.setUnderline(fromFont.getUnderline());
}
private static String parseFormula(String pPOIFormula) {
final String cstReplaceString = "ATTR(semiVolatile)" ;
StringBuffer result;
int index;
result = new StringBuffer();
index = pPOIFormula.indexOf(cstReplaceString);
if (index >= 0 ) {
result.append(pPOIFormula.substring( 0 , index));
result.append(pPOIFormula.substring(index + cstReplaceString.length()));
} else {
result.append(pPOIFormula);
}
return result.toString();
}
}