import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFDataFormat; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; /** * */ import com.bmv.emisnet.dto.DataDTO; /** * @author jolvera * */ public class ImportaArchivo { /** * */ public ImportaArchivo() { } /** * @param args */ public static void main(String[] args) { List> formatos = generarFormatos(Integer.parseInt(args[0]),Integer.parseInt(args[0])); exportaInfoxLibro(formatos); } @SuppressWarnings("unchecked") private static void exportaInfoxLibro(List> formatos) { // new workbook HSSFWorkbook wb = new HSSFWorkbook(); HSSFCellStyle cellStyle = wb.createCellStyle(); // set a sheet for each format of a map for (Map formato : formatos) { String idFormato = (String) formato.get("formato"); // get the titles for the second row of the sheet HSSFSheet libroExcel = wb.createSheet(idFormato); Short i = 0; HSSFRow row = libroExcel.createRow(i); row.createCell(i).setCellValue("test"); Map titulos = (Map) formato.get("data"); i ++; int cuentaValores= titulos.size(); boolean esRenglon=true; short fc= 0; HSSFRow row1 = libroExcel.createRow(1); for (int e = 0; e < cuentaValores; e++) { { row1.createCell(fc).setCellValue("s"+String.valueOf(fc)); fc++; } } int cuentaRenglon=2; for (int w = 0; w < cuentaValores; w++) { { short r=1; short celda=0; //get data and put in cells for (short c = 0; c < cuentaValores; c++) { String key= null; // check if area rows or colums if (esRenglon==true) { HSSFRow titulo = libroExcel.getRow(1); HSSFCell cell= titulo.getCell(c) ; key= cell.getStringCellValue(); }else if (esRenglon==false){ // work with columns HSSFRow renglonT = libroExcel.getRow(r); HSSFCell cellT = renglonT.getCell((short)0); key= cellT.getStringCellValue(); } Object objetoMapa= titulos.get(key); if (esRenglon==true) { agregaDatos(objetoMapa, libroExcel, celda, cuentaRenglon); celda++; } } } // create file FileOutputStream fos = null; try { fos = new FileOutputStream(""+File.separator+"test"+"_"+"1"+"."+"xls" ); } catch (FileNotFoundException e) { e.printStackTrace(); } try { wb.write(fos); } catch (IOException ioe) { } } private static void agregaDatos(Object valorCelda, HSSFSheet formato, short c, int cuentaRenglon) { HSSFRow row = null; row= formato.createRow(cuentaRenglon); row.createCell((short) c).setCellValue((String)valorCelda); } private static List> generarFormatos(int hojas, int renglones){ List> listaFormatos = new ArrayList>(); Map valores = new HashMap(); for (int i = 0; i < hojas; i++) { valores.put("data",generarDatos(renglones)); valores.put("formato", "Sheet"+String.valueOf(hojas)); listaFormatos.add(valores); } return listaFormatos; } private static List> generarDatos(int renglones){ List> listaMapas = new ArrayList>(); Map valores = new HashMap(); for (int i = 0; i < renglones; i++) { valores.put("s"+String.valueOf(renglones),String.valueOf(renglones) ); listaMapas.add(valores); } return listaMapas; } }