diff --git a/src/main/java/plugins/adufour/blocks/tools/io/WorkbookToFile.java b/src/main/java/plugins/adufour/blocks/tools/io/WorkbookToFile.java index 34aae3efbf8e4da7092341c1b49b0a9c83c0c78c..5692fdaa96f501af29a280b4d2a5e74fa5769a2b 100644 --- a/src/main/java/plugins/adufour/blocks/tools/io/WorkbookToFile.java +++ b/src/main/java/plugins/adufour/blocks/tools/io/WorkbookToFile.java @@ -138,9 +138,7 @@ public class WorkbookToFile extends Plugin implements IOBlock { try { - Workbook wb = FileToWorkbook.readWorkbook(file); - mergeWorkbooks(workbook, wb, mergePolicy); - workbook = wb; + mergeWorkbooks(FileToWorkbook.readWorkbook(file), workbook, mergePolicy); } catch (IllegalArgumentException e) { @@ -229,9 +227,7 @@ public class WorkbookToFile extends Plugin implements IOBlock if (file.exists() && mergePolicy != MergePolicy.Overwrite) try { - Workbook wb = FileToWorkbook.readWorkbook(file); - mergeWorkbooks(workbook, wb, mergePolicy); - workbook = wb; + mergeWorkbooks(FileToWorkbook.readWorkbook(file), workbook, mergePolicy); } catch (Exception e) { @@ -389,7 +385,7 @@ public class WorkbookToFile extends Plugin implements IOBlock if (!styles.containsKey(hashCode)) { CellStyle targetStyle = target.createCellStyle(); - targetStyle.cloneStyleFrom(sourceCell.getCellStyle()); + Workbooks.copyStyle(sourceCell.getCellStyle(), targetStyle); styles.put(hashCode, targetStyle); } targetCell.setCellStyle(styles.get(hashCode)); diff --git a/src/main/java/plugins/adufour/workbooks/Workbooks.java b/src/main/java/plugins/adufour/workbooks/Workbooks.java index 22f868c563ed3beabf9f350e525d291c7a199181..1ce22ea74c60efa84f8600215fc042d6df984571 100644 --- a/src/main/java/plugins/adufour/workbooks/Workbooks.java +++ b/src/main/java/plugins/adufour/workbooks/Workbooks.java @@ -10,6 +10,8 @@ import javax.swing.BoxLayout; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; @@ -85,7 +87,7 @@ public class Workbooks extends PluginActionable implements PluginThreaded } /** - * @return A new empty workbook using the old compatibility format (XLS). + * @return A new empty workbook using the new XLSX format. */ public static Workbook createEmptyWorkbook() { @@ -236,6 +238,54 @@ public class Workbooks extends PluginActionable implements PluginThreaded } } + /** + * Copies the {@link CellStyle} of <code>srcStyle</code> into <code>dstStyle</code>. + * + * @param srcStyle + * source style + * @param dstStyle + * destination style + */ + public static void copyStyle(CellStyle srcStyle, CellStyle dstStyle) + { + dstStyle.setAlignment(srcStyle.getAlignmentEnum()); + dstStyle.setBorderBottom(srcStyle.getBorderBottomEnum()); + dstStyle.setBorderLeft(srcStyle.getBorderLeftEnum()); + dstStyle.setBorderRight(srcStyle.getBorderRightEnum()); + dstStyle.setBorderTop(srcStyle.getBorderTopEnum()); + dstStyle.setFillBackgroundColor(srcStyle.getFillBackgroundColor()); + dstStyle.setFillForegroundColor(srcStyle.getFillForegroundColor()); + dstStyle.setFillPattern(srcStyle.getFillPatternEnum()); + // dstStyle.setDataFormat(this.transform(srcStyle.getDataFormat())); + // dstStyle.setFont(this.transform(srcStyle.getFont(this.workbookOld))); + dstStyle.setHidden(srcStyle.getHidden()); + dstStyle.setIndention(srcStyle.getIndention()); + dstStyle.setLocked(srcStyle.getLocked()); + dstStyle.setVerticalAlignment(srcStyle.getVerticalAlignmentEnum()); + dstStyle.setWrapText(srcStyle.getWrapText()); + } + + /** + * Copies the {@link Font} of <code>srcFont</code> into <code>dstFont</code>. + * + * @param srcFont + * source font + * @param dstFont + * destination font + */ + public static void copyFont(Font srcFont, Font dstFont) + { + dstFont.setBold(srcFont.getBold()); + dstFont.setCharSet(srcFont.getCharSet()); + dstFont.setColor(srcFont.getColor()); + dstFont.setFontName(srcFont.getFontName()); + dstFont.setFontHeight(srcFont.getFontHeight()); + dstFont.setItalic(srcFont.getItalic()); + dstFont.setStrikeout(srcFont.getStrikeout()); + dstFont.setTypeOffset(srcFont.getTypeOffset()); + dstFont.setUnderline(srcFont.getUnderline()); + } + /** * Checks whether the specified workbook contains a sheet with the specified name.<br> * NB: If the provided sheet name contains invalid characters, they are automatically replaced