diff --git a/gxoffice/src/main/java/com/genexus/msoffice/excel/ExcelCellsGXWrapper.java b/gxoffice/src/main/java/com/genexus/msoffice/excel/ExcelCellsGXWrapper.java new file mode 100644 index 000000000..d470be23f --- /dev/null +++ b/gxoffice/src/main/java/com/genexus/msoffice/excel/ExcelCellsGXWrapper.java @@ -0,0 +1,96 @@ +package com.genexus.msoffice.excel; + +import com.genexus.msoffice.excel.style.ExcelStyle; + +import java.util.Date; + +public class ExcelCellsGXWrapper implements IExcelCellRange { + private IExcelCellRange _cellRange; + + public ExcelCellsGXWrapper() { + } + + public ExcelCellsGXWrapper(IExcelCellRange cellRange) { + _cellRange = cellRange; + } + + @Override + public int getRowStart() { + return _cellRange.getRowStart(); + } + + @Override + public int getColumnStart() { + return _cellRange.getColumnStart(); + } + + @Override + public int getRowEnd() { + return _cellRange.getRowEnd(); + } + + @Override + public int getColumnEnd() { + return _cellRange.getColumnEnd(); + } + + @Override + public String getValueType() { + return _cellRange.getValueType(); + } + + @Override + public String getText() { + return _cellRange.getText(); + } + + @Override + public Boolean setText(String value) { + return _cellRange.setText(value); + } + + @Override + public java.math.BigDecimal getNumericValue() { + return _cellRange.getNumericValue(); + } + + @Override + public Boolean setNumericValue(java.math.BigDecimal d) { + return _cellRange.setNumericValue(d); + } + + @Override + public Date getDateValue() { + return _cellRange.getDateValue(); + } + + @Override + public Boolean setDateValue(Date value) { + return _cellRange.setDateValue(value); + } + + @Override + public String getHyperlinkValue() { + return _cellRange.getHyperlinkValue(); + } + + @Override + public Boolean setHyperlinkValue(String value) { + return _cellRange.setHyperlinkValue(value); + } + + @Override + public Boolean empty() { + return _cellRange.empty(); + } + + @Override + public Boolean mergeCells() { + return _cellRange.mergeCells(); + } + + @Override + public Boolean setCellStyle(ExcelStyle newCellStyle) { + return _cellRange.setCellStyle(newCellStyle); + } +} diff --git a/gxoffice/src/main/java/com/genexus/msoffice/excel/ExcelFactory.java b/gxoffice/src/main/java/com/genexus/msoffice/excel/ExcelFactory.java index 95a2555a6..f4670f06e 100644 --- a/gxoffice/src/main/java/com/genexus/msoffice/excel/ExcelFactory.java +++ b/gxoffice/src/main/java/com/genexus/msoffice/excel/ExcelFactory.java @@ -1,10 +1,8 @@ package com.genexus.msoffice.excel; import com.genexus.Application; -import com.genexus.msoffice.excel.poi.xssf.ExcelSpreadsheet; import com.genexus.msoffice.excel.exception.ExcelDocumentNotSupported; import com.genexus.msoffice.excel.exception.ExcelTemplateNotFoundException; -import com.genexus.util.GXServices; import java.io.File; import java.io.IOException; @@ -19,11 +17,15 @@ public static IExcelSpreadsheet create(IGXError handler, String filePath, String filePath = resolvePath(filePath); template = resolvePath(template); - if (filePath.endsWith(".xlsx") || !filePath.contains(".")) - { - return new ExcelSpreadsheet(handler, filePath, template); + if (filePath.toLowerCase().endsWith(".xls")) { + return new com.genexus.msoffice.excel.poi.hssf.ExcelSpreadsheet(handler, filePath, template); + } + else { + if (filePath.toLowerCase().endsWith(".xlsx") || !filePath.contains(".")) { + return new com.genexus.msoffice.excel.poi.xssf.ExcelSpreadsheet(handler, filePath, template); + } + throw new ExcelDocumentNotSupported(); } - throw new ExcelDocumentNotSupported(); } private static String resolvePath(String filePath) { diff --git a/gxoffice/src/main/java/com/genexus/msoffice/excel/ExcelSpreadsheetGXWrapper.java b/gxoffice/src/main/java/com/genexus/msoffice/excel/ExcelSpreadsheetGXWrapper.java index a79272560..403538cfd 100644 --- a/gxoffice/src/main/java/com/genexus/msoffice/excel/ExcelSpreadsheetGXWrapper.java +++ b/gxoffice/src/main/java/com/genexus/msoffice/excel/ExcelSpreadsheetGXWrapper.java @@ -17,15 +17,26 @@ public class ExcelSpreadsheetGXWrapper implements IGXError { private int _errCode; private String _errDescription = ""; private IExcelWorksheet _currentWorksheet; - private List _worksheets; private IExcelSpreadsheet _document; private Boolean _isReadonly = false; private Boolean _autofit = false; private final String DEFAULT_SHEET_NAME = "Sheet"; - public Boolean getAutofit() { - return _autofit; - } + private Boolean initialize() { + return initialize(DEFAULT_SHEET_NAME); + } + + private Boolean initialize(String defaultSheetName) { + boolean ok = selectFirstDefaultSheet(defaultSheetName); + if (!ok) { + setErrCod((short) 1); + setErrDes("Could not get/set first Sheet on Document"); + } else { + setErrCod((short) 0); + setErrDes(""); + } + return ok; + } public void setAutofit(Boolean _autofit) { this._autofit = _autofit; @@ -34,23 +45,6 @@ public void setAutofit(Boolean _autofit) { } } - private Boolean initialize() { - - return initialize(DEFAULT_SHEET_NAME); - } - - private Boolean initialize(String defaultSheetName) { - boolean ok = selectFirstDefaultSheet(defaultSheetName); - if (!ok) { - setErrCod((short) 1); - setErrDes("Could not get/set first Sheet on Document"); - } else { - setErrCod((short) 0); - setErrDes(""); - } - return ok; - } - public boolean open(String filePath) { return open(filePath, ""); } @@ -111,10 +105,21 @@ private Boolean saveAsImpl(String newFileName, String password) { return ok; } - public ExcelCells getCell(int rowIdx, int colIdx) { + public ExcelCells getCell(int rowIdx, int colIdx) { + if (initialize()) { + try { + return (ExcelCells) _document.getCell(_currentWorksheet, rowIdx, colIdx); + } catch (ExcelException e) { + this.setError(e); + } + } + return null; + } + + public ExcelCellsGXWrapper getCellv2(int rowIdx, int colIdx) { if (initialize()) { try { - return (ExcelCells) _document.getCell(_currentWorksheet, rowIdx, colIdx); + return new ExcelCellsGXWrapper(_document.getCell(_currentWorksheet, rowIdx, colIdx)); } catch (ExcelException e) { this.setError(e); } @@ -132,10 +137,21 @@ public void setError(String errorMsg, ExcelException e) { logger.error(errorMsg); } - public ExcelCells getCells(int rowIdx, int colIdx, int rowCount, int colCount) { + public ExcelCells getCells(int rowIdx, int colIdx, int rowCount, int colCount) { + if (initialize()) { + try { + return (ExcelCells) _document.getCells(_currentWorksheet, rowIdx, colIdx, rowCount, colCount); + } catch (ExcelException e) { + this.setError(e); + } + } + return null; + } + + public ExcelCellsGXWrapper getCellsv2(int rowIdx, int colIdx, int rowCount, int colCount) { if (initialize()) { try { - return (ExcelCells) _document.getCells(_currentWorksheet, rowIdx, colIdx, rowCount, colCount); + return new ExcelCellsGXWrapper(_document.getCells(_currentWorksheet, rowIdx, colIdx, rowCount, colCount)); } catch (ExcelException e) { this.setError(e); } @@ -157,7 +173,7 @@ public Boolean setCurrentWorksheet(int sheetIdx) { public Boolean setCurrentWorksheetByName(String sheetName) { if (initialize()) { - ExcelWorksheet ws = _document.getWorkSheet(sheetName); + IExcelWorksheet ws = _document.getWorkSheet(sheetName); if (ws != null) { _currentWorksheet = ws; _document.setActiveWorkSheet(sheetName); @@ -174,15 +190,6 @@ public Boolean insertRow(int rowIdx, int rowCount) { return false; } - public Boolean insertColumn(int colIdx, int colCount) { - //throw new Exception("NotImplemented"); - return false; - /* - * if (isOK()) { //return _document.(_currentWorksheet, colIdx, colCount); } - * return false; - */ - } - public Boolean deleteRow(int rowIdx) { if (initialize()) { return _document.deleteRow(_currentWorksheet, rowIdx - 1); @@ -234,7 +241,7 @@ public Boolean toggleRow(int rowIdx, Boolean visible) { public Boolean deleteSheet(String sheetName) { if (initialize()) { - ExcelWorksheet ws = _document.getWorkSheet(sheetName); + IExcelWorksheet ws = _document.getWorkSheet(sheetName); if (ws != null) return _document.deleteSheet(sheetName); } @@ -279,18 +286,25 @@ public String getErrDescription() { return _errDescription; } - public ExcelWorksheet getCurrentWorksheet() { + public ExcelWorksheet getCurrentWorksheet() { + if (initialize()) { + return (ExcelWorksheet) _currentWorksheet; + } + return null; + } + + public ExcelWorksheetGXWrapper getCurrentWorksheetv2() { if (initialize()) { - return (ExcelWorksheet) _currentWorksheet; + return new ExcelWorksheetGXWrapper(_currentWorksheet); } return null; } - public List getWorksheets() { + public List getWorksheets() { if (initialize()) return _document.getWorksheets(); else - return new ArrayList(); + return new ArrayList(); } private boolean selectFirstDefaultSheet(String sheetName) { diff --git a/gxoffice/src/main/java/com/genexus/msoffice/excel/ExcelWorksheetGXWrapper.java b/gxoffice/src/main/java/com/genexus/msoffice/excel/ExcelWorksheetGXWrapper.java new file mode 100644 index 000000000..eeddfbd36 --- /dev/null +++ b/gxoffice/src/main/java/com/genexus/msoffice/excel/ExcelWorksheetGXWrapper.java @@ -0,0 +1,43 @@ +package com.genexus.msoffice.excel; + +import com.genexus.msoffice.excel.exception.ExcelException; + +public class ExcelWorksheetGXWrapper implements IExcelWorksheet { + private IExcelWorksheet _workSheet; + + public ExcelWorksheetGXWrapper() { + } + + public ExcelWorksheetGXWrapper(IExcelWorksheet sheet) { + _workSheet = sheet; + } + + public String getName() { + return _workSheet.getName(); + } + + public Boolean setHidden(boolean hidden) { + return _workSheet.setHidden(hidden); + } + + public Boolean isHidden() { + return _workSheet.isHidden(); + } + + public Boolean rename(String newName) { + return _workSheet.rename(newName); + } + + public Boolean copy(String newName) { + try { + return _workSheet.copy(newName); + } + catch (ExcelException e) { + return false; + } + } + + public void setProtected(String password) { + _workSheet.setProtected(password); + } +} diff --git a/gxoffice/src/main/java/com/genexus/msoffice/excel/IExcelCellRange.java b/gxoffice/src/main/java/com/genexus/msoffice/excel/IExcelCellRange.java index 1d5a3746f..e2c1967d0 100644 --- a/gxoffice/src/main/java/com/genexus/msoffice/excel/IExcelCellRange.java +++ b/gxoffice/src/main/java/com/genexus/msoffice/excel/IExcelCellRange.java @@ -18,8 +18,6 @@ public interface IExcelCellRange int getColumnEnd(); - String getCellAdress(); - String getValueType(); /* @@ -44,8 +42,4 @@ public interface IExcelCellRange Boolean mergeCells(); Boolean setCellStyle(ExcelStyle style); - - ExcelStyle getCellStyle(); - - } diff --git a/gxoffice/src/main/java/com/genexus/msoffice/excel/IExcelSpreadsheet.java b/gxoffice/src/main/java/com/genexus/msoffice/excel/IExcelSpreadsheet.java index 14bf70e56..54d9bf053 100644 --- a/gxoffice/src/main/java/com/genexus/msoffice/excel/IExcelSpreadsheet.java +++ b/gxoffice/src/main/java/com/genexus/msoffice/excel/IExcelSpreadsheet.java @@ -3,7 +3,6 @@ import java.util.List; import com.genexus.msoffice.excel.exception.ExcelException; -import com.genexus.msoffice.excel.poi.xssf.ExcelWorksheet; public interface IExcelSpreadsheet { @@ -29,8 +28,8 @@ public interface IExcelSpreadsheet Boolean deleteColumn(IExcelWorksheet worksheet, int colIdx); // Worksheets - List getWorksheets(); - ExcelWorksheet getWorkSheet(String name); + List getWorksheets(); + IExcelWorksheet getWorkSheet(String name); Boolean insertWorksheet(String newSheetName, int idx) throws ExcelException; Boolean getAutofit(); diff --git a/gxoffice/src/main/java/com/genexus/msoffice/excel/IExcelWorksheet.java b/gxoffice/src/main/java/com/genexus/msoffice/excel/IExcelWorksheet.java index 6a9eb0257..f1e05893a 100644 --- a/gxoffice/src/main/java/com/genexus/msoffice/excel/IExcelWorksheet.java +++ b/gxoffice/src/main/java/com/genexus/msoffice/excel/IExcelWorksheet.java @@ -8,6 +8,8 @@ public interface IExcelWorksheet public Boolean isHidden(); + public Boolean setHidden(boolean hidden); + public Boolean rename(String newName); public Boolean copy(String newName) throws ExcelException; diff --git a/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/hssf/ExcelCells.java b/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/hssf/ExcelCells.java new file mode 100644 index 000000000..87087be46 --- /dev/null +++ b/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/hssf/ExcelCells.java @@ -0,0 +1,996 @@ +package com.genexus.msoffice.excel.poi.hssf; + +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; + +import com.genexus.CommonUtil; +import com.genexus.msoffice.excel.exception.ExcelException; +import com.genexus.msoffice.excel.exception.ExcelReadonlyException; +import com.genexus.msoffice.excel.style.*; +import com.genexus.msoffice.excel.IGXError; +import com.genexus.msoffice.excel.IExcelCellRange; + +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.hssf.usermodel.*; +import org.apache.poi.hssf.util.HSSFColor; + +import java.math.BigDecimal; + + +public class ExcelCells implements IExcelCellRange { + private IGXError _errorHandler; + protected boolean fitColumnWidth; + protected int cellCount; + + protected int colStartIdx; + protected int rowStartIdx; + protected int colEndIdx; + protected int rowEndIdx; + + protected org.apache.poi.ss.usermodel.Sheet pSelectedSheet; + protected HSSFCell[] pCells; + protected HSSFWorkbook pWorkbook; + + protected ExcelSpreadsheet doc; + + protected int pWidth, pHeight; + protected boolean readonly; + + protected StylesCache stylesCache; + + protected ExcelStyle cellStyle; + + public ExcelCells(IGXError errAccess, ExcelSpreadsheet document, HSSFWorkbook workBook, HSSFSheet selectedSheet, + int rowPos, int colPos, int height, int width, StylesCache stylesCache) throws ExcelException { + this(errAccess, document, workBook, selectedSheet, rowPos, colPos, height, width, false, stylesCache); + } + + public ExcelCells() { + } + + public ExcelCells(IGXError errAccess, ExcelSpreadsheet document, HSSFWorkbook workBook, HSSFSheet selectedSheet, + int rowPos, int colPos, int height, int width, boolean readonly, StylesCache stylesCache) + throws ExcelException { + _errorHandler = errAccess; + doc = document; + + cellCount = 0; + pWidth = width; + pHeight = height; + + colStartIdx = colPos; + colEndIdx = colPos + (width - 1); + rowStartIdx = rowPos; + rowEndIdx = rowPos + (height - 1); + + pWorkbook = workBook; + pSelectedSheet = selectedSheet; + fitColumnWidth = true; + this.readonly = readonly; + this.stylesCache = stylesCache; + pCells = new HSSFCell[(width * height) + 1]; + try { + for (int y = rowPos; y < (rowPos + pHeight); y++) { + HSSFRow pRow = getExcelRow(selectedSheet, y); + if (pRow != null) { + for (short x = (short) colPos; x < (colPos + pWidth); x++) { + HSSFCell pCell = getExcelCell(pRow, x); + if (pCell != null) { + cellCount++; + pCells[cellCount] = pCell; + } + } + } + } + } catch (Exception e) { + throw new ExcelException(8, "Invalid cell coordinates"); + } + } + + protected HSSFRow getExcelRow(HSSFSheet sheet, int rowPos) { + HSSFRow row = sheet.getRow(rowPos); + + /* + * if ((row == null) && readonly) { return null; } + */ + + if (row == null) { + row = sheet.createRow(rowPos); + } + return row; + } + + protected HSSFCell getExcelCell(HSSFRow row, short colPos) { + HSSFCell cell = row.getCell(colPos); + + /* + * if ((cell == null) && readonly) { return null; } + */ + + if (cell == null) { + cell = row.createCell(colPos); + } + return cell; + } + + public boolean setNumber(double value) throws ExcelException { + + try { + for (int i = 1; i <= cellCount; i++) { + + pCells[i].setCellValue(value); + } + return true; + + } catch (Exception e) { + throw new ExcelException(7, "Invalid cell value"); + } + } + + public BigDecimal getNumber() throws ExcelException { + try { + return this.getValue(); + + } catch (Exception e) { + throw new ExcelException(7, "Invalid cell value"); + } + + } + + public boolean setDate(Date value) throws ExcelException { + CheckReadonlyDocument(); + try { + if (!CommonUtil.nullDate().equals(value)) { + String dformat = "m/d/yy h:mm";//this.doc.getDateFormat().toLowerCase(); + Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance + calendar.setTime(value); + if (calendar.get(Calendar.MINUTE) == 0 && calendar.get(Calendar.HOUR) == 0 + && calendar.get(Calendar.SECOND) == 0 && dformat.indexOf(' ') > 0) { + dformat = dformat.substring(0, dformat.indexOf(' ')); + } + DataFormat df = pWorkbook.createDataFormat(); + + for (int i = 1; i <= cellCount; i++) { + HSSFCellStyle cellStyle = pCells[i].getCellStyle(); + if (! DateUtil.isCellDateFormatted(pCells[i])) { + HSSFCellStyle newStyle = pWorkbook.createCellStyle(); + copyPropertiesStyle(newStyle, cellStyle); + newStyle.setDataFormat(df.getFormat(dformat)); + pCells[i].setCellStyle(newStyle); + fitColumnWidth(i, dformat.length() + 4); + }else { + fitColumnWidth(i, cellStyle.getDataFormatString().length() + 4); + } + pCells[i].setCellValue(value); + } + return true; + } + } catch (Exception e) { + throw new ExcelException(7, "Invalid cell value"); + } + return false; + } + + public Date getDate() throws ExcelException { + Date returnValue = null; + try { + returnValue = pCells[1].getDateCellValue(); + } catch (Exception e) { // Doc: For strings we throw an exception. For blank cells we return a null + throw new ExcelException(7, "Invalid cell value"); + } + if (returnValue == null) + returnValue = CommonUtil.nullDate(); + return returnValue; + } + + public boolean setTextImpl(String value) throws ExcelException { + CheckReadonlyDocument(); + + try { + for (int i = 1; i <= cellCount; i++) { + // pCells[i].setEncoding((short)1); + if (value.length() > 0 && value.charAt(0) == '=') { + try { + pCells[i].setCellFormula(value.substring(1)); + } catch (Exception e) { + pCells[i].setCellType(CellType.STRING); + pCells[i].setCellValue(value); + } + } else + pCells[i].setCellValue(value); + } + return true; + } catch (Exception e) { + throw new ExcelException(7, "Invalid cell value", e); + } + } + + private void CheckReadonlyDocument() throws ExcelReadonlyException { + if (readonly) { + throw new ExcelReadonlyException(); + } + } + + public String getText() { + try { + if (pCells[1].getCellType() == CellType.FORMULA) + return "=" + pCells[1].getCellFormula(); + else if (pCells[1].getCellType() == CellType.NUMERIC) { + if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(pCells[1])) { + return pCells[1].getDateCellValue().toString(); + } else { + return Double.toString(pCells[1].getNumericCellValue()); + } + } else + return pCells[1].getStringCellValue(); + } catch (Exception e) { + _errorHandler.setErrCod((short) 7); + _errorHandler.setErrDes("Invalid cell value"); + } + return null; + } + + public BigDecimal getValue() throws ExcelException { + BigDecimal value = new BigDecimal(0); + try { + CellType cType = pCells[1].getCellType(); + switch (cType) { + case FORMULA: + String type = getFormulaType(); + if (type == "N") + value = new BigDecimal(pCells[1].getNumericCellValue()); + else if (type == "D") + value = new BigDecimal(getDate().getTime()); + break; + case BOOLEAN: + Boolean b = pCells[1].getBooleanCellValue(); + value = new BigDecimal((b) ? 1 : 0); + break; + default: + value = new BigDecimal(pCells[1].getNumericCellValue()); + } + } catch (Exception e) { + throw new ExcelException(7, "Invalid cell value"); + } + return value; + } + + public String getType() { + String type = null; + switch (pCells[1].getCellType()) { + case BLANK: + type = "U"; + break; + case BOOLEAN: + type = "N"; + break; + case ERROR: + type = "U"; + break; + case FORMULA: + type = getFormulaType(); + break; + case NUMERIC: + if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(pCells[1])) { + type = "D"; + } else { + type = "N"; + } + break; + case STRING: + type = "C"; + break; + default: + type = ""; + } + return type; + } + + private String getFormulaType() { + try { + pCells[1].getNumericCellValue(); + + DataFormatter formatter = new DataFormatter(); + + java.text.Format format = formatter.getDefaultFormat(pCells[1]); + if (format.getClass() == java.text.DateFormat.class) { + pCells[1].getDateCellValue(); + return "D"; + } else { + return "N"; + } + } catch (Exception e) { + try { + Date dVal = pCells[1].getDateCellValue(); + if (dVal != null) { + return "D"; + } + } catch (Exception e1) { + } + } + String sVal = ""; + try { + sVal = pCells[1].getStringCellValue(); + } catch (Exception e) { + } + if (!sVal.equals("")) { + return "C"; + } else { + return "U"; + } + } + + public double getSize() { + return pWorkbook.getFontAt(pCells[1].getCellStyle().getFontIndex()).getFontHeightInPoints(); + } + + public void setSize(double value) throws ExcelReadonlyException { + CheckReadonlyDocument(); + + try { + for (int i = 1; i <= cellCount; i++) { + HSSFCellStyle cellStyle = pCells[1].getCellStyle(); + HSSFFont fontCell = pWorkbook.getFontAt(cellStyle.getFontIndex()); + HSSFCellStyle newStyle = null; + HSSFFont newFont = null; + + if (fontCell.getFontHeightInPoints() != value) { + // System.out.println("Changing Size..."); + newFont = getInternalFont(fontCell.getBold(), fontCell.getColor(), (short) value, + fontCell.getFontName(), fontCell.getItalic(), fontCell.getStrikeout(), + fontCell.getTypeOffset(), fontCell.getUnderline()); + copyPropertiesFont(newFont, fontCell); + + newFont.setFontHeightInPoints((short) value); + + newStyle = stylesCache.getCellStyle(newFont); + copyPropertiesStyle(newStyle, cellStyle); + + newStyle.setFont(newFont); + pCells[1].setCellStyle(newStyle); + } + } + + } catch (Exception e) { + + } + } + + public String getFont() { + return pWorkbook.getFontAt(pCells[1].getCellStyle().getFontIndex()).getFontName(); + } + + protected HSSFFont getInternalFont(boolean bold, short color, short fontHeight, String name, boolean italic, + boolean strikeout, short typeOffset, byte underline) { + HSSFFont font = pWorkbook.findFont(bold, color, fontHeight, name, italic, strikeout, typeOffset, underline); + if (font == null) { + font = pWorkbook.createFont(); + } + return font; + } + + public void setFont(String value) throws ExcelException { + CheckReadonlyDocument(); + + try { + for (int i = 1; i <= cellCount; i++) { + HSSFCellStyle cellStyle = pCells[i].getCellStyle(); + HSSFFont fontCell = pWorkbook.getFontAt(cellStyle.getFontIndex()); + HSSFCellStyle newStyle = null; + HSSFFont newFont = null; + + if (!fontCell.getFontName().equals(value)) { + newFont = getInternalFont(fontCell.getBold(), fontCell.getColor(), fontCell.getFontHeight(), value, + fontCell.getItalic(), fontCell.getStrikeout(), fontCell.getTypeOffset(), + fontCell.getUnderline()); + copyPropertiesFont(newFont, fontCell); + + newFont.setFontName(value); + + newStyle = stylesCache.getCellStyle(newFont); + copyPropertiesStyle(newStyle, cellStyle); + + newStyle.setFont(newFont); + pCells[i].setCellStyle(newStyle); + } + } + + } catch (Exception e) { + throw new ExcelException(7, "Invalid cell value"); + } + } + + public short getBold() { + if (pWorkbook.getFontAt(pCells[1].getCellStyle().getFontIndex()).getBold()) { + return 1; + } + return 0; + } + + public void setBold(short value) throws ExcelException { + CheckReadonlyDocument(); + + try { + + for (int i = 1; i <= cellCount; i++) { + HSSFCellStyle cellStyle = pCells[i].getCellStyle(); + HSSFFont fontCell = pWorkbook.getFontAt(cellStyle.getFontIndex()); + HSSFCellStyle newStyle = null; + HSSFFont newFont = null; + + switch (value) { + case 0: + if (fontCell.getBold()) { + newFont = getInternalFont(true, fontCell.getColor(), fontCell.getFontHeight(), + fontCell.getFontName(), fontCell.getItalic(), fontCell.getStrikeout(), + fontCell.getTypeOffset(), fontCell.getUnderline()); + copyPropertiesFont(newFont, fontCell); + + newFont.setBold(true); + + newStyle = stylesCache.getCellStyle(newFont); + copyPropertiesStyle(newStyle, cellStyle); + + newStyle.setFont(newFont); + pCells[i].setCellStyle(newStyle); + } + break; + case 1: + if (!fontCell.getBold()) { + newFont = getInternalFont(true, fontCell.getColor(), fontCell.getFontHeight(), + fontCell.getFontName(), fontCell.getItalic(), fontCell.getStrikeout(), + fontCell.getTypeOffset(), fontCell.getUnderline()); + copyPropertiesFont(newFont, fontCell); + + newFont.setBold(true); + + newStyle = stylesCache.getCellStyle(newFont); + copyPropertiesStyle(newStyle, cellStyle); + + newStyle.setFont(newFont); + pCells[i].setCellStyle(newStyle); + } + break; + default: + throw new ExcelException(6, "Invalid font properties"); + } + + + } + } catch (Exception e) { + throw new ExcelException(6, "Invalid bold value"); + } + } + + public short getItalic() { + if (pWorkbook.getFontAt(pCells[1].getCellStyle().getFontIndex()).getItalic()) { + return 1; + } + return 0; + } + + public void setItalic(short value) throws ExcelException { + CheckReadonlyDocument(); + + try { + + for (int i = 1; i <= cellCount; i++) { + HSSFCellStyle cellStyle = pCells[i].getCellStyle(); + HSSFFont fontCell = pWorkbook.getFontAt(cellStyle.getFontIndex()); + HSSFCellStyle newStyle = null; + HSSFFont newFont = null; + + switch (value) { + case 0: + if (fontCell.getItalic()) { + newFont = getInternalFont(fontCell.getBold(), fontCell.getColor(), fontCell.getFontHeight(), + fontCell.getFontName(), false, fontCell.getStrikeout(), fontCell.getTypeOffset(), + fontCell.getUnderline()); + copyPropertiesFont(newFont, fontCell); + + newFont.setItalic(false); + + newStyle = stylesCache.getCellStyle(newFont); + copyPropertiesStyle(newStyle, cellStyle); + + newStyle.setFont(newFont); + pCells[i].setCellStyle(newStyle); + } + break; + case 1: + if (!fontCell.getItalic()) { + newFont = getInternalFont(fontCell.getBold(), fontCell.getColor(), fontCell.getFontHeight(), + fontCell.getFontName(), true, fontCell.getStrikeout(), fontCell.getTypeOffset(), + fontCell.getUnderline()); + copyPropertiesFont(newFont, fontCell); + + newFont.setItalic(true); + + newStyle = stylesCache.getCellStyle(newFont); + copyPropertiesStyle(newStyle, cellStyle); + + newStyle.setFont(newFont); + pCells[i].setCellStyle(newStyle); + } + break; + default: + throw new ExcelException(6, "Invalid font properties"); + } + } + } catch (Exception e) { + throw new ExcelException(6, "Invalid font properties"); + } + } + + public short getUnderline() { + if (pWorkbook.getFontAt(pCells[1].getCellStyle().getFontIndex()).getUnderline() != HSSFFont.U_NONE) { + return 1; + } + return 0; + } + + public void setUnderline(short value) throws ExcelException { + CheckReadonlyDocument(); + + try { + + for (int i = 1; i <= cellCount; i++) { + HSSFCellStyle cellStyle = pCells[i].getCellStyle(); + HSSFFont fontCell = pWorkbook.getFontAt(cellStyle.getFontIndex()); + HSSFCellStyle newStyle = null; + HSSFFont newFont = null; + + switch (value) { + case 0: + if (fontCell.getUnderline() != HSSFFont.U_NONE) { + newFont = getInternalFont(fontCell.getBold(), fontCell.getColor(), fontCell.getFontHeight(), + fontCell.getFontName(), fontCell.getItalic(), fontCell.getStrikeout(), + fontCell.getTypeOffset(), HSSFFont.U_NONE); + copyPropertiesFont(newFont, fontCell); + + newFont.setUnderline(HSSFFont.U_NONE); + + newStyle = stylesCache.getCellStyle(newFont); + copyPropertiesStyle(newStyle, cellStyle); + + newStyle.setFont(newFont); + pCells[i].setCellStyle(newStyle); + } + break; + case 1: + if (fontCell.getUnderline() != HSSFFont.U_SINGLE) { + newFont = getInternalFont(fontCell.getBold(), fontCell.getColor(), fontCell.getFontHeight(), + fontCell.getFontName(), fontCell.getItalic(), fontCell.getStrikeout(), + fontCell.getTypeOffset(), HSSFFont.U_SINGLE); + copyPropertiesFont(newFont, fontCell); + + newFont.setUnderline(HSSFFont.U_SINGLE); + + newStyle = stylesCache.getCellStyle(newFont); + copyPropertiesStyle(newStyle, cellStyle); + + newStyle.setFont(newFont); + pCells[i].setCellStyle(newStyle); + } + break; + default: + throw new ExcelException(6, "Invalid font property"); + } + } + } catch (Exception e) { + throw new ExcelException(6, "Invalid font properties"); + } + } + + public long getColor() { + return pWorkbook.getFontAt(pCells[1].getCellStyle().getFontIndex()).getColor() - 7; + } + + public void setColor(short value) throws ExcelException { + setColor((long) value); + } + + public void setColor(int value) throws ExcelException { + setColor((long) value); + } + + // Implementación para XLS que usa HSSFPalette para configurar colores + public void setColor(long value) throws ExcelException { + CheckReadonlyDocument(); + + try { + // En XLS necesitamos manejar la paleta de colores que es más limitada que en XLSX + HSSFPalette palette = pWorkbook.getCustomPalette(); + short colorIdx = (short) (value + 7); // Ajustar al índice de la paleta + + for (int i = 1; i <= cellCount; i++) { + HSSFCellStyle cellStyle = pCells[i].getCellStyle(); + HSSFFont fontCell = pWorkbook.getFontAt(cellStyle.getFontIndex()); + HSSFCellStyle newStyle = null; + HSSFFont newFont = null; + + // En XLS, solo podemos usar índices de color predefinidos o personalizados + if (fontCell.getColor() != colorIdx) { + newFont = (HSSFFont) getInternalFont(fontCell.getBold(), colorIdx, + fontCell.getFontHeight(), fontCell.getFontName(), fontCell.getItalic(), + fontCell.getStrikeout(), fontCell.getTypeOffset(), fontCell.getUnderline()); + copyPropertiesFont(newFont, fontCell); + + newFont.setColor(colorIdx); + + newStyle = stylesCache.getCellStyle(newFont); + copyPropertiesStyle(newStyle, cellStyle); + + newStyle.setFont(newFont); + pCells[i].setCellStyle(newStyle); + } + } + } catch (Exception e) { + throw new ExcelException(6, "Invalid font properties", e); + } + } + + public String getHyperlink() throws ExcelException { + try { + Hyperlink link = pCells[1].getHyperlink(); + if (link != null) { + return link.getAddress(); + } + } catch (Exception e) { + throw new ExcelException(7, "Invalid cell value", e); + } + return ""; + } + + public boolean setHyperlink(String value) throws ExcelException { + CheckReadonlyDocument(); + try { + CreationHelper createHelper = pWorkbook.getCreationHelper(); + Hyperlink link = createHelper.createHyperlink(org.apache.poi.common.usermodel.HyperlinkType.URL); + link.setAddress(value); + for (int i = 1; i <= cellCount; i++) { + pCells[i].setHyperlink(link); + } + return true; + } catch (Exception e) { + throw new ExcelException(7, "Invalid cell value", e); + } + } + + @Override + public String getHyperlinkValue() { + try { + return this.getHyperlink(); + } catch (ExcelException e) { + _errorHandler.setErrCod((short) e.get_errorCode()); + _errorHandler.setErrDes(e.get_errDsc()); + } + return ""; + } + + @Override + public Boolean setHyperlinkValue(String value) { + try { + return this.setHyperlink(value); + } catch (ExcelException e) { + _errorHandler.setErrCod((short) e.get_errorCode()); + _errorHandler.setErrDes(e.get_errDsc()); + } + return false; + } + + protected void copyPropertiesStyle(HSSFCellStyle dest, HSSFCellStyle source) { + dest.cloneStyleFrom(source); + } + + protected void copyPropertiesFont(HSSFFont dest, HSSFFont source) { + dest.setFontHeightInPoints(source.getFontHeightInPoints()); + dest.setFontName(source.getFontName()); + dest.setBold(source.getBold()); + dest.setItalic(source.getItalic()); + dest.setUnderline(source.getUnderline()); + dest.setColor(source.getColor()); + } + + private void fitColumnWidth(int i, int data) { + if (fitColumnWidth) { + int colW = pSelectedSheet.getColumnWidth((int) (i + colStartIdx - 1)); + if ((256 * (data)) > colW) { + colW = (short) (256 * (data)); + } + pSelectedSheet.setColumnWidth((short) (i + colStartIdx - 1), colW); + } + } + + public void setFitColumnWidth(boolean fitCol) { + fitColumnWidth = fitCol; + } + + public boolean getFitColumnWidth() { + return fitColumnWidth; + } + + @Override + public int getRowStart() { + return rowStartIdx + 1; + } + + @Override + public int getRowEnd() { + return rowEndIdx + 1; + } + + @Override + public int getColumnStart() { + return colStartIdx + 1; + } + + @Override + public int getColumnEnd() { + return colEndIdx + 1; + } + + @Override + public String getValueType() { + return this.getType(); + } + + @Override + public java.math.BigDecimal getNumericValue() { + try { + return this.getNumber(); + } catch (ExcelException e) { + _errorHandler.setErrCod((short) e.get_errorCode()); + _errorHandler.setErrDes(e.get_errDsc()); + } + return new java.math.BigDecimal(0); + } + + @Override + public Date getDateValue() { + try { + return this.getDate(); + } catch (ExcelException e) { + _errorHandler.setErrCod((short) e.get_errorCode()); + _errorHandler.setErrDes(e.get_errDsc()); + } + return null; + } + + @Override + public Boolean setText(String value) { + try { + return this.setTextImpl(value); + } catch (ExcelException e) { + _errorHandler.setErrCod((short) e.get_errorCode()); + _errorHandler.setErrDes(e.get_errDsc()); + } + return false; + } + + @Override + public Boolean setNumericValue(java.math.BigDecimal d) { + try { + return this.setNumber(d.doubleValue()); + } catch (ExcelException e) { + _errorHandler.setErrCod((short) e.get_errorCode()); + _errorHandler.setErrDes(e.get_errDsc()); + } + return false; + } + + @Override + public Boolean setDateValue(Date value) { + try { + return this.setDate(value); + } catch (ExcelException e) { + _errorHandler.setErrCod((short) e.get_errorCode()); + _errorHandler.setErrDes(e.get_errDsc()); + } + return false; + } + + @Override + public Boolean empty() { + for (int i = 1; i <= cellCount; i++) { + pCells[i].setCellValue(""); + } + return this.cellCount > 0; + } + + @Override + public Boolean mergeCells() { + CellRangeAddress cellRange = new CellRangeAddress(rowStartIdx, rowEndIdx, colStartIdx, colEndIdx); + for (int i = 0; i < pSelectedSheet.getNumMergedRegions(); i++){ + CellRangeAddress mergedRegion = pSelectedSheet.getMergedRegion(i); + if (cellRange.intersects(mergedRegion)){ + pSelectedSheet.removeMergedRegion(i); + } + } + pSelectedSheet.addMergedRegion(cellRange); + return true; + } + + @Override + public Boolean setCellStyle(ExcelStyle newCellStyle) { + if (cellCount > 0) { + HSSFCellStyle style = pWorkbook.createCellStyle(); + + style.cloneStyleFrom(style); + applyNewCellStyle(style, newCellStyle); + for (int i = 1; i <= cellCount; i++) { + pCells[i].setCellStyle(style); + } + } + return cellCount > 0; + } + + private HSSFColor toColor(ExcelColor color) { + // En HSSF, necesitamos usar la paleta de colores personalizada + HSSFPalette palette = pWorkbook.getCustomPalette(); + byte r = (byte)(color.getRed() & 0xFF); + byte g = (byte)(color.getGreen() & 0xFF); + byte b = (byte)(color.getBlue() & 0xFF); + + // Tratar de encontrar el color más cercano o crear un color personalizado + try { + short customColorIdx = 0x40; // Empezar a usar índices personalizados + palette.setColorAtIndex(customColorIdx, r, g, b); + return palette.getColor(customColorIdx); + } catch (Exception e) { + // Si hay un problema con los colores personalizados, devolver negro + return palette.getColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex()); + } + } + + private HSSFCellStyle applyNewCellStyle(HSSFCellStyle cellStyle, ExcelStyle newCellStyle) { + ExcelFont cellFont = newCellStyle.getCellFont(); + if (cellFont != null && cellFont.isDirty()) { + HSSFFont cellStyleFont = pWorkbook.createFont(); + cellStyle.setFont(cellStyleFont); + ExcelFont font = newCellStyle.getCellFont(); + if (font != null) { + if (font.getBold() != null) { + cellStyleFont.setBold(font.getBold()); + } + if (font.getFontFamily() != null && font.getFontFamily().length() > 0) { + cellStyleFont.setFontName(font.getFontFamily()); + } + if (font.getItalic() != null) { + cellStyleFont.setItalic(font.getItalic()); + } + if (font.getStrike() != null) { + cellStyleFont.setStrikeout(font.getStrike()); + } + if (font.getSize() != null) { + cellStyleFont.setFontHeight(font.getSize().shortValue()); + } + if (font.getUnderline() != null) { + cellStyleFont.setUnderline((byte) (font.getUnderline() ? 1 : 0)); + } + if (font.getColor() != null && font.getColor().isDirty()) { + HSSFColor color = toColor(font.getColor()); + cellStyleFont.setColor(color.getIndex()); + } + } + } + + ExcelFill cellfill = newCellStyle.getCellFill(); + if (cellfill != null && cellfill.getCellBackColor() != null && cellfill.getCellBackColor().isDirty()) { + HSSFColor color = toColor(cellfill.getCellBackColor()); + cellStyle.setFillForegroundColor(color.getIndex()); + cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + } + + ExcelAlignment alignment = newCellStyle.getCellAlignment(); + if (alignment != null && alignment.isDirty()) { + if (alignment.getHorizontalAlignment() != null) { + HorizontalAlignment align; + switch (alignment.getHorizontalAlignment()) { + case ExcelAlignment.HORIZONTAL_ALIGN_CENTER: + align = HorizontalAlignment.CENTER; + break; + case ExcelAlignment.HORIZONTAL_ALIGN_LEFT: + align = HorizontalAlignment.LEFT; + break; + case ExcelAlignment.HORIZONTAL_ALIGN_RIGHT: + align = HorizontalAlignment.RIGHT; + break; + default: + align = HorizontalAlignment.forInt(alignment.getHorizontalAlignment()); + } + cellStyle.setAlignment(align); + } + if (alignment.getVerticalAlignment() != null) { + VerticalAlignment align; + switch (alignment.getVerticalAlignment()) { + case ExcelAlignment.VERTICAL_ALIGN_BOTTOM: + align = VerticalAlignment.BOTTOM; + break; + case ExcelAlignment.VERTICAL_ALIGN_MIDDLE: + align = VerticalAlignment.CENTER; + break; + case ExcelAlignment.VERTICAL_ALIGN_TOP: + align = VerticalAlignment.TOP; + break; + default: + align = VerticalAlignment.forInt(alignment.getVerticalAlignment()); + } + + cellStyle.setVerticalAlignment(align); + } + } + + if (newCellStyle.isLocked() != null) { + cellStyle.setLocked(newCellStyle.isLocked()); + } + + if (newCellStyle.isHidden() != null) { + cellStyle.setHidden(newCellStyle.isHidden()); + } + + if (newCellStyle.getShrinkToFit() != null) { + cellStyle.setShrinkToFit(newCellStyle.getShrinkToFit()); + } + + if (newCellStyle.getWrapText() != null) { + cellStyle.setWrapText(newCellStyle.getWrapText()); + } + + if (newCellStyle.getTextRotation() != 0) { + cellStyle.setRotation((short) (newCellStyle.getTextRotation())); + } + + if (newCellStyle.getIndentation() >= 0) { + cellStyle.setIndention((short) newCellStyle.getIndentation()); + } + + if (newCellStyle.getDataFormat() != null && newCellStyle.getDataFormat().length() > 0) { + cellStyle.setDataFormat(pWorkbook.createDataFormat().getFormat(newCellStyle.getDataFormat())); + } + + if (newCellStyle.getBorder() != null) { + ExcelCellBorder cellBorder = newCellStyle.getBorder(); + applyBorderSide(cellStyle, BorderCellSide.TOP, cellBorder.getBorderTop()); + applyBorderSide(cellStyle, BorderCellSide.BOTTOM, cellBorder.getBorderBottom()); + applyBorderSide(cellStyle, BorderCellSide.LEFT, cellBorder.getBorderLeft()); + applyBorderSide(cellStyle, BorderCellSide.RIGHT, cellBorder.getBorderRight()); + } + return cellStyle; + } + + private void applyBorderSide(HSSFCellStyle cellStyle, BorderCellSide bSide, ExcelBorder border) { + if (border != null && border.isDirty()) { + if (border.getBorderColor().isDirty()) { + HSSFColor color = toColor(border.getBorderColor()); + if (bSide == BorderCellSide.BOTTOM) { + cellStyle.setBottomBorderColor(color.getIndex()); + } else if (bSide == BorderCellSide.TOP) { + cellStyle.setTopBorderColor(color.getIndex()); + } else if (bSide == BorderCellSide.LEFT) { + cellStyle.setLeftBorderColor(color.getIndex()); + } else if (bSide == BorderCellSide.RIGHT) { + cellStyle.setRightBorderColor(color.getIndex()); + } + } + if (border.getBorder() != null && border.getBorder().length() > 0) { + BorderStyle bs = BorderStyle.valueOf(border.getBorder()); + if (bSide == BorderCellSide.BOTTOM) { + cellStyle.setBorderBottom(bs); + } else if (bSide == BorderCellSide.TOP) { + cellStyle.setBorderTop(bs); + } else if (bSide == BorderCellSide.LEFT) { + cellStyle.setBorderLeft(bs); + } else if (bSide == BorderCellSide.RIGHT) { + cellStyle.setBorderRight(bs); + } + } + } + } + + public enum BorderCellSide { + RIGHT, LEFT, TOP, BOTTOM + } +} \ No newline at end of file diff --git a/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/hssf/ExcelSpreadsheet.java b/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/hssf/ExcelSpreadsheet.java new file mode 100644 index 000000000..efdca5e2b --- /dev/null +++ b/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/hssf/ExcelSpreadsheet.java @@ -0,0 +1,496 @@ +package com.genexus.msoffice.excel.poi.hssf; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +import com.genexus.msoffice.excel.Constants; +import com.genexus.msoffice.excel.exception.ExcelException; +import com.genexus.msoffice.excel.IExcelSpreadsheet; +import com.genexus.msoffice.excel.IGXError; +import com.genexus.msoffice.excel.exception.ExcelTemplateNotFoundException; +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.FormulaEvaluator; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; + +import com.genexus.util.GXFile; +import com.genexus.diagnostics.core.ILogger; +import com.genexus.diagnostics.core.LogManager; +import com.genexus.msoffice.excel.IExcelCellRange; +import com.genexus.msoffice.excel.IExcelWorksheet; + +public class ExcelSpreadsheet implements IExcelSpreadsheet { + public static final ILogger logger = LogManager.getLogger(ExcelSpreadsheet.class); + private HSSFWorkbook _workbook; + private String _documentFileName; + private boolean _autoFitColumnsOnSave = false; + + private boolean _isReadonly; + private IGXError _errorHandler; + + private StylesCache _stylesCache; + + public ExcelSpreadsheet(IGXError errHandler, String fileName, String template) throws ExcelTemplateNotFoundException, IOException { + _errorHandler = errHandler; + if (fileName.indexOf('.') == -1) { + fileName += ".xls"; + } + + InputStream is = null; + try { + if (!template.equals("")) { + GXFile templateFile = new GXFile(template); + if (templateFile.exists()) { + is = templateFile.getStream(); + _workbook = new HSSFWorkbook(is); + } else { + throw new ExcelTemplateNotFoundException(); + } + } else { + GXFile file = new GXFile(fileName, Constants.EXTERNAL_PRIVATE_UPLOAD); + try (InputStream fileStream = file.getStream()) { + if (file.exists()) + _workbook = new HSSFWorkbook(fileStream); + else + _workbook = new HSSFWorkbook(); + } + } + } finally { if (is != null) is.close(); } + + _documentFileName = fileName; + + _stylesCache = new StylesCache(_workbook); + + } + + public boolean getAutoFit() { + return _autoFitColumnsOnSave; + } + + public void setAutofit(boolean autoFitColumnsOnSave) { + this._autoFitColumnsOnSave = autoFitColumnsOnSave; + } + + public Boolean save() throws ExcelException { + return saveAsImpl(_documentFileName); + } + + private Boolean saveAsImpl(String fileName) throws ExcelException { + ByteArrayOutputStream fs = null; + ByteArrayInputStream in = null; + GXFile file = null; + boolean savedOK = false; + + autoFitColumns(); + recalculateFormulas(); + + try { + fs = new ByteArrayOutputStream(); + _workbook.write(fs); + in = new ByteArrayInputStream(fs.toByteArray()); + fs.close(); + file = new GXFile(fileName, Constants.EXTERNAL_PRIVATE_UPLOAD); + savedOK = file.create(in, true); + in.close(); + file.close(); + } catch (Exception e) { + try { + if (fs != null) + fs.close(); + if (in != null) + in.close(); + if (file != null) + file.close(); + } catch (Exception e1) { + logger.error("saveAsImpl", e1); + } + + throw new ExcelException(12, "GeneXus Office Module Error: " + e.toString()); + } + return savedOK; + } + + public Boolean saveAs(String newFileName) throws ExcelException { + return saveAsImpl(newFileName); + } + + public Boolean close() throws ExcelException { + return save(); + } + + public IExcelCellRange getCells(IExcelWorksheet worksheet, int startRow, int startCol, int rowCount, int colCount) throws ExcelException { + return new ExcelCells(_errorHandler, this, _workbook, _workbook.getSheet(worksheet.getName()), startRow - 1, startCol - 1, rowCount, colCount, _isReadonly, _stylesCache); + } + + public IExcelCellRange getCell(IExcelWorksheet worksheet, int startRow, int startCol) throws ExcelException { + return getCells(worksheet, startRow, startCol, 1, 1); + } + + public Boolean insertRow(IExcelWorksheet worksheet, int rowIdx, int rowCount) { + HSSFSheet sheet = getSheet(worksheet); + + int createNewRowAt = rowIdx; // Add the new row between row 9 and 10 + + if (sheet != null) { + for (int i = 1; i <= rowCount; i++) { + int lastRow = Math.max(0, sheet.getLastRowNum()); + if (lastRow < rowIdx) { + for (int j = lastRow; j <= rowIdx; j++) { + sheet.createRow(j); + } + } + else { + if (sheet.getRow(createNewRowAt) == null) { + sheet.createRow(createNewRowAt); + } + sheet.shiftRows(createNewRowAt, lastRow, 1, true, false); + } + } + return true; + } + return false; + } + + public Boolean insertColumn(IExcelWorksheet worksheet, int colIdx, int colCount) { + /* + * HSSFSheet sheet = getSheet(worksheet); int createNewColumnAt = colIdx; //Add + * the new row between row 9 and 10 + * + * if (sheet != null) { for (int i = 1; i<= colCount; i++) { + * + * int lastRow = sheet.getLastRowNum(); sheet.shi(createNewColumnAt, lastRow, 1, + * true, false); HSSFRow newRow = sheet.createRow(createNewColumnAt); } return + * true; } return false; + */ + return false; // POI not supported + } + + public Boolean deleteRow(IExcelWorksheet worksheet, int rowIdx) { + HSSFSheet sheet = getSheet(worksheet); + if (sheet != null) { + HSSFRow row = sheet.getRow(rowIdx); + if (row != null) { + sheet.removeRow(row); + } + int rowIndex = rowIdx; + int lastRowNum = sheet.getLastRowNum(); + if (rowIndex >= 0 && rowIndex < lastRowNum) { + sheet.shiftRows(rowIndex + 1, lastRowNum, -1); + } + } + return sheet != null; + } + + public List getWorksheets() { + List list = new ArrayList(); + for (int i = 0; i < _workbook.getNumberOfSheets(); i++) { + HSSFSheet sheet = _workbook.getSheetAt(i); + if (sheet != null) { + list.add(new ExcelWorksheet(sheet)); + } + } + return list; + } + + public Boolean insertWorksheet(String newSheetName, int idx) throws ExcelException{ + HSSFSheet newSheet; + if (_workbook.getSheet(newSheetName) == null) { + newSheet = _workbook.createSheet(newSheetName); + } + else + { + throw new ExcelException(13, "The workbook already contains a sheet named:" + newSheetName); + } + return newSheet != null; + } + + @Override + public boolean cloneSheet(String sheetName, String newSheetName) throws ExcelException{ + int idx = _workbook.getSheetIndex(sheetName); + if (_workbook.getSheet(newSheetName) != null) { + throw new ExcelException(13, "The workbook already contains a sheet named:" + newSheetName); + } + if (idx < 0) { + throw new ExcelException(14, "The workbook does not contain a sheet named:" + sheetName); + } + _workbook.cloneSheet(idx); + int newIdx = _workbook.getSheetIndex(_workbook.getSheetName(_workbook.getNumberOfSheets() - 1)); + _workbook.setSheetName(newIdx, newSheetName); + return true; + } + + private HSSFSheet getSheet(IExcelWorksheet sheet) { + return _workbook.getSheet(sheet.getName()); + } + + private void recalculateFormulas() { + try { + _workbook.getCreationHelper().createFormulaEvaluator().evaluateAll(); + _workbook.setForceFormulaRecalculation(true); + } catch (Exception e) { + logger.error("recalculateFormulas", e); + } + } + + private void autoFitColumns() { + if (_autoFitColumnsOnSave) { + int sheetsCount = _workbook.getNumberOfSheets(); + for (int i = 0; i < sheetsCount; i++) { + org.apache.poi.ss.usermodel.Sheet sheet = _workbook.getSheetAt(i); + int columnCount = 0; + for (int j =0; j <= sheet.getLastRowNum(); j++){ + Row row = sheet.getRow(j); + if (row != null) { + columnCount = Math.max(columnCount, row.getLastCellNum()); + } + } + for (int j = 0; j < columnCount; j++) { + sheet.autoSizeColumn(j); + } + } + } + } + + + @Override + public boolean setActiveWorkSheet(String name) { + int idx = _workbook.getSheetIndex(name); + if (idx >= 0) { + _workbook.setActiveSheet(idx); + _workbook.setSelectedTab(idx); + } + return idx >= 0; + } + + @Override + public ExcelWorksheet getWorkSheet(String name) { + HSSFSheet sheet = _workbook.getSheet(name); + if (sheet != null) + return new ExcelWorksheet(sheet); + return null; + } + + @Override + public Boolean getAutofit() { + return _autoFitColumnsOnSave; + } + + @Override + public void setColumnWidth(IExcelWorksheet worksheet, int colIdx, int width) { + HSSFSheet sheet = _workbook.getSheet(worksheet.getName()); + if (colIdx >= 1 && sheet != null && width <= 255) { + sheet.setColumnWidth(colIdx - 1, 256 * width); + } + } + + @Override + public void setRowHeight(IExcelWorksheet worksheet, int rowIdx, int height) { + HSSFSheet sheet = _workbook.getSheet(worksheet.getName()); + if (rowIdx >= 1 && sheet != null) { + rowIdx = rowIdx - 1; + if (sheet.getRow(rowIdx) == null) { + sheet.createRow(rowIdx); + } + sheet.getRow(rowIdx).setHeightInPoints((short) height); + } + } + + @Override + public Boolean deleteColumn(IExcelWorksheet worksheet, int colIdx) { + HSSFSheet sheet = _workbook.getSheet(worksheet.getName()); + if (colIdx >= 0) { + return deleteColumnImpl(sheet, colIdx); + } + return false; + } + + private Boolean deleteColumnImpl(HSSFSheet sheet, int columnToDelete) { + for (int rId = 0; rId <= sheet.getLastRowNum(); rId++) { + Row row = sheet.getRow(rId); + for (int cID = columnToDelete; row != null && cID <= row.getLastCellNum(); cID++) { + Cell cOld = row.getCell(cID); + if (cOld != null) { + row.removeCell(cOld); + } + Cell cNext = row.getCell(cID + 1); + if (cNext != null) { + Cell cNew = row.createCell(cID, cNext.getCellType()); + cloneCell(cNew, cNext); + //Set the column width only on the first row. + //Other wise the second row will overwrite the original column width set previously. + if (rId == 0) { + sheet.setColumnWidth(cID, sheet.getColumnWidth(cID + 1)); + + } + } + } + } + return true; + } + + private int getNumberOfRows(HSSFSheet sheet) { + int rowNum = sheet.getLastRowNum() + 1; + return rowNum; + } + + public int getNrColumns(HSSFSheet sheet) { + Row headerRow = sheet.getRow(0); + return headerRow.getLastCellNum(); + } + + public void insertNewColumnBefore(HSSFSheet sheet, int columnIndex) { + FormulaEvaluator evaluator = _workbook.getCreationHelper() + .createFormulaEvaluator(); + evaluator.clearAllCachedResultValues(); + + int nrRows = getNumberOfRows(sheet); + int nrCols = getNrColumns(sheet); + + for (int row = 0; row < nrRows; row++) { + Row r = sheet.getRow(row); + + if (r == null) { + continue; + } + + // shift to right + for (int col = nrCols; col > columnIndex; col--) { + Cell rightCell = r.getCell(col); + if (rightCell != null) { + r.removeCell(rightCell); + } + + Cell leftCell = r.getCell(col - 1); + + if (leftCell != null) { + Cell newCell = r.createCell(col, leftCell.getCellType()); + cloneCell(newCell, leftCell); + /*if (newCell.getCellTypeEnum() == CellType.FORMULA) { + newCell.setCellFormula(ExcelHelper.updateFormula(newCell.getCellFormula(), columnIndex)); + evaluator.notifySetFormula(newCell); + CellValue cellValue = evaluator.evaluate(newCell); + evaluator.evaluateFormulaCell(newCell); + }*/ + } + } + + // delete old column + CellType cellType = CellType.BLANK; + + Cell currentEmptyWeekCell = r.getCell(columnIndex); + if (currentEmptyWeekCell != null) { +// cellType = currentEmptyWeekCell.getCellType(); + r.removeCell(currentEmptyWeekCell); + } + + // create new column + r.createCell(columnIndex, cellType); + } + + // Adjust the column widths + for (int col = nrCols; col > columnIndex; col--) { + sheet.setColumnWidth(col, sheet.getColumnWidth(col - 1)); + } + + // currently updates formula on the last cell of the moved column + // TODO: update all cells if their formulas contain references to the moved cell +// Row specialRow = sheet.getRow(nrRows-1); +// Cell cellFormula = specialRow.createCell(nrCols - 1); +// cellFormula.setCellType(HSSFCell.CELL_TYPE_FORMULA); +// cellFormula.setCellFormula(formula); + + //HSSFFormulaEvaluator.evaluateAllFormulaCells(workbook); + } + + /* + * Takes an existing Cell and merges all the styles and forumla into the new + * one + */ + private static void cloneCell(Cell cNew, Cell cOld) { + cNew.setCellComment(cOld.getCellComment()); + cNew.setCellStyle(cOld.getCellStyle()); + + switch (cOld.getCellType()) { + case BOOLEAN: { + cNew.setCellValue(cOld.getBooleanCellValue()); + break; + } + case NUMERIC: { + cNew.setCellValue(cOld.getNumericCellValue()); + break; + } + case STRING: { + cNew.setCellValue(cOld.getStringCellValue()); + break; + } + case ERROR: { + cNew.setCellValue(cOld.getErrorCellValue()); + break; + } + case FORMULA: { + cNew.setCellFormula(cOld.getCellFormula()); + break; + } + default: + //ignore + break; + } + } + + @Override + public boolean deleteSheet(int sheetIdx) { + if (_workbook.getNumberOfSheets() > sheetIdx) { + _workbook.removeSheetAt(sheetIdx); + return true; + } + return false; + } + + @Override + public boolean deleteSheet(String sheetName) { + if (_workbook.getSheetIndex(sheetName) >= 0) { + _workbook.removeSheetAt(_workbook.getSheetIndex(sheetName)); + return true; + } + return false; + } + + @Override + public boolean toggleColumn(IExcelWorksheet worksheet, int colIdx, Boolean visible) { + HSSFSheet sheet = _workbook.getSheet(worksheet.getName()); + if (sheet != null) { + sheet.setColumnHidden(colIdx, !visible); + return true; + } + return false; + } + + @Override + public boolean toggleRow(IExcelWorksheet worksheet, int i, Boolean visible) { + HSSFSheet sheet = _workbook.getSheet(worksheet.getName()); + if (sheet != null) { + HSSFRow row = sheet.getRow(i); + if (row == null) { + insertRow(worksheet, i, 1); + row = sheet.getRow(i); + } + if (row != null) { + CellStyle style = _workbook.createCellStyle(); + style.setHidden(!visible); //Does not work.. + row.setRowStyle(style); + row.setZeroHeight(!visible); + } + return true; + } + return false; + } + +} \ No newline at end of file diff --git a/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/hssf/ExcelWorksheet.java b/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/hssf/ExcelWorksheet.java new file mode 100644 index 000000000..0ca573300 --- /dev/null +++ b/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/hssf/ExcelWorksheet.java @@ -0,0 +1,87 @@ +package com.genexus.msoffice.excel.poi.hssf; + +import com.genexus.msoffice.excel.exception.ExcelException; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; + +import com.genexus.msoffice.excel.IExcelWorksheet; + +public class ExcelWorksheet implements IExcelWorksheet +{ + private HSSFSheet _sheet; + + public ExcelWorksheet() + { + + } + + + public ExcelWorksheet(HSSFSheet sheet) + { + _sheet = sheet; + } + + public String getName() + { + return _sheet.getSheetName(); + } + + public Boolean setHidden(boolean hidden) + { + if (_sheet != null) { + HSSFWorkbook wb = _sheet.getWorkbook(); + wb.setSheetHidden(sheetIndex(wb), hidden); + return true; + } + return false; + } + + public Boolean isHidden() + { + if (_sheet != null) { + HSSFWorkbook wb = _sheet.getWorkbook(); + return wb.isSheetHidden(sheetIndex(wb)); + } + return false; + } + + public Boolean rename(String newName) + { + if (_sheet != null) { + HSSFWorkbook wb = _sheet.getWorkbook(); + wb.setSheetName(wb.getSheetIndex(getName()), newName); + return getName().equals(newName); + } + return false; + } + + + @Override + public Boolean copy(String newName) { + if (_sheet != null) { + HSSFWorkbook wb = _sheet.getWorkbook(); + if(wb.getSheet(newName) == null) { + wb.cloneSheet(wb.getSheetIndex(getName())); + int newIdx = wb.getSheetIndex(wb.getSheetName(wb.getNumberOfSheets() - 1)); + wb.setSheetName(newIdx, newName); + return true; + } + } + return false; + } + + @Override + public void setProtected(String password) { + if (_sheet != null) { + if (password.length() == 0) + _sheet.protectSheet(null); + else + _sheet.protectSheet(password); + } + } + + private int sheetIndex(HSSFWorkbook wb) { + return wb.getSheetIndex(getName()); + } + +} \ No newline at end of file diff --git a/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/hssf/StylesCache.java b/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/hssf/StylesCache.java new file mode 100644 index 000000000..edf9fa723 --- /dev/null +++ b/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/hssf/StylesCache.java @@ -0,0 +1,51 @@ +package com.genexus.msoffice.excel.poi.hssf; + +import java.util.Hashtable; + +import org.apache.poi.hssf.usermodel.HSSFCellStyle; +import org.apache.poi.hssf.usermodel.HSSFFont; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; + +public class StylesCache +{ + private HSSFWorkbook pWorkbook; + private Hashtable stylesByFont; + private Hashtable stylesByFormat; + + public StylesCache(HSSFWorkbook pWorkbook) + { + this.pWorkbook = pWorkbook; + this.stylesByFont = new Hashtable(); + this.stylesByFormat = new Hashtable(); + } + + public HSSFCellStyle getCellStyle(HSSFFont newFont) + { + + String fontKey = newFont.getFontHeightInPoints() + newFont.getFontName() + newFont.getBold() + + newFont.getItalic() + newFont.getUnderline() + newFont.getColor(); + + Object styleObj = stylesByFont.get(fontKey); + if (styleObj != null) + { + return (HSSFCellStyle) styleObj; + } + HSSFCellStyle newStyle = pWorkbook.createCellStyle(); + stylesByFont.put(fontKey, newStyle); + return newStyle; + } + + public HSSFCellStyle getCellStyle(short format) + { + String formatKey = String.valueOf(format); + + Object styleObj = stylesByFormat.get(formatKey); + if (styleObj != null) + { + return (HSSFCellStyle) styleObj; + } + HSSFCellStyle newStyle = pWorkbook.createCellStyle(); + stylesByFormat.put(formatKey, newStyle); + return newStyle; + } +} \ No newline at end of file diff --git a/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/xssf/ExcelCells.java b/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/xssf/ExcelCells.java index 443c96936..3726c4718 100644 --- a/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/xssf/ExcelCells.java +++ b/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/xssf/ExcelCells.java @@ -870,12 +870,6 @@ public int getColumnEnd() { return colEndIdx + 1; } - @Override - public String getCellAdress() { - // TODO Auto-generated method stub - return null; - } - @Override public String getValueType() { return this.getType(); @@ -957,12 +951,6 @@ public Boolean mergeCells() { return true; } - @Override - public ExcelStyle getCellStyle() { - - return cellStyle; - } - @Override public Boolean setCellStyle(ExcelStyle newCellStyle) { if (cellCount > 0) { diff --git a/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/xssf/ExcelSpreadsheet.java b/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/xssf/ExcelSpreadsheet.java index aad0c4cd2..a383a86a8 100644 --- a/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/xssf/ExcelSpreadsheet.java +++ b/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/xssf/ExcelSpreadsheet.java @@ -190,8 +190,8 @@ public Boolean deleteRow(IExcelWorksheet worksheet, int rowIdx) { return sheet != null; } - public List getWorksheets() { - List list = new ArrayList(); + public List getWorksheets() { + List list = new ArrayList(); for (int i = 0; i < _workbook.getNumberOfSheets(); i++) { XSSFSheet sheet = _workbook.getSheetAt(i); if (sheet != null) { @@ -271,7 +271,7 @@ public boolean setActiveWorkSheet(String name) { } @Override - public ExcelWorksheet getWorkSheet(String name) { + public IExcelWorksheet getWorkSheet(String name) { XSSFSheet sheet = _workbook.getSheet(name); if (sheet != null) return new ExcelWorksheet(sheet); diff --git a/gxoffice/src/test/java/com/genexus/msoffice/excel/ExcelSpreadsheetTest.java b/gxoffice/src/test/java/com/genexus/msoffice/excel/ExcelSpreadsheetTest.java index 220f49b2e..2ab2ebec9 100644 --- a/gxoffice/src/test/java/com/genexus/msoffice/excel/ExcelSpreadsheetTest.java +++ b/gxoffice/src/test/java/com/genexus/msoffice/excel/ExcelSpreadsheetTest.java @@ -10,12 +10,9 @@ import java.util.List; import com.genexus.msoffice.excel.style.ExcelStyle; -import org.junit.*; - -import com.genexus.msoffice.excel.poi.xssf.ExcelCells; -import com.genexus.msoffice.excel.poi.xssf.ExcelWorksheet; import com.genexus.msoffice.excel.style.ExcelAlignment; +import org.junit.*; import static org.junit.Assert.*; /** * Unit test for simple App. @@ -37,11 +34,11 @@ public void beforeEachTestMethod() { @Test public void testNumberFormat1() { ExcelSpreadsheetGXWrapper excel = create("testNumberFormat1"); - excel.getCells(1, 1, 1, 1).setNumericValue(BigDecimal.valueOf(123.456)); - excel.getCells(2, 1, 1, 1).setNumericValue(BigDecimal.valueOf(1)); - excel.getCells(3, 1, 1, 1).setNumericValue(BigDecimal.valueOf(100)); + excel.getCellsv2(1, 1, 1, 1).setNumericValue(BigDecimal.valueOf(123.456)); + excel.getCellsv2(2, 1, 1, 1).setNumericValue(BigDecimal.valueOf(1)); + excel.getCellsv2(3, 1, 1, 1).setNumericValue(BigDecimal.valueOf(100)); - excel.getCells(4, 1, 1, 1).setNumericValue(BigDecimal.valueOf(0.123)); + excel.getCellsv2(4, 1, 1, 1).setNumericValue(BigDecimal.valueOf(0.123)); excel.save(); } @@ -50,10 +47,10 @@ public void testNumberFormat1() { public void testCellStyle1() { ExcelSpreadsheetGXWrapper excel = create("testCellStyle1"); excel.setColumnWidth(1, 100); - excel.getCells(2, 1, 1, 5).setNumericValue(BigDecimal.valueOf(123.456)); + excel.getCellsv2(2, 1, 1, 5).setNumericValue(BigDecimal.valueOf(123.456)); ExcelStyle newCellStyle = new ExcelStyle(); newCellStyle.getCellFont().setBold(true); - excel.getCells(2, 1, 1, 5).setCellStyle(newCellStyle); + excel.getCellsv2(2, 1, 1, 5).setCellStyle(newCellStyle); boolean ok = excel.save(); Assert.assertTrue(ok); @@ -63,10 +60,10 @@ public void testCellStyle1() { public void testCellStyle2() { ExcelSpreadsheetGXWrapper excel = create("testCellStyle2"); excel.setColumnWidth(1, 100); - excel.getCells(2, 1, 5, 5).setNumericValue(BigDecimal.valueOf(123.456)); + excel.getCellsv2(2, 1, 5, 5).setNumericValue(BigDecimal.valueOf(123.456)); ExcelStyle newCellStyle = new ExcelStyle(); newCellStyle.getCellFont().setBold(true); - excel.getCells(2, 1, 3, 3).setCellStyle(newCellStyle); + excel.getCellsv2(2, 1, 3, 3).setCellStyle(newCellStyle); excel.save(); @@ -125,13 +122,13 @@ public void testInsertDuplicateSheets() { @Test public void testActiveWorksheet() { ExcelSpreadsheetGXWrapper excel = create("testActiveWorksheet"); - excel.getCells(2, 1, 5, 5).setNumericValue(BigDecimal.valueOf(123.456)); + excel.getCellsv2(2, 1, 5, 5).setNumericValue(BigDecimal.valueOf(123.456)); excel.insertSheet("test1"); excel.insertSheet("test2"); excel.insertSheet("test3"); excel.setCurrentWorksheetByName("test2"); - excel.getCells(2, 1, 5, 5).setNumericValue(new java.math.BigDecimal(3)); + excel.getCellsv2(2, 1, 5, 5).setNumericValue(new java.math.BigDecimal(3)); excel.save(); } @@ -140,7 +137,7 @@ public void testActiveWorksheet() { public void testOpenAndSave() { ExcelSpreadsheetGXWrapper excel = create("testActive"); try { - excel.getCells(2, 1, 5, 5).setDate(new Date()); + excel.getCellsv2(2, 1, 5, 5).setDateValue(new Date()); } catch (Exception e) { e.printStackTrace(); } @@ -166,7 +163,7 @@ public void testOpenAndSaveLocked() { Assert.assertEquals("File is locked", 7, excel.getErrCode()); try { - excel.getCells(2, 1, 5, 5).setDate(new Date()); + excel.getCellsv2(2, 1, 5, 5).setDateValue(new Date()); } catch (Exception e) { e.printStackTrace(); } @@ -186,7 +183,7 @@ public void testFolderNotExists() { excel.open(excel1); try { - excel.getCells(2, 1, 5, 5).setDate(new Date()); + excel.getCellsv2(2, 1, 5, 5).setDateValue(new Date()); } catch (Exception e) { e.printStackTrace(); } @@ -207,7 +204,7 @@ public void testWithoutExtensions() { excel.insertSheet("genexus1"); excel.insertSheet("genexus2"); - List wSheets = excel.getWorksheets(); + List wSheets = excel.getWorksheets(); Assert.assertTrue(wSheets.size() == 3); Assert.assertTrue(wSheets.get(0).getName() == "genexus0"); Assert.assertTrue(wSheets.get(1).getName() == "genexus1"); @@ -224,7 +221,7 @@ public void testInsertSheet() { excel.insertSheet("genexus1"); excel.insertSheet("genexus2"); - List wSheets = excel.getWorksheets(); + List wSheets = excel.getWorksheets(); Assert.assertTrue(wSheets.size() == 3); Assert.assertTrue(wSheets.get(0).getName() == "genexus0"); Assert.assertTrue(wSheets.get(1).getName() == "genexus1"); @@ -243,7 +240,7 @@ public void testDeleteSheet() { excel.insertSheet("gx3"); excel.insertSheet("gx4"); - List wSheets = excel.getWorksheets(); + List wSheets = excel.getWorksheets(); Assert.assertTrue(wSheets.size() == 4); Assert.assertTrue(wSheets.get(0).getName() == "gx1"); Assert.assertTrue(wSheets.get(1).getName() == "gx2"); @@ -260,19 +257,19 @@ public void testDeleteSheet() { public void testSetCellValues() { ExcelSpreadsheetGXWrapper excel = create("testSetCellValues"); excel.setAutofit(true); - excel.getCells(1, 1, 1, 1).setNumericValue(new java.math.BigDecimal(100)); - excel.getCells(2, 1, 1, 1).setText("hola!"); - excel.getCells(3, 1, 1, 1).setDateValue(new Date()); - excel.getCells(4, 1, 1, 1).setNumericValue(BigDecimal.valueOf(66.78)); + excel.getCellsv2(1, 1, 1, 1).setNumericValue(new java.math.BigDecimal(100)); + excel.getCellsv2(2, 1, 1, 1).setText("hola!"); + excel.getCellsv2(3, 1, 1, 1).setDateValue(new Date()); + excel.getCellsv2(4, 1, 1, 1).setNumericValue(BigDecimal.valueOf(66.78)); excel.save(); excel.close(); // Verify previous Excel Document excel = open("testSetCellValues"); - assertEquals(100, excel.getCells(1, 1, 1, 1).getNumericValue().intValue()); + assertEquals(100, excel.getCellsv2(1, 1, 1, 1).getNumericValue().intValue()); - assertEquals("No Coindicen", excel.getCells(2, 1, 1, 1).getText(), "hola!"); + assertEquals("No Coindicen", excel.getCellsv2(2, 1, 1, 1).getText(), "hola!"); excel.save(); } @@ -297,7 +294,7 @@ public void testFormulas() { @Test public void testExcelCellRange() { ExcelSpreadsheetGXWrapper excel = create("testExcelCellRange"); - IExcelCellRange cellRange = excel.getCells(2, 2, 5, 10); + IExcelCellRange cellRange = excel.getCellsv2(2, 2, 5, 10); assertEquals(2, cellRange.getColumnStart(), 0); assertEquals(11, cellRange.getColumnEnd(), 0); @@ -381,7 +378,7 @@ public void testCopySheet() { excel.insertSheet("hoja1"); excel.setCurrentWorksheetByName("hoja1"); - excel.getCells(1, 1, 3, 3).setText("test"); + excel.getCellsv2(1, 1, 3, 3).setText("test"); excel.insertSheet("hoja2"); excel.insertSheet("hoja3"); excel.save(); @@ -393,7 +390,7 @@ public void testCopySheet() { excel.close(); excel = open("testCopySheet"); excel.setCurrentWorksheetByName("hoja1Copia"); - assertEquals("No Coindicen", excel.getCells(1, 1, 3, 3).getText(), "test"); + assertEquals("No Coindicen", excel.getCellsv2(1, 1, 3, 3).getText(), "test"); excel.close(); } @@ -403,7 +400,7 @@ public void testCopySheet2() { excel.insertSheet("hoja1"); excel.setCurrentWorksheetByName("hoja1"); - excel.getCells(1, 1, 3, 3).setText("test"); + excel.getCellsv2(1, 1, 3, 3).setText("test"); excel.insertSheet("hoja2"); excel.insertSheet("hoja3"); excel.save(); @@ -417,7 +414,7 @@ public void testCopySheet2() { excel.close(); excel = open("testCopySheet"); excel.setCurrentWorksheetByName("hoja1Copia"); - assertEquals("No Coindicen", excel.getCells(1, 1, 3, 3).getText(), "test"); + assertEquals("No Coindicen", excel.getCellsv2(1, 1, 3, 3).getText(), "test"); excel.close(); } @@ -432,7 +429,7 @@ public void testGetWorksheets() { excel.save(); excel.close(); excel = open("testGetWorksheets"); - List sheets = excel.getWorksheets(); + List sheets = excel.getWorksheets(); assertEquals("hoja1", sheets.get(0).getName()); assertEquals("hoja2", sheets.get(1).getName()); assertEquals("hoja3", sheets.get(2).getName()); @@ -448,13 +445,13 @@ public void testHiddenCells() { excel.insertSheet("hoja1"); excel.setCurrentWorksheetByName("hoja1"); excel.getCurrentWorksheet().setProtected("password"); - excel.getCells(1, 1, 3, 3).setText("texto no se puede editar"); + excel.getCellsv2(1, 1, 3, 3).setText("texto no se puede editar"); ExcelStyle style = new ExcelStyle(); style.setHidden(true); - excel.getCells(1, 1, 3, 3).setCellStyle(style); + excel.getCellsv2(1, 1, 3, 3).setCellStyle(style); - ExcelCells cells = excel.getCells(5, 1, 3, 3); + ExcelCellsGXWrapper cells = excel.getCellsv2(5, 1, 3, 3); cells.setText("texto SI se puede editar"); style = new ExcelStyle(); style.setLocked(false); @@ -470,13 +467,13 @@ public void testProtectSheet() { excel.insertSheet("hoja1"); excel.setCurrentWorksheetByName("hoja1"); excel.getCurrentWorksheet().setProtected("password"); - excel.getCells(1, 1, 3, 3).setText("texto no se puede editar"); + excel.getCellsv2(1, 1, 3, 3).setText("texto no se puede editar"); ExcelStyle style = new ExcelStyle(); style.setLocked(true); - excel.getCells(1, 1, 3, 3).setCellStyle(style); + excel.getCellsv2(1, 1, 3, 3).setCellStyle(style); - ExcelCells cells = excel.getCells(5, 1, 3, 3); + ExcelCellsGXWrapper cells = excel.getCellsv2(5, 1, 3, 3); cells.setText("texto SI se puede editar"); style = new ExcelStyle(); style.setLocked(false); @@ -542,7 +539,7 @@ public void testCloneSheet() { excel.save(); excel.close(); excel = open("testCloneSheet"); - List sheets = excel.getWorksheets(); + List sheets = excel.getWorksheets(); Assert.assertEquals(4, sheets.size()); excel.close(); } @@ -556,7 +553,7 @@ public void testCloneSheet2() { excel.save(); excel.close(); excel = open("testCloneSheet2"); - List sheets = excel.getWorksheets(); + List sheets = excel.getWorksheets(); Assert.assertEquals(2, sheets.size()); excel.close(); } @@ -579,7 +576,7 @@ public void testCloneSheetError() { excel.save(); excel.close(); excel = open("testCloneSheetError"); - List sheets = excel.getWorksheets(); + List sheets = excel.getWorksheets(); Assert.assertEquals(4, sheets.size()); excel.close(); } @@ -600,7 +597,7 @@ public void testWorksheetRename() { excel.save(); excel.close(); excel = open("testWorksheetRename"); - List sheets = excel.getWorksheets(); + List sheets = excel.getWorksheets(); assertEquals("hoja1", sheets.get(1).getName()); assertEquals("hoja2", sheets.get(2).getName()); assertEquals("modificada", sheets.get(3).getName()); @@ -611,8 +608,8 @@ public void testWorksheetRename() { @Test public void testMergeCells() { ExcelSpreadsheetGXWrapper excel = create("testMergeCells"); - excel.getCells(2, 10, 10, 5).mergeCells(); - excel.getCells(2, 10, 10, 5).setText("merged cells"); + excel.getCellsv2(2, 10, 10, 5).mergeCells(); + excel.getCellsv2(2, 10, 10, 5).setText("merged cells"); excel.save(); excel.close(); } @@ -620,14 +617,14 @@ public void testMergeCells() { @Test public void testMergeMultipleCells() { ExcelSpreadsheetGXWrapper excel = create("testMergeCells-2"); - excel.getCells(1, 1, 2, 5).mergeCells(); - excel.getCells(1, 1, 2, 5).setText("merged cells 1"); + excel.getCellsv2(1, 1, 2, 5).mergeCells(); + excel.getCellsv2(1, 1, 2, 5).setText("merged cells 1"); - excel.getCells(5, 1, 2, 5).mergeCells(); - excel.getCells(5, 1, 2, 5).setText("merged cells 2"); + excel.getCellsv2(5, 1, 2, 5).mergeCells(); + excel.getCellsv2(5, 1, 2, 5).setText("merged cells 2"); - excel.getCells(8, 1, 2, 5).mergeCells(); - excel.getCells(8, 1, 2, 5).setText("merged cells 3"); + excel.getCellsv2(8, 1, 2, 5).mergeCells(); + excel.getCellsv2(8, 1, 2, 5).setText("merged cells 3"); excel.save(); excel.close(); @@ -636,11 +633,11 @@ public void testMergeMultipleCells() { @Test public void testMergeMultipleCellsIntersect() { ExcelSpreadsheetGXWrapper excel = create("testMergeCells-3"); - excel.getCells(1, 1, 8, 5).mergeCells(); - excel.getCells(1, 1, 8, 5).setText("merged cells 1"); + excel.getCellsv2(1, 1, 8, 5).mergeCells(); + excel.getCellsv2(1, 1, 8, 5).setText("merged cells 1"); - excel.getCells(5, 1, 8, 5).mergeCells(); - excel.getCells(5, 1, 8, 5).setText("merged cells 2"); + excel.getCellsv2(5, 1, 8, 5).mergeCells(); + excel.getCellsv2(5, 1, 8, 5).setText("merged cells 2"); excel.save(); excel.close(); @@ -650,9 +647,9 @@ public void testMergeMultipleCellsIntersect() { @Test public void testMergeNestedCells() { ExcelSpreadsheetGXWrapper excel = create("testMergeNestedCells"); - excel.getCells(5, 5, 4, 4).mergeCells(); - excel.getCells(5, 5, 4, 4).setText("merged cells"); - excel.getCells(1, 1, 10, 10).mergeCells(); + excel.getCellsv2(5, 5, 4, 4).mergeCells(); + excel.getCellsv2(5, 5, 4, 4).setText("merged cells"); + excel.getCellsv2(1, 1, 10, 10).mergeCells(); excel.save(); excel.close(); } @@ -660,13 +657,13 @@ public void testMergeNestedCells() { @Test public void testMergeCellsError() { ExcelSpreadsheetGXWrapper excel = create("testMergeCellsError"); - excel.getCells(2, 10, 10, 5).mergeCells(); - excel.getCells(2, 10, 10, 5).mergeCells(); - excel.getCells(2, 10, 10, 5).mergeCells(); - excel.getCells(3, 11, 2, 2).mergeCells(); - excel.getCells(2, 10, 10, 5).mergeCells(); + excel.getCellsv2(2, 10, 10, 5).mergeCells(); + excel.getCellsv2(2, 10, 10, 5).mergeCells(); + excel.getCellsv2(2, 10, 10, 5).mergeCells(); + excel.getCellsv2(3, 11, 2, 2).mergeCells(); + excel.getCellsv2(2, 10, 10, 5).mergeCells(); - excel.getCells(2, 10, 10, 5).setText("merged cells"); + excel.getCellsv2(2, 10, 10, 5).setText("merged cells"); excel.save(); excel.close(); } @@ -674,7 +671,7 @@ public void testMergeCellsError() { @Test public void testColumnAndRowHeight() { ExcelSpreadsheetGXWrapper excel = create("testColumnAndRowHeight"); - excel.getCells(1, 1, 5, 5).setText("texto de las celdas largo"); + excel.getCellsv2(1, 1, 5, 5).setText("texto de las celdas largo"); excel.setRowHeight(2, 50); excel.setColumnWidth(1, 100); excel.save(); @@ -684,11 +681,11 @@ public void testColumnAndRowHeight() { @Test public void testAlignment() { ExcelSpreadsheetGXWrapper excel = create("testAlignment"); - excel.getCells(2, 2, 3, 3).setText("a"); + excel.getCellsv2(2, 2, 3, 3).setText("a"); ExcelStyle style = new ExcelStyle(); style.getCellAlignment().setHorizontalAlignment(ExcelAlignment.HORIZONTAL_ALIGN_RIGHT); //center style.getCellAlignment().setVerticalAlignment(ExcelAlignment.VERTICAL_ALIGN_MIDDLE); //middle - excel.getCells(2, 2, 3, 3).setCellStyle(style); + excel.getCellsv2(2, 2, 3, 3).setCellStyle(style); excel.save(); excel.close(); @@ -698,7 +695,7 @@ public void testAlignment() { public void testExcelCellStyle() { ExcelSpreadsheetGXWrapper excel = create("testExcelCellStyle"); - IExcelCellRange cells = excel.getCells(1, 1, 2, 2); + IExcelCellRange cells = excel.getCellsv2(1, 1, 2, 2); ExcelStyle style = new ExcelStyle(); @@ -717,7 +714,7 @@ public void testExcelCellStyle() { excel.setRowHeight(1, 45); excel.setRowHeight(2, 45); - cells = excel.getCells(5, 2, 4, 4); + cells = excel.getCellsv2(5, 2, 4, 4); cells.setText("texto2"); style = new ExcelStyle(); @@ -729,7 +726,7 @@ public void testExcelCellStyle() { cells.setCellStyle(style); - cells = excel.getCells(10, 2, 2, 2); + cells = excel.getCellsv2(10, 2, 2, 2); cells.setText("texto3"); style = new ExcelStyle(); style.getCellFont().setBold(false); @@ -749,7 +746,7 @@ public void testExcelCellStyle() { @Test public void testExcelBorderStyle() { ExcelSpreadsheetGXWrapper excel = create("testExcelBorderStyle"); - IExcelCellRange cells = excel.getCells(5, 2, 4, 4); + IExcelCellRange cells = excel.getCellsv2(5, 2, 4, 4); cells.setText("texto2"); ExcelStyle style = new ExcelStyle(); @@ -766,7 +763,7 @@ public void testExcelBorderStyle() { cells.setCellStyle(style); - cells = excel.getCells(10, 2, 2, 2); + cells = excel.getCellsv2(10, 2, 2, 2); cells.setText("texto3"); style = new ExcelStyle(); @@ -821,10 +818,10 @@ public void testInsertRow() { public void testDeleteRow() { ExcelSpreadsheetGXWrapper excel = create("testDeleteRow"); - excel.getCells(1, 1, 1, 5).setNumericValue(new java.math.BigDecimal(1)); - excel.getCells(2, 1, 1, 5).setNumericValue(new java.math.BigDecimal(2)); - excel.getCells(3, 1, 1, 5).setNumericValue(new java.math.BigDecimal(3)); - excel.getCells(4, 1, 1, 5).setNumericValue(new java.math.BigDecimal(4)); + excel.getCellsv2(1, 1, 1, 5).setNumericValue(new java.math.BigDecimal(1)); + excel.getCellsv2(2, 1, 1, 5).setNumericValue(new java.math.BigDecimal(2)); + excel.getCellsv2(3, 1, 1, 5).setNumericValue(new java.math.BigDecimal(3)); + excel.getCellsv2(4, 1, 1, 5).setNumericValue(new java.math.BigDecimal(4)); excel.save(); excel.close(); // Verify previous Excel Document @@ -994,7 +991,7 @@ public void testDeleteColumn2() { @Test public void testDeleteColumn3() { ExcelSpreadsheetGXWrapper excel = create("testDeleteColumn3"); - excel.getCells(1,1,5,5).setText("cell"); + excel.getCellsv2(1,1,5,5).setText("cell"); excel.insertRow(3, 5); excel.deleteRow(3); excel.deleteColumn(2); @@ -1010,7 +1007,7 @@ public void testDeleteColumn3() { @Test public void testSaveAs() { ExcelSpreadsheetGXWrapper excel = create("testSaveAs"); - excel.getCells(1, 1, 15, 15).setNumericValue(new BigDecimal(100)); + excel.getCellsv2(1, 1, 15, 15).setNumericValue(new BigDecimal(100)); String excelNew = basePath + "testSaveAsCopy.xlsx"; excel.saveAs(excelNew); excel.close(); @@ -1022,11 +1019,11 @@ public void testSaveAs() { public void testAutoFit() { ExcelSpreadsheetGXWrapper excel = create("testAutoFit"); excel.setAutofit(true); - excel.getCells(1, 2, 1, 1).setText("LONGTEXTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT"); - excel.getCells(1, 3, 1, 1).setText("VERYLONGTEXTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT"); - excel.getCells(2, 4, 1, 1).setText("hola!"); - excel.getCells(6, 6, 1, 1).setText("VERYLONGTEXTINDIFFERENTROWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW"); - ExcelCells cells = excel.getCells(7, 7, 1, 1); + excel.getCellsv2(1, 2, 1, 1).setText("LONGTEXTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT"); + excel.getCellsv2(1, 3, 1, 1).setText("VERYLONGTEXTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT"); + excel.getCellsv2(2, 4, 1, 1).setText("hola!"); + excel.getCellsv2(6, 6, 1, 1).setText("VERYLONGTEXTINDIFFERENTROWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW"); + ExcelCellsGXWrapper cells = excel.getCellsv2(7, 7, 1, 1); ExcelStyle style = new ExcelStyle(); style.setDataFormat("#.##"); //change style, so it shows the full number not scientific notation cells.setNumericValue(new BigDecimal("123456789123456789123456789")); @@ -1041,16 +1038,16 @@ public void testDateFormat() { excel.setAutofit(true); Date date = new Date(); //sets date with default format - ExcelCells cells = excel.getCells(1, 1, 1, 1); + ExcelCellsGXWrapper cells = excel.getCellsv2(1, 1, 1, 1); cells.setDateValue(date); //sets date and apply format after - cells = excel.getCells(2, 1, 1, 1); + cells = excel.getCellsv2(2, 1, 1, 1); ExcelStyle style = new ExcelStyle(); cells.setDateValue(date); style.setDataFormat("YYYY/MM/DD hh:mm:ss"); cells.setCellStyle(style); //sets date and apply format before - cells = excel.getCells(3, 1, 1, 1); + cells = excel.getCellsv2(3, 1, 1, 1); style = new ExcelStyle(); style.setDataFormat("YYYY/MM/DD hh:mm:ss"); cells.setCellStyle(style); @@ -1060,16 +1057,16 @@ public void testDateFormat() { date.setMinutes(0); date.setSeconds(0); //sets date with default format without hours - cells = excel.getCells(4, 1, 1, 1); + cells = excel.getCellsv2(4, 1, 1, 1); cells.setDateValue(date); //sets date and apply format after - cells = excel.getCells(5, 1, 1, 1); + cells = excel.getCellsv2(5, 1, 1, 1); style = new ExcelStyle(); cells.setDateValue(date); style.setDataFormat("YYYY/MM/DD hh:mm:ss"); cells.setCellStyle(style); //sets date and apply format before - cells = excel.getCells(6, 1, 1, 1); + cells = excel.getCellsv2(6, 1, 1, 1); style = new ExcelStyle(); style.setDataFormat("YYYY/MM/DD hh:mm:ss"); cells.setCellStyle(style);