[EDIT] CAMBIOS PARA EL EXCEL

parent 85adfa2d
......@@ -28,33 +28,33 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-logging</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-logging</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>3.2.0</version>
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-logging</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions> -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
......@@ -190,11 +190,11 @@
<artifactId>log4j-to-slf4j</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.9.0</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.kafka</groupId>-->
<!-- <artifactId>spring-kafka</artifactId>-->
<!-- <version>2.9.0</version>-->
<!-- </dependency>-->
</dependencies>
<build>
<finalName>trismegisto-services</finalName>
......
......@@ -31,6 +31,9 @@ public class ExcelApi {
int size = json.getBytes().length;
System.out.println("Size: " + size);
byte[] excelByte = excelService.generateExcel(jsonBody, size);
if(excelByte == null) {
return ResponseEntity.internalServerError().body("Error al generar el archivo");
}
if (jsonBody.optBoolean("base64", false)) {
JSONObject response = new JSONObject();
response.put("file_name", jsonBody.optString("file_name", "no_name.xlsx"));
......
package web.multitask.trismegistoservices.consumer;
import org.json.JSONObject;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
import java.util.logging.Logger;
@Component
public class KafkaConsumer {
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")
void listener(@Payload String data,
@Header(KafkaHeaders.RECEIVED_PARTITION) int partition,
@Header(KafkaHeaders.OFFSET) int offset) {
JSONObject json = new JSONObject(data);
System.out.println(json);
}
}
\ No newline at end of file
//package web.multitask.trismegistoservices.consumer;
//
//import org.json.JSONObject;
//import org.springframework.kafka.annotation.KafkaListener;
//import org.springframework.kafka.support.KafkaHeaders;
//import org.springframework.messaging.handler.annotation.Header;
//import org.springframework.messaging.handler.annotation.Payload;
//import org.springframework.stereotype.Component;
//
//import java.util.logging.Logger;
//
//@Component
//public class KafkaConsumer {
//
// 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")
// void listener(@Payload String data,
// @Header(KafkaHeaders.RECEIVED_PARTITION) int partition,
// @Header(KafkaHeaders.OFFSET) int offset) {
// JSONObject json = new JSONObject(data);
// System.out.println(json);
// }
//}
\ No newline at end of file
......@@ -12,6 +12,7 @@ import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.IntStream;
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.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
......@@ -54,8 +55,10 @@ public class ExcelService {
if (json.optJSONArray("sheetArray", new JSONArray()).isEmpty()) {
XSSFSheet sheet = workbook.createSheet(json.optString("sheet_name", "no_name"));
buildSheet(workbook, sheet, json);
for (int i = 0; i < json.getJSONArray("identifiers").length(); i++) {
sheet.setColumnWidth(i, 16 * 512);
JSONArray identifiers = json.getJSONArray("identifiers");
for (int i = 0; i < identifiers.length(); i++) {
// sheet.setColumnWidth(i, 16 * 512);
findLargerTextByIdentifer(sheet,json.getJSONArray("data"),identifiers);
}
} else {
JSONArray sheetArray = json.optJSONArray("sheetArray", new JSONArray());
......@@ -65,7 +68,8 @@ public class ExcelService {
XSSFSheet sheet = workbook.createSheet(jsonObject.optString("sheet_name", "no_name"));
JSONArray identifiers = jsonObject.getJSONArray("identifiers");
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);
}
......@@ -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) {
final XSSFCellStyle defaultStyle = createCellStyle(workbook, new JSONObject());
......@@ -101,49 +123,86 @@ public class ExcelService {
JSONArray styles = json.optJSONArray("styles", new JSONArray());
fillStylesArray(styles,workbook,stylesSaved,styleObjectsSaved);
int i = 0;
for (int i = 0; i < data.length(); i++) {
if(json.has("title")){
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++) {
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());
String value = row_data.optString(identifiers.getString(j),row_data.optString(String.valueOf(j), ""));
if(style.isEmpty()){
cell.setCellStyle(defaultStyle);
cell.setCellValue(value);
}else{
if(!stylesSaved.contains(style.toString())){
stylesSaved.add(style.toString());
cellStyle = createCellStyle(workbook,style);
cell.setCellStyle(cellStyle);
styleObjectsSaved.add(cellStyle);
cell.setCellValue(value);
}else{
cell.setCellStyle(styleObjectsSaved.get(stylesSaved.indexOf(style.toString())));
cell.setCellValue(value);
}
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){
case "number":
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;
case "date":
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(cell.getDateCellValue());
cell.setCellValue(value);
break;
case "formula":
cell.setCellType(CellType.FORMULA);
cell.setCellFormula(value);
break;
default:
cell.setCellType(CellType.STRING);
cell.setCellValue(value);
break;
}
}
......@@ -193,7 +252,7 @@ public class ExcelService {
private XSSFCellStyle createCellStyle(XSSFWorkbook workbook, JSONObject styleJson) {
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 border = styleJson.optBoolean("border", true);
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