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 6472df460..4faaf16e1 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 @@ -145,36 +145,39 @@ public BigDecimal getNumber() throws ExcelException { } - public boolean setDate(Date value) throws ExcelException { - CheckReadonlyDocument(); - try { - if (!CommonUtil.nullDate().equals(value)) { - String dformat = ""; //this.doc.getDateFormat().toLowerCase(); + 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) { + && calendar.get(Calendar.SECOND) == 0 && dformat.indexOf(' ') > 0) { dformat = dformat.substring(0, dformat.indexOf(' ')); } - DataFormat df = pWorkbook.createDataFormat(); - XSSFCellStyle newStyle = stylesCache.getCellStyle(df.getFormat(dformat)); for (int i = 1; i <= cellCount; i++) { XSSFCellStyle cellStyle = pCells[i].getCellStyle(); - copyPropertiesStyle(newStyle, cellStyle); - newStyle.setDataFormat(df.getFormat(dformat)); + if (! DateUtil.isCellDateFormatted(pCells[i])) { + XSSFCellStyle 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); - pCells[i].setCellStyle(newStyle); - fitColumnWidth(i, dformat.length() + 4); } - return true; - } - } catch (Exception e) { - throw new ExcelException(7, "Invalid cell value"); - } - return false; - } + return true; + } + } catch (Exception e) { + throw new ExcelException(7, "Invalid cell value"); + } + return false; + } public Date getDate() throws ExcelException { Date returnValue = null; 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 6cd6cb5ea..b6635a692 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 @@ -233,22 +233,24 @@ private void recalculateFormulas() { } } - 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); - - Row row = sheet.getRow(0); - if (row != null) { - int columnCount = row.getPhysicalNumberOfCells(); - for (int j = 0; j < columnCount; j++) { - sheet.autoSizeColumn(j); - } - } - } - } - } + 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 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 8c4334dbe..220f49b2e 100644 --- a/gxoffice/src/test/java/com/genexus/msoffice/excel/ExcelSpreadsheetTest.java +++ b/gxoffice/src/test/java/com/genexus/msoffice/excel/ExcelSpreadsheetTest.java @@ -890,7 +890,7 @@ public void testHideRow2() { excel.save(); excel.close(); // Verify previous Excel Document - excel = open("testHideRow"); + excel = open("testHideRow2"); assertEquals(1, excel.getCell(1, 1).getNumericValue().intValue()); excel.save(); @@ -909,7 +909,7 @@ public void testHideRow3() { excel.save(); excel.close(); // Verify previous Excel Document - excel = open("testHideRow"); + excel = open("testHideRow3"); assertEquals(1, excel.getCell(1, 1).getNumericValue().intValue()); excel.save(); @@ -1018,6 +1018,67 @@ public void testSaveAs() { } + @Test + 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); + ExcelStyle style = new ExcelStyle(); + style.setDataFormat("#.##"); //change style, so it shows the full number not scientific notation + cells.setNumericValue(new BigDecimal("123456789123456789123456789")); + cells.setCellStyle(style); + excel.save(); + excel.close(); + } + + @Test + public void testDateFormat() { + ExcelSpreadsheetGXWrapper excel = create("testDateFormat"); + excel.setAutofit(true); + Date date = new Date(); + //sets date with default format + ExcelCells cells = excel.getCells(1, 1, 1, 1); + cells.setDateValue(date); + //sets date and apply format after + cells = excel.getCells(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); + style = new ExcelStyle(); + style.setDataFormat("YYYY/MM/DD hh:mm:ss"); + cells.setCellStyle(style); + cells.setDateValue(date); + + date.setHours(0); + date.setMinutes(0); + date.setSeconds(0); + //sets date with default format without hours + cells = excel.getCells(4, 1, 1, 1); + cells.setDateValue(date); + //sets date and apply format after + cells = excel.getCells(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); + style = new ExcelStyle(); + style.setDataFormat("YYYY/MM/DD hh:mm:ss"); + cells.setCellStyle(style); + cells.setDateValue(date); + + excel.save(); + excel.close(); + } + private void logErrorCodes(ExcelSpreadsheetGXWrapper excel) { // System.out.println(String.format("%s - %s", excel.getErrCode(), excel.getErrDescription())); }