[EDIT] MEJORAS DE RENDIMIENTO EN EL SERVICIO DE EXCEL

parent 6092edef
......@@ -2,6 +2,10 @@ package web.multitask.trismegistoservices;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@SpringBootApplication
public class AppApplication {
......@@ -10,4 +14,15 @@ public class AppApplication {
SpringApplication.run(AppApplication.class, args);
}
// @Bean("threadPoolTaskExecutor")
// TaskExecutor asyncExecutor() {
// ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// executor.setCorePoolSize(10);
// executor.setMaxPoolSize(1000);
// executor.setQueueCapacity(500);
// executor.setThreadNamePrefix("AsyncThread-");
// executor.initialize();
// return executor;
// }
}
\ No newline at end of file
package web.multitask.trismegistoservices.services;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
......@@ -15,7 +16,6 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;
import org.json.JSONArray;
import org.json.JSONObject;
import java.awt.Color;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
......@@ -53,35 +53,59 @@ public class ExcelService {
private void buildSheet(XSSFWorkbook workbook, XSSFSheet sheet, JSONObject json) {
final XSSFCellStyle defaultStyle = createCellStyle(workbook, new JSONObject());
JSONArray identifiers = json.optJSONArray("identifiers", new JSONArray());
JSONArray data = json.optJSONArray("data", new JSONArray());
JSONArray styles = json.optJSONArray("styles", new JSONArray());
for (int i = 0; i < data.length(); i++) {
XSSFRow row = sheet.createRow(i);
JSONObject row_data = data.optJSONObject(i);
for (int j = 0; j < identifiers.length(); j++) {
sheet.autoSizeColumn(j);
String value = row_data.optString(identifiers.getString(j), "");
XSSFCell cell = row.createCell(j);
JSONArray styleArray = styles.optJSONArray(i, new JSONArray());
JSONObject style = styleArray.optJSONObject(j, new JSONObject());
XSSFCellStyle cellStyle = createCellStyle(workbook,
getXSSFColor(style.optString("background", "#ffffff")),
getXSSFColor(style.optString("foreground", "#333333")),
style.optBoolean("bold", false),
style.optBoolean("border", true));
cell.setCellStyle(cellStyle);
cell.setCellValue(value);
if(style.isEmpty()){
cell.setCellStyle(defaultStyle);
}else{
XSSFCellStyle cellStyle = createCellStyle(workbook,style);
cell.setCellStyle(cellStyle);
}
if(row_data.get(identifiers.optString(j, "")) instanceof String){
cell.setCellValue(row_data.optString(identifiers.optString(j, ""), ""));
cell.setCellType(org.apache.poi.ss.usermodel.CellType.STRING);
}else if(row_data.get(identifiers.optString(j, "")) instanceof Integer){
cell.setCellValue(row_data.optInt(identifiers.optString(j, ""), 0));
cell.setCellType(org.apache.poi.ss.usermodel.CellType.NUMERIC);
}else if(row_data.get(identifiers.optString(j, "")) instanceof Double){
cell.setCellValue(row_data.optDouble(identifiers.optString(j, ""), 0.0));
cell.setCellType(org.apache.poi.ss.usermodel.CellType.NUMERIC);
}else if(row_data.get(identifiers.optString(j, "")) instanceof Boolean){
cell.setCellValue(row_data.optBoolean(identifiers.optString(j, ""), false));
cell.setCellType(org.apache.poi.ss.usermodel.CellType.BOOLEAN);
}else{
cell.setCellValue(row_data.optString(identifiers.optString(j, ""), ""));
cell.setCellType(org.apache.poi.ss.usermodel.CellType.STRING);
}
}
}
for (int i = 0; i < identifiers.length(); i++) {
sheet.autoSizeColumn(i, true);
}
}
private XSSFCellStyle createCellStyle(XSSFWorkbook workbook, XSSFColor background, XSSFColor foreground,
boolean bold, boolean border) {
private XSSFCellStyle createCellStyle(XSSFWorkbook workbook, JSONObject styleJson) {
XSSFColor background = getXSSFColor(styleJson.optString("background", "#ffffff"));
XSSFColor foreground = getXSSFColor(styleJson.optString("foreground", "#333333"));
boolean bold = styleJson.optBoolean("bold", false);
boolean border = styleJson.optBoolean("border", true);
String align = styleJson.optString("textAlign", "center");
XSSFCellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
style.setAlignment(HorizontalAlignment.CENTER);
style.setAlignment( align.equals("center") ? HorizontalAlignment.CENTER : align.equals("left") ? HorizontalAlignment.LEFT : HorizontalAlignment.RIGHT);
style.setWrapText(true);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
......@@ -95,9 +119,17 @@ public class ExcelService {
style.setBorderColor(BorderSide.TOP, getXSSFColor("#333333"));
style.setBorderColor(BorderSide.LEFT, getXSSFColor("#333333"));
style.setBorderColor(BorderSide.RIGHT, getXSSFColor("#333333"));
}else{
style.setBorderBottom(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderColor(BorderSide.BOTTOM, getXSSFColor("#aaaaaa"));
style.setBorderColor(BorderSide.TOP, getXSSFColor("#aaaaaa"));
style.setBorderColor(BorderSide.LEFT, getXSSFColor("#aaaaaa"));
style.setBorderColor(BorderSide.RIGHT, getXSSFColor("#aaaaaa"));
}
// font.setFontHeightInPoints((short) 15);
font.setBold(bold);
style.setFont(font);
......
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