Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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()));
}
Expand Down