From 0793bee0ddab803e38b8360a3dc6d4cf81b645c8 Mon Sep 17 00:00:00 2001 From: Gonzalo Gallotti Date: Thu, 15 Sep 2022 13:48:16 -0300 Subject: [PATCH 1/3] Support ExcelDocuement Cell BackColor --- .../java/com/genexus/gxoffice/ExcelCells.java | 3 ++ .../com/genexus/gxoffice/IExcelCells.java | 51 ++++++++++--------- .../genexus/gxoffice/poi/hssf/ExcelCells.java | 34 ++++++++++++- .../genexus/gxoffice/poi/xssf/ExcelCells.java | 39 +++++++++++--- 4 files changed, 94 insertions(+), 33 deletions(-) diff --git a/gxoffice/src/main/java/com/genexus/gxoffice/ExcelCells.java b/gxoffice/src/main/java/com/genexus/gxoffice/ExcelCells.java index d99716263..d3635a4d9 100644 --- a/gxoffice/src/main/java/com/genexus/gxoffice/ExcelCells.java +++ b/gxoffice/src/main/java/com/genexus/gxoffice/ExcelCells.java @@ -25,6 +25,9 @@ public class ExcelCells implements IExcelCells public native long getColor(); public native void setColor(long nNewVal); + public native long getBackColor(); + public native void setBackColor(long nNewVal); + public native double getSize(); public native void setSize(double nNewVal); diff --git a/gxoffice/src/main/java/com/genexus/gxoffice/IExcelCells.java b/gxoffice/src/main/java/com/genexus/gxoffice/IExcelCells.java index 4257db48e..5636de97b 100644 --- a/gxoffice/src/main/java/com/genexus/gxoffice/IExcelCells.java +++ b/gxoffice/src/main/java/com/genexus/gxoffice/IExcelCells.java @@ -1,34 +1,39 @@ package com.genexus.gxoffice; -import java.util.*; -public interface IExcelCells -{ -public double getNumber(); - public Date getDate(); - public String getText(); - public String getValue(); +import java.util.Date; - public void setNumber(double Value); - public void setDate(Date Value); - public void setText(String Value); +public interface IExcelCells { + public String getValue(); - public String getFont(); - public void setFont(String sNewVal); + public double getNumber(); + public void setNumber(double Value); - public long getColor(); - public void setColor(long nNewVal); + public Date getDate(); + public void setDate(Date Value); - public double getSize(); - public void setSize(double nNewVal); + public String getText(); + public void setText(String Value); - public String getType(); + public String getFont(); + public void setFont(String sNewVal); - public short getBold(); - public void setBold(short nNewVal); + public long getColor(); + public void setColor(long nNewVal); - public short getItalic(); - public void setItalic(short nNewVal); + public long getBackColor(); + public void setBackColor(long nNewVal); - public short getUnderline(); - public void setUnderline(short nNewVal); + public double getSize(); + public void setSize(double nNewVal); + + public String getType(); + + public short getBold(); + public void setBold(short nNewVal); + + public short getItalic(); + public void setItalic(short nNewVal); + + public short getUnderline(); + public void setUnderline(short nNewVal); } diff --git a/gxoffice/src/main/java/com/genexus/gxoffice/poi/hssf/ExcelCells.java b/gxoffice/src/main/java/com/genexus/gxoffice/poi/hssf/ExcelCells.java index efb917494..08c8d4759 100644 --- a/gxoffice/src/main/java/com/genexus/gxoffice/poi/hssf/ExcelCells.java +++ b/gxoffice/src/main/java/com/genexus/gxoffice/poi/hssf/ExcelCells.java @@ -12,8 +12,7 @@ import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; -import org.apache.poi.ss.usermodel.CellType; -import org.apache.poi.ss.usermodel.DateUtil; +import org.apache.poi.ss.usermodel.*; import com.genexus.CommonUtil; import com.genexus.gxoffice.IExcelCells; @@ -752,6 +751,37 @@ public void setColor(long value) // 05/07/05 B@tero } } + @Override + public long getBackColor() { + if (pCells.length == 0) { + return 0; + } + + Cell cell = pCells[1]; + if (cell.getCellStyle() == null) { + return 0; + } + + return cell.getCellStyle().getFillBackgroundColor(); + } + + @Override + public void setBackColor(long nNewVal) { + for (int i = 1; i <= cntCells; i++) { + Cell cell = pCells[i]; + CellStyle cellStyle = cell.getCellStyle(); + if (cellStyle == null) { + cellStyle = cell.getSheet().getWorkbook().createCellStyle(); + } + cellStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex()); + if (cellStyle.getFillPattern() != FillPatternType.NO_FILL) { + cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + } + cell.setCellStyle(cellStyle); + } + } + + private void copyPropertiesStyle(HSSFCellStyle dest, HSSFCellStyle source) { dest.setAlignment(source.getAlignment()); dest.setBorderBottom(source.getBorderBottom()); diff --git a/gxoffice/src/main/java/com/genexus/gxoffice/poi/xssf/ExcelCells.java b/gxoffice/src/main/java/com/genexus/gxoffice/poi/xssf/ExcelCells.java index f1205e06b..e9c613cb0 100644 --- a/gxoffice/src/main/java/com/genexus/gxoffice/poi/xssf/ExcelCells.java +++ b/gxoffice/src/main/java/com/genexus/gxoffice/poi/xssf/ExcelCells.java @@ -5,14 +5,7 @@ import java.util.Date; import java.util.GregorianCalendar; -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.CellStyle; -import org.apache.poi.ss.usermodel.CellType; -import org.apache.poi.ss.usermodel.DataFormat; -import org.apache.poi.ss.usermodel.DataFormatter; -import org.apache.poi.ss.usermodel.Font; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.NumberToTextConverter; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFColor; @@ -731,6 +724,36 @@ public void setColor(long value) // 05/07/05 B@tero } } + @Override + public long getBackColor() { + if (pCells.length == 0) { + return 0; + } + + Cell cell = pCells[1]; + if (cell.getCellStyle() == null) { + return 0; + } + + return cell.getCellStyle().getFillBackgroundColor(); + } + + @Override + public void setBackColor(long nNewVal) { + for (int i = 1; i <= cntCells; i++) { + Cell cell = pCells[i]; + CellStyle cellStyle = cell.getCellStyle(); + if (cellStyle == null) { + cellStyle = cell.getSheet().getWorkbook().createCellStyle(); + } + cellStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex()); + if (cellStyle.getFillPattern() != FillPatternType.NO_FILL) { + cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + } + cell.setCellStyle(cellStyle); + } + } + protected void copyPropertiesStyle(CellStyle dest, CellStyle source) { dest.cloneStyleFrom(source); } From c9b1ac5c5137da5e37c292e3822add8a3b34caf0 Mon Sep 17 00:00:00 2001 From: Gonzalo Gallotti Date: Thu, 15 Sep 2022 15:31:04 -0300 Subject: [PATCH 2/3] Cell Background Color --- .../genexus/gxoffice/poi/hssf/ExcelCells.java | 16 +++++++++--- .../genexus/gxoffice/poi/xssf/ExcelCells.java | 26 ++++++++++++++----- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/gxoffice/src/main/java/com/genexus/gxoffice/poi/hssf/ExcelCells.java b/gxoffice/src/main/java/com/genexus/gxoffice/poi/hssf/ExcelCells.java index 08c8d4759..d06f35f4a 100644 --- a/gxoffice/src/main/java/com/genexus/gxoffice/poi/hssf/ExcelCells.java +++ b/gxoffice/src/main/java/com/genexus/gxoffice/poi/hssf/ExcelCells.java @@ -18,6 +18,8 @@ import com.genexus.gxoffice.IExcelCells; import com.genexus.gxoffice.IGxError; import org.apache.poi.ss.util.NumberToTextConverter; +import org.apache.poi.xssf.usermodel.XSSFCellStyle; +import org.apache.poi.xssf.usermodel.XSSFColor; /** * @author Diego @@ -766,17 +768,25 @@ public long getBackColor() { } @Override - public void setBackColor(long nNewVal) { + public void setBackColor(long value) { + int val = (int) value; + int red = val >> 16 & 0xff; + int green = val >> 8 & 0xff; + int blue = val & 0xff; + XSSFColor newColor = new XSSFColor(new java.awt.Color(red, green, blue), null); + for (int i = 1; i <= cntCells; i++) { Cell cell = pCells[i]; CellStyle cellStyle = cell.getCellStyle(); if (cellStyle == null) { cellStyle = cell.getSheet().getWorkbook().createCellStyle(); } - cellStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex()); - if (cellStyle.getFillPattern() != FillPatternType.NO_FILL) { + if (cellStyle.getFillPattern() == FillPatternType.NO_FILL) { cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); } + + ((HSSFCellStyle)cellStyle).setFillForegroundColor(newColor.getIndexed()); + cellStyle.setFillForegroundColor((short) value); cell.setCellStyle(cellStyle); } } diff --git a/gxoffice/src/main/java/com/genexus/gxoffice/poi/xssf/ExcelCells.java b/gxoffice/src/main/java/com/genexus/gxoffice/poi/xssf/ExcelCells.java index e9c613cb0..bc94712c5 100644 --- a/gxoffice/src/main/java/com/genexus/gxoffice/poi/xssf/ExcelCells.java +++ b/gxoffice/src/main/java/com/genexus/gxoffice/poi/xssf/ExcelCells.java @@ -5,11 +5,10 @@ import java.util.Date; import java.util.GregorianCalendar; +import org.apache.poi.ss.formula.functions.Index; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.NumberToTextConverter; -import org.apache.poi.xssf.usermodel.XSSFCell; -import org.apache.poi.xssf.usermodel.XSSFColor; -import org.apache.poi.xssf.usermodel.XSSFFont; +import org.apache.poi.xssf.usermodel.*; import com.genexus.CommonUtil; import com.genexus.gxoffice.IExcelCells; @@ -630,7 +629,11 @@ public void setColor(long value) // 05/07/05 B@tero try { for (int i = 1; i <= cntCells; i++) { - CellStyle cellStyle = pCells[i].getCellStyle(); + Cell cell = pCells[i]; + CellStyle cellStyle = cell.getCellStyle(); + if (cellStyle == null) { + cellStyle = cell.getSheet().getWorkbook().createCellStyle(); + } Font fontCell = pWorkbook.getFontAt(cellStyle.getFontIndexAsInt()); CellStyle newStyle = null; XSSFFont newFont = null; @@ -739,17 +742,26 @@ public long getBackColor() { } @Override - public void setBackColor(long nNewVal) { + public void setBackColor(long value) { + int val = (int) value; + int red = val >> 16 & 0xff; + int green = val >> 8 & 0xff; + int blue = val & 0xff; + + XSSFColor newColor = new XSSFColor(new java.awt.Color(red, green, blue),null); + for (int i = 1; i <= cntCells; i++) { Cell cell = pCells[i]; CellStyle cellStyle = cell.getCellStyle(); if (cellStyle == null) { cellStyle = cell.getSheet().getWorkbook().createCellStyle(); } - cellStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex()); - if (cellStyle.getFillPattern() != FillPatternType.NO_FILL) { + + if (cellStyle.getFillPattern() == FillPatternType.NO_FILL) { cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); } + + ((XSSFCellStyle)cellStyle).setFillForegroundColor(newColor); cell.setCellStyle(cellStyle); } } From 7377925e8061fa5ecba16470a75c6fd54fdbf396 Mon Sep 17 00:00:00 2001 From: Gonzalo Gallotti Date: Thu, 15 Sep 2022 15:32:50 -0300 Subject: [PATCH 3/3] Cell Background Color --- .../src/main/java/com/genexus/gxoffice/poi/hssf/ExcelCells.java | 2 +- .../src/main/java/com/genexus/gxoffice/poi/xssf/ExcelCells.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gxoffice/src/main/java/com/genexus/gxoffice/poi/hssf/ExcelCells.java b/gxoffice/src/main/java/com/genexus/gxoffice/poi/hssf/ExcelCells.java index d06f35f4a..6b5485342 100644 --- a/gxoffice/src/main/java/com/genexus/gxoffice/poi/hssf/ExcelCells.java +++ b/gxoffice/src/main/java/com/genexus/gxoffice/poi/hssf/ExcelCells.java @@ -764,7 +764,7 @@ public long getBackColor() { return 0; } - return cell.getCellStyle().getFillBackgroundColor(); + return cell.getCellStyle().getFillForegroundColor(); } @Override diff --git a/gxoffice/src/main/java/com/genexus/gxoffice/poi/xssf/ExcelCells.java b/gxoffice/src/main/java/com/genexus/gxoffice/poi/xssf/ExcelCells.java index bc94712c5..ea811a4d8 100644 --- a/gxoffice/src/main/java/com/genexus/gxoffice/poi/xssf/ExcelCells.java +++ b/gxoffice/src/main/java/com/genexus/gxoffice/poi/xssf/ExcelCells.java @@ -738,7 +738,7 @@ public long getBackColor() { return 0; } - return cell.getCellStyle().getFillBackgroundColor(); + return cell.getCellStyle().getFillForegroundColor(); } @Override