[EDIT] MEJORAS DE RENDIMIENTO EN EL SERVICIO DE EXCEL

parent 6092edef
...@@ -2,6 +2,10 @@ package web.multitask.trismegistoservices; ...@@ -2,6 +2,10 @@ package web.multitask.trismegistoservices;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; 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 @SpringBootApplication
public class AppApplication { public class AppApplication {
...@@ -10,4 +14,15 @@ public class AppApplication { ...@@ -10,4 +14,15 @@ public class AppApplication {
SpringApplication.run(AppApplication.class, args); 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; package web.multitask.trismegistoservices.services;
import java.awt.Color;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.FillPatternType;
...@@ -15,7 +16,6 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; ...@@ -15,7 +16,6 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide; import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.awt.Color;
import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -53,35 +53,59 @@ public class ExcelService { ...@@ -53,35 +53,59 @@ public class ExcelService {
private void buildSheet(XSSFWorkbook workbook, XSSFSheet sheet, JSONObject json) { private void buildSheet(XSSFWorkbook workbook, XSSFSheet sheet, JSONObject json) {
final XSSFCellStyle defaultStyle = createCellStyle(workbook, new JSONObject());
JSONArray identifiers = json.optJSONArray("identifiers", new JSONArray()); JSONArray identifiers = json.optJSONArray("identifiers", new JSONArray());
JSONArray data = json.optJSONArray("data", new JSONArray()); JSONArray data = json.optJSONArray("data", new JSONArray());
JSONArray styles = json.optJSONArray("styles", new JSONArray()); JSONArray styles = json.optJSONArray("styles", new JSONArray());
for (int i = 0; i < data.length(); i++) { for (int i = 0; i < data.length(); i++) {
XSSFRow row = sheet.createRow(i); XSSFRow row = sheet.createRow(i);
JSONObject row_data = data.optJSONObject(i); JSONObject row_data = data.optJSONObject(i);
for (int j = 0; j < identifiers.length(); j++) { for (int j = 0; j < identifiers.length(); j++) {
sheet.autoSizeColumn(j);
String value = row_data.optString(identifiers.getString(j), "");
XSSFCell cell = row.createCell(j); XSSFCell cell = row.createCell(j);
JSONArray styleArray = styles.optJSONArray(i, new JSONArray()); JSONArray styleArray = styles.optJSONArray(i, new JSONArray());
JSONObject style = styleArray.optJSONObject(j, new JSONObject()); JSONObject style = styleArray.optJSONObject(j, new JSONObject());
XSSFCellStyle cellStyle = createCellStyle(workbook, if(style.isEmpty()){
getXSSFColor(style.optString("background", "#ffffff")), cell.setCellStyle(defaultStyle);
getXSSFColor(style.optString("foreground", "#333333")), }else{
style.optBoolean("bold", false), XSSFCellStyle cellStyle = createCellStyle(workbook,style);
style.optBoolean("border", true)); cell.setCellStyle(cellStyle);
cell.setCellStyle(cellStyle); }
cell.setCellValue(value); 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, private XSSFCellStyle createCellStyle(XSSFWorkbook workbook, JSONObject styleJson) {
boolean bold, boolean border) {
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(); XSSFCellStyle style = workbook.createCellStyle();
Font font = workbook.createFont(); 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.setWrapText(true);
style.setVerticalAlignment(VerticalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setFillPattern(FillPatternType.SOLID_FOREGROUND); style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
...@@ -95,9 +119,17 @@ public class ExcelService { ...@@ -95,9 +119,17 @@ public class ExcelService {
style.setBorderColor(BorderSide.TOP, getXSSFColor("#333333")); style.setBorderColor(BorderSide.TOP, getXSSFColor("#333333"));
style.setBorderColor(BorderSide.LEFT, getXSSFColor("#333333")); style.setBorderColor(BorderSide.LEFT, getXSSFColor("#333333"));
style.setBorderColor(BorderSide.RIGHT, 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); font.setBold(bold);
style.setFont(font); 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