En mi app movil estoy usando la libreria Apache Poi y generando un excel con HSSFWorkbook() esto funciona muy bien, ahora quiero cambiar a XSSFWorkbook pero me sale este error: javax.xml.stream.FactoryConfigurationError: Provider com.bea.xml.stream.EventFactory not found, leí algunas paginas y decia que debo poner en el gradel implementation 'javax.xml.stream:stax-api:1.0' pero tampoco funciono. En relidad quiero hacer este cambio de formato porque al abrir los excel siempre necesita internet para convertir el formato y además no me esta funcionando el style.setWrapText(true) y creo que es por esta conversión que hace. Este es mi codigo para los excel:
public void crearExelPorOperador(List<Reporte> listReportes){
String[] actividadesM = context.getResources().getStringArray(R.array.combo_tiposOperaciones);
String[] actividadesPrincipales = context.getResources().getStringArray(R.array.combo_tiposOperacionesPrinciapales);
String[] actividadesSecundarias = context.getResources().getStringArray(R.array.combo_tiposOperacionesSecundarias);
Workbook wb= new HSSFWorkbook();
Map<String, CellStyle> styles= createStyles(wb);
Sheet sheet= wb.createSheet("Informe");
sheet.setFitToPage(true);
sheet.setHorizontallyCenter(true);
//title row
String rango;
int nRow=0;
int nCell=0;
Row titleRow = sheet.createRow(nRow);
titleRow.setHeightInPoints(20);
Cell titleCell = titleRow.createCell(nCell);
titleCell.setCellValue("Informe General por Operador");
titleCell.setCellStyle(styles.get("title"));
sheet.addMergedRegion(new CellRangeAddress(
nRow, //first row (0-based)
nRow, //last row (0-based)
0, //first column (0-based)
actividadesM.length+7 //last column (0-based)
));
//Datos
//nombre de la empresa
nRow=nRow+1;
Row row = sheet.createRow(nRow);
Cell cell = row.createCell(nCell);
cell.setCellValue("Empresa: ");
cell.setCellStyle(styles.get("item_left1"));
rango="$A$"+(nRow+1)+":$B$"+(nRow+1);
sheet.addMergedRegion(CellRangeAddress.valueOf(rango));
nRow=nRow+1;
cell = row.createCell(nRow);
cell.setCellValue("Holcim Ecuador");
cell.setCellStyle(styles.get("item_left"));
//fecha
row = sheet.createRow(nRow);
cell = row.createCell(nCell);
cell.setCellValue("Fecha: ");
cell.setCellStyle(styles.get("item_left1"));
sheet.addMergedRegion(new CellRangeAddress(
nRow, //first row (0-based)
nRow, //last row (0-based)
0, //first column (0-based)
1 //last column (0-based)
));
cell = row.createCell(nRow);
cell.setCellValue(editFechadesde.getText().toString()+" - "+editFechahasta.getText().toString());
cell.setCellStyle(styles.get("item_left"));
nRow=nRow+1;
//fecha
row = sheet.createRow(nRow);
cell = row.createCell(nCell);
cell.setCellValue("Actividades Prinpales:");
cell.setCellStyle(styles.get("item_left1"));
sheet.addMergedRegion(new CellRangeAddress(
nRow, //first row (0-based)
nRow, //last row (0-based)
0, //first column (0-based)
2 //last column (0-based)
));
int i=0;
for(String n:actividadesPrincipales){
i=i+1;
nRow=nRow+1;
row = sheet.createRow(nRow);
cell = row.createCell(nCell);
cell.setCellValue(i);
cell.setCellStyle(styles.get("item_leftamarillo"));
cell = row.createCell(nCell+1);
cell.setCellValue(n);
cell.setCellStyle(styles.get("item_left"));
}
nRow=nRow+1;
row = sheet.createRow(nRow);
cell = row.createCell(nCell);
cell.setCellValue("Actividades Secundarias:");
cell.setCellStyle(styles.get("item_left1"));
sheet.addMergedRegion(new CellRangeAddress(
nRow, //first row (0-based)
nRow, //last row (0-based)
0, //first column (0-based)
2 //last column (0-based)
));
for(String n:actividadesSecundarias){
i=i+1;
nRow=nRow+1;
row = sheet.createRow(nRow);
cell = row.createCell(nCell);
cell.setCellValue(i);
cell.setCellStyle(styles.get("item_leflime"));
cell = row.createCell(nCell+1);
cell.setCellValue(n);
cell.setCellStyle(styles.get("item_left"));
}
//titulo
nRow=nRow+3;
nCell=nCell+1;
row = sheet.createRow(nRow);
cell = row.createCell(nCell);
cell.setCellValue("Operadores");
cell.setCellStyle(styles.get("headerAzul"));
for (int n = (nCell+1); n < (nCell+3); n++) {
cell = row.createCell(n);
cell.setCellStyle(styles.get("headerAzul"));
}
cell = row.createCell(nCell+3);
cell.setCellValue("Actividades");
cell.setCellStyle(styles.get("headerAzul"));
for (int n = (nCell+4); n < (nCell+actividadesM.length+5); n++) {
cell = row.createCell(n);
cell.setCellStyle(styles.get("headerAzul"));
}
sheet.addMergedRegion(new CellRangeAddress(
nRow, //first row (0-based)
nRow+1, //last row (0-based)
1, //first column (0-based)
3 //last column (0-based)
));
sheet.addMergedRegion(new CellRangeAddress(
nRow, //first row (0-based)
nRow, //last row (0-based)
4, //first column (0-based)
actividadesM.length+5 //last column (0-based)
));
cell = row.createCell(nCell+actividadesM.length+5);
cell.setCellValue("Total");
cell.setCellStyle(styles.get("headerAzul"));
sheet.addMergedRegion(new CellRangeAddress(
nRow, //first row (0-based)
nRow+1, //last row (0-based)
actividadesM.length+6, //first column (0-based)
actividadesM.length+6 //last column (0-based)
));
cell.setCellStyle(styles.get("headerAzul"));
nRow=nRow+1;
int index=0;
row = sheet.createRow(nRow);
for (int n = (nCell+3); n < (nCell+actividadesPrincipales.length+3); n++) {
cell = row.createCell(n);
cell.setCellValue(String.valueOf(index+1));
cell.setCellStyle(styles.get("headerAmarillo"));
index=index+1;
}
cell = row.createCell(nCell+3+actividadesPrincipales.length);
cell.setCellValue("T.P.");
cell.setCellStyle(styles.get("headerAzul"));
for (int n = (nCell+4+actividadesPrincipales.length); n < (nCell+actividadesSecundarias.length+4+actividadesPrincipales.length); n++) {
cell = row.createCell(n);
cell.setCellValue(String.valueOf(index+1));
cell.setCellStyle(styles.get("headerLime"));
index=index+1;
}
cell = row.createCell(nCell+4+actividadesPrincipales.length+actividadesSecundarias.length);
cell.setCellValue("T.S.");
cell.setCellStyle(styles.get("headerAzul"));
//contenido
nRow=nRow+1;
String[] operadores = context.getResources().getStringArray(R.array.combo_nombresOperadores);
List<sumaInformeOperador> listaOperadores=GetDataInformeporoperador(listReportes,asListString(operadores),context);
List<Double> sumasActividadesPrinpales=new ArrayList<>();
Double totalTodasPrincipales=0.0, totalTodasSecundarias=0.0;
for(String n:actividadesPrincipales){
sumasActividadesPrinpales.add(0.0);
}
List<Double> sumasActividadesSecundarias=new ArrayList<>();
for(String n:actividadesSecundarias){
sumasActividadesSecundarias.add(0.0);
}
for(sumaInformeOperador operador:listaOperadores){
row = sheet.createRow(nRow);
cell = row.createCell(nCell);
cell.setCellValue(operador.getNombreOperador());
cell.setCellStyle(styles.get("cell"));
for (int n = (nCell+1); n < (nCell+3); n++) {
cell = row.createCell(n);
cell.setCellStyle(styles.get("cell"));
}
sheet.addMergedRegion(new CellRangeAddress(
nRow, //first row (0-based)
nRow, //last row (0-based)
1, //first column (0-based)
3 //last column (0-based)
));
int index2=0;
double totalPrincipales=0.0;
for (int n = (nCell+3); n < (nCell+actividadesPrincipales.length+3); n++) {
cell = row.createCell(n);
double total= 0.0;
int posicion=indexActividadeenSuma(operador.getListaactividades(),actividadesPrincipales[index2]);
if (posicion!=-1){
total=operador.getListaactividades().get(posicion).getHoras();
}
totalPrincipales=totalPrincipales+total;
BigDecimal bdtotal = new BigDecimal(total).setScale(0, RoundingMode.HALF_UP);
int valtotal = (int) bdtotal.doubleValue();
cell.setCellValue(valtotal);
cell.setCellStyle(styles.get("cell"));
sumasActividadesPrinpales.set(index2,sumasActividadesPrinpales.get(index2)+total);
index2=index2+1;
}
BigDecimal bdprinciaples = new BigDecimal(totalPrincipales).setScale(0, RoundingMode.HALF_UP);
int valprinciaples = (int) bdprinciaples.doubleValue();
cell = row.createCell(nCell+3+actividadesPrincipales.length);
cell.setCellValue(valprinciaples);
cell.setCellStyle(styles.get("cell"));
int index3=0;
double totalSecundaria=0.0;
for (int n = (nCell+4+actividadesPrincipales.length); n < (nCell+actividadesSecundarias.length+4+actividadesPrincipales.length); n++) {
cell = row.createCell(n);
double total= 0.0;
int posicion=indexActividadeenSuma(operador.getListaactividades(),actividadesSecundarias[index3]);
if (posicion!=-1){
total=operador.getListaactividades().get(posicion).getHoras();
}
totalSecundaria=totalSecundaria+total;
BigDecimal bdtotal = new BigDecimal(total).setScale(0, RoundingMode.HALF_UP);
int valtotal = (int) bdtotal.doubleValue();
cell.setCellValue(valtotal);
cell.setCellStyle(styles.get("cell"));
sumasActividadesSecundarias.set(index3,sumasActividadesSecundarias.get(index3)+total);
index3=index3+1;
}
BigDecimal bdtotal = new BigDecimal(totalSecundaria).setScale(0, RoundingMode.HALF_UP);
int valtotal = (int) bdtotal.doubleValue();
cell = row.createCell(nCell+4+actividadesPrincipales.length+actividadesSecundarias.length);
cell.setCellValue(valtotal);
cell.setCellStyle(styles.get("cell"));
cell = row.createCell(nCell+actividadesM.length+5);
BigDecimal bdtotaltotal = new BigDecimal(totalPrincipales+totalSecundaria).setScale(0, RoundingMode.HALF_UP);
int valtotaltotal = (int) bdtotaltotal.doubleValue();
cell.setCellValue(valtotaltotal);
cell.setCellStyle(styles.get("headerAzul"));
nRow=nRow+1;
}
row = sheet.createRow(nRow);
cell = row.createCell(nCell);
cell.setCellValue("Total");
cell.setCellStyle(styles.get("headerAzul"));
for (int n = (nCell+1); n < (nCell+3); n++) {
cell = row.createCell(n);
cell.setCellStyle(styles.get("headerAzul"));
}
sheet.addMergedRegion(new CellRangeAddress(
nRow, //first row (0-based)
nRow, //last row (0-based)
1, //first column (0-based)
3 //last column (0-based)
));
int index4=0;
for (int n = (nCell+3); n < (nCell+actividadesPrincipales.length+3); n++) {
cell = row.createCell(n);
BigDecimal bdtotaltotal = new BigDecimal(sumasActividadesPrinpales.get(index4)).setScale(0, RoundingMode.HALF_UP);
int valtotaltotal = (int) bdtotaltotal.doubleValue();
cell.setCellValue(valtotaltotal);
cell.setCellStyle(styles.get("headerAmarillo"));
index4=index4+1;
}
for(Double n:sumasActividadesPrinpales){
totalTodasPrincipales=totalTodasPrincipales+n;
}
cell = row.createCell(nCell+3+actividadesPrincipales.length);
BigDecimal bdtotaltotal = new BigDecimal(totalTodasPrincipales).setScale(0, RoundingMode.HALF_UP);
int valtotaltotal = (int) bdtotaltotal.doubleValue();
cell.setCellValue(valtotaltotal);
cell.setCellStyle(styles.get("headerAzul"));
int index5=0;
for (int n = (nCell+4+actividadesPrincipales.length); n < (nCell+actividadesSecundarias.length+4+actividadesPrincipales.length); n++) {
cell = row.createCell(n);
BigDecimal bdtotalS = new BigDecimal(sumasActividadesSecundarias.get(index5)).setScale(0, RoundingMode.HALF_UP);
int valtotalS = (int) bdtotalS.doubleValue();
cell.setCellValue(valtotalS);
cell.setCellStyle(styles.get("headerLime"));
index5=index5+1;
}
for(Double n:sumasActividadesSecundarias){
totalTodasSecundarias=totalTodasSecundarias+n;
}
cell = row.createCell(nCell+4+actividadesPrincipales.length+actividadesSecundarias.length);
BigDecimal bdtotalS = new BigDecimal(totalTodasSecundarias).setScale(0, RoundingMode.HALF_UP);
int valtotalS = (int) bdtotalS.doubleValue();
cell.setCellValue(valtotalS);
cell.setCellStyle(styles.get("headerAzul"));
cell = row.createCell(nCell+5+actividadesPrincipales.length+actividadesSecundarias.length);
BigDecimal bdtotalSP = new BigDecimal(totalTodasSecundarias+totalTodasPrincipales).setScale(0, RoundingMode.HALF_UP);
int valtotalSP = (int) bdtotalSP.doubleValue();
cell.setCellValue(valtotalSP);
cell.setCellStyle(styles.get("headerAzul"));
//creacion del documento
String nombreFile= "InformeGeneralPorOperador.xls";
File file = new File(context.getExternalFilesDir(null),nombreFile);
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
wb.write(outputStream);
Toast.makeText(context.getApplicationContext(),"Reporte generado correctamente",Toast.LENGTH_LONG).show();
String[] mailto = {correo};
Uri uri = FileProvider.getUriForFile(
context,
"com.example.operacionesmteriasprimas", //(use your app signature + ".provider" )
file);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, mailto);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Reporte de horas de las actividades realizadas "+editFechadesde.getText().toString()+" - "+editFechahasta.getText().toString());
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"Fecha de reporte: "+editFechadesde.getText().toString()+" - "+editFechahasta.getText().toString()+".\nTipo: General por operador"+".\nAtentamente HolcimQuarry.");
emailIntent.setType("application/pdf");
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
context.startActivity(Intent.createChooser(emailIntent, "Send email using:"));
} catch (java.io.IOException e) {
e.printStackTrace();
Toast.makeText(context.getApplicationContext(),"NO OK",Toast.LENGTH_LONG).show();
try {
outputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
private static Map<String, CellStyle> createStyles(Workbook wb){
Map<String, CellStyle> styles = new HashMap<>();
CellStyle style;
Font titleFont = wb.createFont();
titleFont.setFontHeightInPoints((short)18);
titleFont.setBold(true);
style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setFont(titleFont);
styles.put("title", style);
Font itemFont = wb.createFont();
itemFont.setFontHeightInPoints((short)12);
itemFont.setFontName("Trebuchet MS");
style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.LEFT);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setFont(titleFont);
style.setFont(itemFont);
styles.put("item_left", style);
Font itemresFont = wb.createFont();
itemresFont.setFontHeightInPoints((short)12);
itemresFont.setFontName("Trebuchet MS");
itemresFont.setBold(true);
style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.LEFT);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setFont(itemresFont);
styles.put("item_left1", style);
Font itemFontcenter = wb.createFont();
itemFontcenter.setFontHeightInPoints((short)12);
itemFontcenter.setFontName("Trebuchet MS");
style = wb.createCellStyle();
style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setFont(titleFont);
style.setFont(itemFontcenter);
styles.put("item_leftamarillo", style);
Font itemFontcenterlime = wb.createFont();
itemFontcenterlime.setFontHeightInPoints((short)12);
itemFontcenterlime.setFontName("Trebuchet MS");
style = wb.createCellStyle();
style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setFillForegroundColor(IndexedColors.LIME.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setFont(titleFont);
style.setFont(itemFontcenterlime);
styles.put("item_leflime", style);
Font monthFont = wb.createFont();
monthFont.setFontHeightInPoints((short)12);
monthFont.setColor(IndexedColors.WHITE.getIndex());
style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setFont(monthFont);
style.setWrapText(true);
styles.put("header", style);
Font monthFont2 = wb.createFont();
monthFont2.setFontHeightInPoints((short)12);
monthFont2.setFontName("Trebuchet MS");
//monthFont2.setColor(IndexedColors.WHITE.getIndex());
style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
style.setFont(monthFont2);
style.setWrapText(true);
styles.put("header2", style);
Font headerFont = wb.createFont();
headerFont.setBold(true);
style = createBorderedStyle(wb);
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setFont(headerFont);
styles.put("headerAzul", style);
Font headerFont2 = wb.createFont();
headerFont2.setBold(true);
style = createBorderedStyle(wb);
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setFont(headerFont2);
styles.put("headerAmarillo", style);
Font headerFont3 = wb.createFont();
headerFont3.setBold(true);
style = createBorderedStyle(wb);
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setFillForegroundColor(IndexedColors.LIME.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setFont(headerFont3);
styles.put("headerLime", style);
Font cellFont = wb.createFont();
cellFont.setFontHeightInPoints((short)12);
style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
style.setFont(cellFont);
style.setWrapText(true);
styles.put("cell", style);
return styles;
}