[EDIT] CAMBIOS PARA EL EXCEL

parent 85adfa2d
...@@ -28,33 +28,33 @@ ...@@ -28,33 +28,33 @@
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId> <artifactId>spring-boot-starter-jdbc</artifactId>
<!-- <exclusions>--> <exclusions>
<!-- <exclusion>--> <exclusion>
<!-- <groupId>org.springframework.boot</groupId>--> <groupId>org.springframework.boot</groupId>
<!-- <artifactId>spring-boot-starter-logging</artifactId>--> <artifactId>spring-boot-starter-logging</artifactId>
<!-- </exclusion>--> </exclusion>
<!-- </exclusions>--> </exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
<!-- <exclusions>--> <exclusions>
<!-- <exclusion>--> <exclusion>
<!-- <groupId>org.springframework.boot</groupId>--> <groupId>org.springframework.boot</groupId>
<!-- <artifactId>spring-boot-starter-logging</artifactId>--> <artifactId>spring-boot-starter-logging</artifactId>
<!-- </exclusion>--> </exclusion>
<!-- </exclusions>--> </exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId> <artifactId>spring-boot-starter-security</artifactId>
<version>3.2.0</version> <version>3.2.0</version>
<!-- <exclusions>--> <exclusions>
<!-- <exclusion>--> <exclusion>
<!-- <groupId>org.springframework.boot</groupId>--> <groupId>org.springframework.boot</groupId>
<!-- <artifactId>spring-boot-starter-logging</artifactId>--> <artifactId>spring-boot-starter-logging</artifactId>
<!-- </exclusion>--> </exclusion>
<!-- </exclusions> --> </exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
...@@ -190,11 +190,11 @@ ...@@ -190,11 +190,11 @@
<artifactId>log4j-to-slf4j</artifactId> <artifactId>log4j-to-slf4j</artifactId>
<version>2.20.0</version> <version>2.20.0</version>
</dependency> </dependency>
<dependency> <!-- <dependency>-->
<groupId>org.springframework.kafka</groupId> <!-- <groupId>org.springframework.kafka</groupId>-->
<artifactId>spring-kafka</artifactId> <!-- <artifactId>spring-kafka</artifactId>-->
<version>2.9.0</version> <!-- <version>2.9.0</version>-->
</dependency> <!-- </dependency>-->
</dependencies> </dependencies>
<build> <build>
<finalName>trismegisto-services</finalName> <finalName>trismegisto-services</finalName>
......
...@@ -31,6 +31,9 @@ public class ExcelApi { ...@@ -31,6 +31,9 @@ public class ExcelApi {
int size = json.getBytes().length; int size = json.getBytes().length;
System.out.println("Size: " + size); System.out.println("Size: " + size);
byte[] excelByte = excelService.generateExcel(jsonBody, size); byte[] excelByte = excelService.generateExcel(jsonBody, size);
if(excelByte == null) {
return ResponseEntity.internalServerError().body("Error al generar el archivo");
}
if (jsonBody.optBoolean("base64", false)) { if (jsonBody.optBoolean("base64", false)) {
JSONObject response = new JSONObject(); JSONObject response = new JSONObject();
response.put("file_name", jsonBody.optString("file_name", "no_name.xlsx")); response.put("file_name", jsonBody.optString("file_name", "no_name.xlsx"));
......
package web.multitask.trismegistoservices.consumer; //package web.multitask.trismegistoservices.consumer;
//
import org.json.JSONObject; //import org.json.JSONObject;
import org.springframework.kafka.annotation.KafkaListener; //import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.KafkaHeaders; //import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.handler.annotation.Header; //import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload; //import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component; //import org.springframework.stereotype.Component;
//
import java.util.logging.Logger; //import java.util.logging.Logger;
//
@Component //@Component
public class KafkaConsumer { //public class KafkaConsumer {
//
Logger logger = Logger.getLogger(KafkaConsumer.class.getName()); // Logger logger = Logger.getLogger(KafkaConsumer.class.getName());
//
@KafkaListener(topics = {"SQLSERVER.EJBPLANILLA","schemahistory.topic","SQLSERVER.EJBPLANILLA.EJBPLANILLA.dbo.CONCODIANE1","SQLSERVER.EJBPLANILLA.EJBPLANILLA.dbo.GEN0001DOC2"}, groupId = "1") // @KafkaListener(topics = {"SQLSERVER.EJBPLANILLA","schemahistory.topic","SQLSERVER.EJBPLANILLA.EJBPLANILLA.dbo.CONCODIANE1","SQLSERVER.EJBPLANILLA.EJBPLANILLA.dbo.GEN0001DOC2"}, groupId = "1")
void listener(@Payload String data, // void listener(@Payload String data,
@Header(KafkaHeaders.RECEIVED_PARTITION) int partition, // @Header(KafkaHeaders.RECEIVED_PARTITION) int partition,
@Header(KafkaHeaders.OFFSET) int offset) { // @Header(KafkaHeaders.OFFSET) int offset) {
JSONObject json = new JSONObject(data); // JSONObject json = new JSONObject(data);
System.out.println(json); // System.out.println(json);
} // }
} //}
\ No newline at end of file \ No newline at end of file
...@@ -12,6 +12,7 @@ import java.util.concurrent.atomic.AtomicReference; ...@@ -12,6 +12,7 @@ import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor; import org.apache.poi.xssf.usermodel.XSSFColor;
...@@ -54,8 +55,10 @@ public class ExcelService { ...@@ -54,8 +55,10 @@ public class ExcelService {
if (json.optJSONArray("sheetArray", new JSONArray()).isEmpty()) { if (json.optJSONArray("sheetArray", new JSONArray()).isEmpty()) {
XSSFSheet sheet = workbook.createSheet(json.optString("sheet_name", "no_name")); XSSFSheet sheet = workbook.createSheet(json.optString("sheet_name", "no_name"));
buildSheet(workbook, sheet, json); buildSheet(workbook, sheet, json);
for (int i = 0; i < json.getJSONArray("identifiers").length(); i++) { JSONArray identifiers = json.getJSONArray("identifiers");
sheet.setColumnWidth(i, 16 * 512); for (int i = 0; i < identifiers.length(); i++) {
// sheet.setColumnWidth(i, 16 * 512);
findLargerTextByIdentifer(sheet,json.getJSONArray("data"),identifiers);
} }
} else { } else {
JSONArray sheetArray = json.optJSONArray("sheetArray", new JSONArray()); JSONArray sheetArray = json.optJSONArray("sheetArray", new JSONArray());
...@@ -65,7 +68,8 @@ public class ExcelService { ...@@ -65,7 +68,8 @@ public class ExcelService {
XSSFSheet sheet = workbook.createSheet(jsonObject.optString("sheet_name", "no_name")); XSSFSheet sheet = workbook.createSheet(jsonObject.optString("sheet_name", "no_name"));
JSONArray identifiers = jsonObject.getJSONArray("identifiers"); JSONArray identifiers = jsonObject.getJSONArray("identifiers");
for (int j = 0; j < identifiers.length(); j++) { for (int j = 0; j < identifiers.length(); j++) {
sheet.setColumnWidth(j, 16 * 512); // sheet.setColumnWidth(j, 16 * 512);
findLargerTextByIdentifer(sheet,jsonObject.getJSONArray("data"),identifiers);
} }
buildSheet(workbook, sheet, jsonObject); buildSheet(workbook, sheet, jsonObject);
} }
...@@ -86,6 +90,24 @@ public class ExcelService { ...@@ -86,6 +90,24 @@ public class ExcelService {
} }
} }
private void findLargerTextByIdentifer(XSSFSheet sheet,JSONArray data, JSONArray identifiers){
int[] maxLengthByIdentifier = new int[identifiers.length()];
for (int i = 0; i < data.length(); i++) {
JSONObject row_data = data.optJSONObject(i);
for (int j = 0; j < identifiers.length(); j++) {
int length = row_data.optString(identifiers.optString(j, "")).length();
int length2 = row_data.optString(String.valueOf(j), "").length();
if(length > maxLengthByIdentifier[j] || length2 > maxLengthByIdentifier[j]){
maxLengthByIdentifier[j] = Math.max(length, length2);
}
}
}
for (int i = 0; i < maxLengthByIdentifier.length; i++) {
sheet.setColumnWidth(i, ((maxLengthByIdentifier[i] * 333)));
}
}
public void buildSheet(XSSFWorkbook workbook, XSSFSheet sheet, JSONObject json) { public void buildSheet(XSSFWorkbook workbook, XSSFSheet sheet, JSONObject json) {
final XSSFCellStyle defaultStyle = createCellStyle(workbook, new JSONObject()); final XSSFCellStyle defaultStyle = createCellStyle(workbook, new JSONObject());
...@@ -101,49 +123,86 @@ public class ExcelService { ...@@ -101,49 +123,86 @@ public class ExcelService {
JSONArray styles = json.optJSONArray("styles", new JSONArray()); JSONArray styles = json.optJSONArray("styles", new JSONArray());
fillStylesArray(styles,workbook,stylesSaved,styleObjectsSaved); fillStylesArray(styles,workbook,stylesSaved,styleObjectsSaved);
int i = 0;
for (int i = 0; i < data.length(); i++) { if(json.has("title")){
row = sheet.createRow(i); row = sheet.createRow(i);
JSONObject row_data = data.optJSONObject(i); JSONObject titleJson = json.getJSONObject("title");
JSONObject style = titleJson.optJSONObject("style", new JSONObject());
cell = row.createCell(0);
cell.setCellValue(titleJson.optString("text",""));
if(style.isEmpty()){
cell.setCellStyle(defaultStyle);
}else{
if(!stylesSaved.contains(style.toString())){
stylesSaved.add(style.toString());
cellStyle = createCellStyle(workbook,style);
cell.setCellStyle(cellStyle);
styleObjectsSaved.add(cellStyle);
}else{
cell.setCellStyle(styleObjectsSaved.get(stylesSaved.indexOf(style.toString())));
}
}
sheet.addMergedRegion(new CellRangeAddress(i, i, 0, style.getInt("colspan")-1));
i++;
}
for (; i < data.length(); i++) {
int index = json.has("title") ? i-1 : i;
row = sheet.createRow(i);
JSONObject row_data = data.optJSONObject(index);
for (int j = 0; j < identifiers.length(); j++) { for (int j = 0; j < identifiers.length(); j++) {
cell = row.createCell(j); cell = row.createCell(j);
JSONArray styleArray = styles.optJSONArray(i, new JSONArray()); JSONArray styleArray = styles.optJSONArray(index, new JSONArray());
JSONObject style = styleArray.optJSONObject(j, new JSONObject()); JSONObject style = styleArray.optJSONObject(j, new JSONObject());
String value = row_data.optString(identifiers.getString(j),row_data.optString(String.valueOf(j), ""));
if(style.isEmpty()){ if(style.isEmpty()){
cell.setCellStyle(defaultStyle); cell.setCellStyle(defaultStyle);
cell.setCellValue(value);
}else{ }else{
if(!stylesSaved.contains(style.toString())){ if(!stylesSaved.contains(style.toString())){
stylesSaved.add(style.toString()); stylesSaved.add(style.toString());
cellStyle = createCellStyle(workbook,style); cellStyle = createCellStyle(workbook,style);
cell.setCellStyle(cellStyle); cell.setCellStyle(cellStyle);
styleObjectsSaved.add(cellStyle); styleObjectsSaved.add(cellStyle);
cell.setCellValue(value);
}else{ }else{
cell.setCellStyle(styleObjectsSaved.get(stylesSaved.indexOf(style.toString()))); cell.setCellStyle(styleObjectsSaved.get(stylesSaved.indexOf(style.toString())));
cell.setCellValue(value);
} }
if(style.has("format")){ if(style.has("format")){
setCellFormat(cell,style.optString("format","text")); setCellFormat(cell,style.optString("format","text"),value );
}
if(style.has("colspan")){
sheet.addMergedRegion(new CellRangeAddress(i, i, j, j + style.optInt("colspan", 0)));
} }
} }
cell.setCellValue(row_data.optString(identifiers.optString(j, "")));
} }
} }
} }
public void setCellFormat (XSSFCell cell, String format){ public void setCellFormat (XSSFCell cell, String format, String value){
switch (format){ switch (format){
case "number": case "number":
cell.setCellType(CellType.NUMERIC); cell.setCellType(CellType.NUMERIC);
XSSFCellStyle style = cell.getCellStyle();
DataFormat dataFormat = cell.getSheet().getWorkbook().createDataFormat();
style.setDataFormat(dataFormat.getFormat("0.00"));
cell.setCellStyle(style);
cell.setCellValue(Double.parseDouble(value));
break; break;
case "date": case "date":
cell.setCellType(CellType.NUMERIC); cell.setCellType(CellType.NUMERIC);
cell.setCellValue(cell.getDateCellValue()); cell.setCellValue(value);
break; break;
case "formula": case "formula":
cell.setCellType(CellType.FORMULA); cell.setCellType(CellType.FORMULA);
cell.setCellFormula(value);
break; break;
default: default:
cell.setCellType(CellType.STRING); cell.setCellType(CellType.STRING);
cell.setCellValue(value);
break; break;
} }
} }
...@@ -193,7 +252,7 @@ public class ExcelService { ...@@ -193,7 +252,7 @@ public class ExcelService {
private XSSFCellStyle createCellStyle(XSSFWorkbook workbook, JSONObject styleJson) { private XSSFCellStyle createCellStyle(XSSFWorkbook workbook, JSONObject styleJson) {
XSSFColor background = getXSSFColor(styleJson.optString("background", "#ffffff")); XSSFColor background = getXSSFColor(styleJson.optString("background", "#ffffff"));
XSSFColor foreground = getXSSFColor(styleJson.optString("foreground", "#333333")); XSSFColor foreground = getXSSFColor(styleJson.optString("foreground", styleJson.optString("color", "#333333")));
boolean bold = styleJson.optBoolean("bold", false); boolean bold = styleJson.optBoolean("bold", false);
boolean border = styleJson.optBoolean("border", true); boolean border = styleJson.optBoolean("border", true);
String align = styleJson.optString("textAlign", "center"); String align = styleJson.optString("textAlign", "center");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment