Skip to content

Commit f55d952

Browse files
committed
[KNOWAGE-9263] improve decimal formatting and alignment in Excel summary rows
1 parent c722e7c commit f55d952

1 file changed

Lines changed: 26 additions & 5 deletions

File tree

  • knowage-excel-export/src/main/java/it/eng/knowage/engine/api/excel/export/oldcockpit

knowage-excel-export/src/main/java/it/eng/knowage/engine/api/excel/export/oldcockpit/ExcelExporter.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import java.io.IOException;
4848
import java.text.DateFormat;
4949
import java.text.DecimalFormat;
50+
import java.text.DecimalFormatSymbols;
5051
import java.text.SimpleDateFormat;
5152
import java.util.*;
5253
import java.util.regex.Matcher;
@@ -1037,14 +1038,15 @@ private boolean isSummaryRow(int r, JSONArray rows, int numberOfSummaryRows) {
10371038

10381039
private static void setSummaryRowValue(JSONObject[] columnStyles, int c, Object value, Cell cell, String summaryRowLabel, JSONObject column, boolean isOnlyPinned) {
10391040
try {
1041+
CellStyle summaryStyle = cell.getSheet().getWorkbook().createCellStyle();
1042+
summaryStyle.setAlignment(HorizontalAlignment.RIGHT);
1043+
10401044
int precision = (columnStyles[c] != null && columnStyles[c].has("precision")) ? columnStyles[c].getInt("precision") : 2;
10411045
String formattedValue;
10421046
try {
1043-
if (precision == 0) {
1044-
formattedValue = new DecimalFormat("#,##0").format(value);
1045-
} else {
1046-
formattedValue = new DecimalFormat("#,##0." + StringUtils.repeat("0", precision)).format(value);
1047-
}
1047+
DecimalFormat formatter = getDecimalFormat(precision);
1048+
formattedValue = formatter.format(value);
1049+
10481050
} catch (Exception e) {
10491051
formattedValue = value.toString();
10501052
}
@@ -1053,12 +1055,14 @@ private static void setSummaryRowValue(JSONObject[] columnStyles, int c, Object
10531055
if (column.has("pinned") || value.equals("")) {
10541056
cell.setCellValue(formattedValue);
10551057
} else {
1058+
cell.setCellStyle(summaryStyle);
10561059
cell.setCellValue(valueWithLabel);
10571060
}
10581061
} else {
10591062
if (column.has("pinned")) {
10601063
cell.setCellValue(valueWithLabel);
10611064
} else {
1065+
cell.setCellStyle(summaryStyle);
10621066
cell.setCellValue(formattedValue);
10631067
}
10641068
}
@@ -1068,6 +1072,23 @@ private static void setSummaryRowValue(JSONObject[] columnStyles, int c, Object
10681072
}
10691073
}
10701074

1075+
private static DecimalFormat getDecimalFormat(int precision) {
1076+
DecimalFormat formatter;
1077+
// Create custom DecimalFormatSymbols with dot as grouping separator and comma as decimal separator
1078+
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
1079+
symbols.setGroupingSeparator('.');
1080+
symbols.setDecimalSeparator(',');
1081+
1082+
if (precision == 0) {
1083+
// Pattern should use standard # symbols, output will use custom separators
1084+
formatter = new DecimalFormat("#,##0", symbols);
1085+
} else {
1086+
// Pattern should use standard # symbols, output will use custom separators
1087+
formatter = new DecimalFormat("#,##0." + StringUtils.repeat("0", precision), symbols);
1088+
}
1089+
return formatter;
1090+
}
1091+
10711092
private int doCockpitSummaryRowsLogic(JSONObject widgetData, int numberOfSummaryRows, List<String> summaryRowsLabels) throws JSONException {
10721093
if (cockpitSummaryRowsEnabled(widgetData)) {
10731094
JSONArray list = widgetData.getJSONObject("settings").getJSONObject("summary").getJSONArray("list");

0 commit comments

Comments
 (0)