Example usage for org.apache.poi.xssf.usermodel XSSFWorkbook createFont

List of usage examples for org.apache.poi.xssf.usermodel XSSFWorkbook createFont

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFWorkbook createFont.

Prototype

@Override
public XSSFFont createFont() 

Source Link

Document

Create a new Font and add it to the workbook's font table

Usage

From source file:aco.Utilities.java

License:Open Source License

static void writeInputDataPoints() {
    //the file already exists
    if (new File(filePath3).canRead()) {
        //System.out.println("File already exists..");
        try {//w w  w  .  ja v a2  s . c om
            FileInputStream file = new FileInputStream(new File(filePath3));

            //Create Workbook instance holding reference to .xlsx file
            XSSFWorkbook workbook1 = new XSSFWorkbook(file);

            //Get first/desired sheet from the workbook
            XSSFSheet sheet1 = workbook1.getSheetAt(11);

            //define a cell style for bold font
            CellStyle style = workbook1.createCellStyle();
            Font font = workbook1.createFont();
            font.setBoldweight(Font.BOLDWEIGHT_BOLD);
            style.setFont(font);

            Row r1 = sheet1.getRow(0);
            if (r1 == null) {
                // First cell in the row, create
                r1 = sheet1.createRow(0);
            }

            Cell c = r1.getCell(0);
            if (c == null) {
                c = r1.createCell(0);
            }
            c.setCellValue("VRPTW instance - " + VRPTW.instance.name
                    + "; data point coordinates corresponding to customers' requests");
            c.setCellStyle(style);

            Row r = sheet1.getRow(2);
            if (r == null) {
                // First cell in the row, create
                r = sheet1.createRow(2);
            }

            Cell c1 = r.getCell(0);
            if (c1 == null) {
                c1 = r.createCell(0);
            }
            c1.setCellValue("Point #");
            c1.setCellStyle(style);

            c1 = r.getCell(1);
            if (c1 == null) {
                c1 = r.createCell(1);
            }
            c1.setCellValue("X Coord");
            c1.setCellStyle(style);

            c1 = r.getCell(2);
            if (c1 == null) {
                c1 = r.createCell(2);
            }
            c1.setCellValue("Y Coord");
            c1.setCellStyle(style);

            int size = VRPTW.instance.nodes.length;
            int rowIndex = 3;
            double x, y;
            for (int i = 0; i < size; i++) {
                x = VRPTW.instance.nodes[i].x;
                y = VRPTW.instance.nodes[i].y;
                r = sheet1.getRow(rowIndex + i);
                if (r == null) {
                    // First cell in the row, create
                    //System.out.println("Empty row, create new one");
                    r = sheet1.createRow(rowIndex + i);
                }

                c1 = r.getCell(0);
                if (c1 == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c1 = r.createCell(0);
                }
                c1.setCellValue(i);

                c1 = r.getCell(1);
                if (c1 == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c1 = r.createCell(1);
                }
                c1.setCellValue(x);

                c1 = r.getCell(2);
                if (c1 == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c1 = r.createCell(2);
                }
                c1.setCellValue(y);

            }

            //Write the workbook in file system
            FileOutputStream out = new FileOutputStream(new File(filePath3));
            workbook1.write(out);
            out.close();

            //System.out.println("Written successfully on disk.");
        } catch (Exception e) {
            e.printStackTrace();
        }

    } else {
        System.out.println("File not exists..");
    }
}

From source file:aco.Utilities.java

License:Open Source License

static void writeResultsExcel(int trialNumber, boolean saveIterCosts) {
    Row r, r1;/*ww w.  jav  a2s .  c o  m*/
    Cell c;
    int index1 = 0, index2 = 0, index3 = 0, index4 = 0, index5 = 0;
    //int index6 = 0;

    //the file already exists; we should add a new row as the last one in the Excel file
    if (new File(filePath).canRead()) {
        //System.out.println("File already exists..");
        try {
            FileInputStream file = new FileInputStream(new File(filePath));

            //Create Workbook instance holding reference to .xlsx file
            XSSFWorkbook workbook1 = new XSSFWorkbook(file);

            int startIndex = 0, rowIndex = 0;
            /*switch (VRPTW.m) {
               case 2: 
             startIndex = 0;
             rowIndex = 4;
             break;
               case 3: 
             startIndex = 2;
             rowIndex = 5;
             break;
               case 5: 
             startIndex = 4;
             rowIndex = 7;
             break;
               case 7: 
             startIndex = 6;
             rowIndex = 9;
             break;
               default:
             System.out.println("Unknown value for m");
             break;         
            }*/

            //Get desired sheet from the workbook
            XSSFSheet sheet1 = workbook1.getSheetAt(startIndex); //for tours
            /*XSSFSheet sheet2 = workbook1.getSheetAt(startIndex + 1);  //for number of assigned cities
            XSSFSheet sheet3 = workbook1.getSheetAt(startIndex + 2);  //for cost of individual subtours
            XSSFSheet sheet4 = workbook1.getSheetAt(startIndex + 3);  //for total cost of subtours
            XSSFSheet sheet5 = workbook1.getSheetAt(startIndex + 4);  //for verbose output of total cost at each 5 iteration
            */
            XSSFSheet sheet2 = workbook1.getSheetAt(startIndex + 1); //for verbose output of longest cost at each 5 iteration

            //define a cell style for bold font
            CellStyle style = workbook1.createCellStyle();
            Font font = workbook1.createFont();
            font.setBoldweight(Font.BOLDWEIGHT_BOLD);
            style.setFont(font);

            //define style with bold font and blue color for font
            CellStyle styleBoldBlue = workbook1.createCellStyle();
            font = workbook1.createFont();
            font.setBoldweight(Font.BOLDWEIGHT_BOLD);
            font.setColor(IndexedColors.BLUE.index);
            styleBoldBlue.setFont(font);

            index1 = 133;
            if (!saveIterCosts) {
                //write only once the name of the algorithm that was run
                if (trialNumber == 0) {
                    r = sheet1.getRow(index1);
                    if (r == null) {
                        // First cell in the row, create
                        //System.out.println("Empty row, create new one");
                        r = sheet1.createRow(index1);
                    }

                    c = r.getCell(0);
                    if (c == null) {
                        // New cell
                        //System.out.println("Empty cell, create new one");
                        c = r.createCell(0);
                    }
                    c.setCellValue(
                            "Obtained solutions (values) after running new version (ACS MinMax global, voiajor si oras alesi simultan) with local search");
                    c.setCellStyle(styleBoldBlue);
                }

                //write only once the table header
                index1 = index1 + 3;
                r = sheet1.getRow(index1);
                if (r == null) {
                    // First cell in the row, create
                    //System.out.println("Empty row, create new one");
                    r = sheet1.createRow(index1);
                }

                c = r.getCell(0);
                if (c == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c = r.createCell(0);
                }
                c.setCellValue("Run #");
                c.setCellStyle(style);

                c = r.getCell(1);
                if (c == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c = r.createCell(1);
                }
                c.setCellValue("MinMax (cost of longest subtour)");
                c.setCellStyle(style);

                c = r.getCell(2);
                if (c == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c = r.createCell(2);
                }
                c.setCellValue("Total Cost");
                c.setCellStyle(style);

                c = r.getCell(3);
                if (c == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c = r.createCell(3);
                }
                c.setCellValue("Amplitude");
                c.setCellStyle(style);

                //write number of run
                index1 = 137 + trialNumber;
                r = sheet1.getRow(index1);
                if (r == null) {
                    // First cell in the row, create
                    //System.out.println("Empty row, create new one");
                    r = sheet1.createRow(index1);
                }

                c = r.getCell(0);
                if (c == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c = r.createCell(0);
                }
                c.setCellValue(trialNumber + 1);

                //write MinMax (cost of longest subtour)
                double longestSubtour = getLongestSubtour();
                c = r.getCell(1);
                if (c == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c = r.createCell(1);
                }
                c.setCellValue(longestSubtour);

                //write total cost
                double totalCost = getTotalCost();
                c = r.getCell(2);
                if (c == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c = r.createCell(2);
                }
                c.setCellValue(totalCost);

                //write amplitude
                double amplitude = getAmplitude();
                c = r.getCell(3);
                if (c == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c = r.createCell(3);
                }
                c.setCellValue(amplitude);
            }

            index5 = 859;
            if (saveIterCosts) {
                //write only once the name of the algorithm that was run
                if (trialNumber == 0) {
                    r = sheet2.getRow(index5);
                    if (r == null) {
                        // First cell in the row, create
                        //System.out.println("Empty row, create new one");
                        r = sheet2.createRow(index5);
                    }

                    c = r.getCell(0);
                    if (c == null) {
                        // New cell
                        //System.out.println("Empty cell, create new one");
                        c = r.createCell(0);
                    }
                    c.setCellValue(
                            "Longest cost of subtour at each 5 iteration after running new version (ACS MinMax global, voiajor si oras alesi simultan) with local search");
                    c.setCellStyle(styleBoldBlue);

                    int tempIndex = index5 + 3;
                    r = sheet2.getRow(tempIndex);
                    if (r == null) {
                        // First cell in the row, create
                        //System.out.println("Empty row, create new one");
                        r = sheet2.createRow(tempIndex);
                    }
                    ArrayList<Integer> iterNumber = getIterNumber();

                    c = r.getCell(0);
                    if (c == null) {
                        // New cell
                        //System.out.println("Empty cell, create new one");
                        c = r.createCell(0);
                    }
                    c.setCellValue("Nr Iter");
                    c.setCellStyle(style);

                    int indexTemp = 0;
                    for (int j = 0; j < iterNumber.size(); j++) {
                        indexTemp = tempIndex + 1 + j;
                        r1 = sheet2.getRow(indexTemp);
                        if (r1 == null) {
                            // First cell in the row, create
                            //System.out.println("Empty row, create new one");
                            r1 = sheet2.createRow(indexTemp);
                        }

                        c = r1.getCell(0);
                        if (c == null) {
                            // New cell
                            //System.out.println("Empty cell, create new one");
                            c = r1.createCell(0);
                        }
                        c.setCellValue(iterNumber.get(j));
                    }
                }

                index5 = index5 + 3;
                r = sheet2.getRow(index5);
                if (r == null) {
                    // First cell in the row, create
                    //System.out.println("Empty row, create new one");
                    r = sheet2.createRow(index5);
                }

                //for each trial run save at each 5 iteration the best longest cost of a subtour so far
                ArrayList<Double> iterLongestCost = getIterLongestCost();
                int index;

                //for each run write the table header cell
                c = r.getCell(trialNumber + 1);
                if (c == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c = r.createCell(trialNumber + 1);
                }
                c.setCellValue("Run " + (trialNumber + 1));
                c.setCellStyle(style);

                for (int j = 0; j < iterLongestCost.size(); j++) {
                    index = index5 + 1 + j;
                    r1 = sheet2.getRow(index);
                    if (r1 == null) {
                        // First cell in the row, create
                        //System.out.println("Empty row, create new one");
                        r1 = sheet2.createRow(index);
                    }

                    c = r1.getCell(trialNumber + 1);
                    if (c == null) {
                        // New cell
                        //System.out.println("Empty cell, create new one");
                        c = r1.createCell(trialNumber + 1);
                    }
                    c.setCellValue(iterLongestCost.get(j));
                }
            }

            //Write the workbook in file system
            FileOutputStream out = new FileOutputStream(new File(filePath));
            workbook1.write(out);
            out.close();

            int nrOfRun = trialNumber + 1;
            System.out.println("\nRun #" + nrOfRun + " written successfully on disk.\n");
        } catch (Exception e) {
            e.printStackTrace();
        }

    } else {
        //Blank workbook
        System.out.println("File " + filePath + " doesn't exists..");

    }
}

From source file:aco.Utilities.java

License:Open Source License

static void writeExcelFinalSolution(int trial, double scalledValue) {
    Row r;//from   w ww . ja  va2  s  .  c  o m
    Cell c;
    int index1 = 0;

    //the file already exists; we should add a new row as the last one in the Excel file
    if (new File(filePath5).canRead()) {
        //System.out.println("File already exists..");
        try {
            FileInputStream file = new FileInputStream(new File(filePath5));

            //Create Workbook instance holding reference to .xlsx file
            XSSFWorkbook workbook1 = new XSSFWorkbook(file);

            //Get desired sheet from the workbook
            XSSFSheet sheet1 = workbook1.getSheetAt(0);

            //define a cell style for bold font
            CellStyle style = workbook1.createCellStyle();
            Font font = workbook1.createFont();
            font.setBoldweight(Font.BOLDWEIGHT_BOLD);
            style.setFont(font);

            //define style with bold font and blue color for font
            CellStyle styleBoldBlue = workbook1.createCellStyle();
            font = workbook1.createFont();
            font.setBoldweight(Font.BOLDWEIGHT_BOLD);
            font.setColor(IndexedColors.BLUE.index);
            styleBoldBlue.setFont(font);

            index1 = 8; //8  //26

            index1 = index1 + trial;
            r = sheet1.getRow(index1);
            if (r == null) {
                // First cell in the row, create
                //System.out.println("Empty row, create new one");
                r = sheet1.createRow(index1);
            }

            int nrOfRun = trial + 1;
            //write trial number (Run #)
            c = r.getCell(15);
            if (c == null) {
                // New cell
                //System.out.println("Empty cell, create new one");
                c = r.createCell(15);
            }
            c.setCellValue(nrOfRun);

            //write number of used vehicles
            c = r.getCell(16);
            if (c == null) {
                // New cell
                //System.out.println("Empty cell, create new one");
                c = r.createCell(16);
            }
            c.setCellValue(Ants.best_so_far_ant.usedVehicles);

            //write total traveled distance
            c = r.getCell(17);
            if (c == null) {
                // New cell
                //System.out.println("Empty cell, create new one");
                c = r.createCell(17);
            }
            c.setCellValue(scalledValue);

            //write the total number of feasible solutions
            c = r.getCell(18);
            if (c == null) {
                // New cell
                //System.out.println("Empty cell, create new one");
                c = r.createCell(18);
            }
            c.setCellValue(InOut.noSolutions);

            //Write the workbook in file system
            FileOutputStream out = new FileOutputStream(new File(filePath5));
            workbook1.write(out);
            out.close();

            System.out.println("\nRun #" + nrOfRun + " written successfully on disk.\n");
        }

        catch (Exception e) {
            e.printStackTrace();
        }

    } else {
        //Blank workbook
        System.out.println("File " + filePath5 + " doesn't exists..");

    }
}

From source file:br.com.jinsync.controller.ExportExcelBook.java

License:Open Source License

@Override
protected Object doInBackground() throws Exception {

    final String dir = System.getProperty("user.dir") + "\\layouts";

    File arqProp = new File(dir);
    if (!arqProp.exists()) {
        arqProp.mkdirs();/*from   w  ww .j  a  v  a2s .c  o m*/
    }

    String nameFile = this.nameFile;

    int lin = 0;
    int col = 0;
    int pos = 0;
    int posFim = 0;

    pos = nameFile.lastIndexOf("(");
    if (pos > 0) {
        posFim = nameFile.lastIndexOf(")");
        nameFile = dir + "\\" + nameFile.substring(pos + 1, posFim) + "_layout" + ".xlsx";
    } else {
        pos = nameFile.lastIndexOf("\\");
        if (pos > 0) {
            posFim = nameFile.lastIndexOf(".");
            if (posFim > 0) {
                nameFile = dir + "\\" + nameFile.substring(pos + 1, posFim) + "_layout" + ".xlsx";
            } else {
                nameFile = dir + nameFile.substring(pos) + "_layout" + ".xlsx";
            }
        }
    }

    FileOutputStream out;

    try {
        out = new FileOutputStream(nameFile);
        TableModel model = tableName.getModel();

        XSSFWorkbook wb = new XSSFWorkbook(); // Criando area de trabalho
        // para o excel
        XSSFSheet s = wb.createSheet(); // criando uma nova sheet

        XSSFFont f = wb.createFont();
        XSSFFont f2 = wb.createFont();

        XSSFCellStyle cs = wb.createCellStyle();
        XSSFCellStyle cs2 = wb.createCellStyle();
        XSSFCellStyle cs3 = wb.createCellStyle();
        XSSFCellStyle cs4 = wb.createCellStyle();

        f.setFontHeightInPoints((short) 8);
        f2.setFontHeightInPoints((short) 8);

        f.setBoldweight(Font.BOLDWEIGHT_BOLD);
        f2.setBoldweight(Font.BOLDWEIGHT_NORMAL);

        f.setFontName("Courier New");
        f2.setFontName("Courier New");

        XSSFRow r = null; // Criando uma referencia para Linha
        XSSFCell c = null; // Referencia para Celula

        cs.setFont(f);
        cs2.setFont(f2);
        cs3.setFont(f2);
        cs4.setFont(f);

        cs2.setAlignment(CellStyle.ALIGN_LEFT);
        cs3.setAlignment(CellStyle.ALIGN_RIGHT);
        cs4.setAlignment(CellStyle.ALIGN_RIGHT);

        r = s.createRow(lin);
        for (int i = 1; i < model.getColumnCount(); i++) {
            c = r.createCell(col);
            c.setCellStyle(cs);
            c.setCellValue(model.getColumnName(i));
            col = col + 1;
        }

        col = 0;
        for (int i = 0; i < model.getRowCount(); i++) {
            lin = lin + 1;
            r = s.createRow(lin);
            col = 0;
            for (int j = 1; j < model.getColumnCount(); j++) {
                String valor = model.getValueAt(i, j).toString();
                if (valor == null) {
                    valor = "";
                }

                c = r.createCell(col);
                c.setCellStyle(cs2);

                if (valor.matches("-?\\d+(\\.\\d+)?")) {
                    c.setCellValue(Double.parseDouble(valor));
                    c.setCellType(Cell.CELL_TYPE_NUMERIC);
                    c.setCellStyle(cs4);
                } else {
                    c.setCellValue(valor);
                }
                col = col + 1;
            }
        }

        for (int i = 0; i <= model.getColumnCount(); i++) {
            s.autoSizeColumn(i);
        }

        wb.write(out);
        wb.close();
        out.close();

        Desktop desktop = Desktop.getDesktop();
        desktop.open(new File(nameFile));

    } catch (FileNotFoundException e) {
        JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
        e.printStackTrace();
    } catch (IOException e) {
        JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
        e.printStackTrace();
    }

    return null;

}

From source file:br.com.jinsync.controller.ExportExcelFile.java

License:Open Source License

@Override
protected Object doInBackground() throws Exception {

    int valuePgsBar = 0;
    int qtdTotLin = 0;

    final String dir = System.getProperty("user.dir") + "\\file";

    File arqProp = new File(dir);
    if (!arqProp.exists()) {
        arqProp.mkdirs();//from   w  w w .  j ava 2 s . co m
    }

    String nameFile = name;

    int lin = 0;
    int col = 0;
    int pos = 0;
    int posEnd = 0;

    pos = nameFile.lastIndexOf("(");
    if (pos > 0) {
        posEnd = nameFile.lastIndexOf(")");
        nameFile = dir + "\\" + nameFile.substring(pos + 1, posEnd) + "_file" + ".xlsx";
    } else {
        pos = nameFile.lastIndexOf("\\");
        if (pos > 0) {
            posEnd = nameFile.lastIndexOf(".");
            if (posEnd > 0) {
                nameFile = dir + "\\" + nameFile.substring(pos + 1, posEnd) + "_file" + ".xlsx";
            } else {
                nameFile = dir + nameFile.substring(pos) + "_file" + ".xlsx";
            }
        }
    }

    FileOutputStream out;

    try {
        out = new FileOutputStream(nameFile);
        TableModel model = tableName.getModel();

        XSSFWorkbook wb = new XSSFWorkbook(); // Criando area de trabalho
        // para o excel
        XSSFSheet s = wb.createSheet(); // criando uma nova sheet

        XSSFFont f = wb.createFont();
        XSSFFont f2 = wb.createFont();

        XSSFCellStyle cs = wb.createCellStyle();
        XSSFCellStyle cs2 = wb.createCellStyle();
        XSSFCellStyle cs3 = wb.createCellStyle();
        XSSFCellStyle cs4 = wb.createCellStyle();

        f.setFontHeightInPoints((short) 8);
        f2.setFontHeightInPoints((short) 8);

        f.setBoldweight(Font.BOLDWEIGHT_BOLD);
        f2.setBoldweight(Font.BOLDWEIGHT_NORMAL);

        f.setFontName("Courier New");
        f2.setFontName("Courier New");

        XSSFRow r = null; // Criando uma referencia para Linha
        XSSFCell c = null; // Referencia para Celula

        cs.setFont(f);
        cs2.setFont(f2);
        cs3.setFont(f2);
        cs4.setFont(f2);

        cs2.setAlignment(CellStyle.ALIGN_LEFT);
        cs3.setAlignment(CellStyle.ALIGN_RIGHT);
        cs4.setAlignment(CellStyle.ALIGN_RIGHT);

        r = s.createRow(lin);
        for (int i = 1; i < model.getColumnCount(); i++) {
            c = r.createCell(col);
            c.setCellStyle(cs);
            c.setCellValue(model.getColumnName(i));
            col = col + 1;
        }

        col = 0;
        qtdTotLin = model.getRowCount() - 1;

        if (qtdTotLin == 0) {
            qtdTotLin = 1;
        }

        for (int i = 0; i < model.getRowCount(); i++) {
            valuePgsBar = (i * 100) / qtdTotLin;
            lin = lin + 1;
            r = s.createRow(lin);
            col = 0;
            for (int j = 1; j < model.getColumnCount(); j++) {
                String valor = model.getValueAt(i, j).toString();
                if (valor == null) {
                    valor = "";
                }

                c = r.createCell(col);
                c.setCellStyle(cs2);

                valor = valor.trim();
                if (valor.matches("-?\\d+(\\.\\d+)?")) {
                    c.setCellValue(Double.parseDouble(valor));
                    c.setCellType(Cell.CELL_TYPE_NUMERIC);
                    c.setCellStyle(cs4);
                } else {
                    c.setCellValue(valor);
                }
                col = col + 1;
                setProgress(valuePgsBar);
            }
        }

        for (int i = 0; i <= model.getColumnCount(); i++) {
            s.autoSizeColumn(i);
        }

        wb.write(out);
        wb.close();
        out.close();

        Desktop desktop = Desktop.getDesktop();
        desktop.open(new File(nameFile));

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
        e.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return null;
}

From source file:br.com.jinsync.controller.ExportExcelString.java

License:Open Source License

@Override
protected Object doInBackground() throws Exception {

    final String dir = System.getProperty("user.dir") + "\\string";

    File arqProp = new File(dir);
    if (!arqProp.exists()) {
        arqProp.mkdirs();/*from w  w  w .j  a  v  a  2  s  .  com*/
    }

    String nameFile = this.nameFile;

    int lin = 0;
    int col = 0;
    int pos = 0;
    int posFim = 0;
    int valDec = 0;

    pos = nameFile.lastIndexOf("(");
    if (pos > 0) {
        posFim = nameFile.lastIndexOf(")");
        nameFile = dir + "\\" + nameFile.substring(pos + 1, posFim) + "_string" + ".xlsx";
    } else {
        pos = nameFile.lastIndexOf("\\");
        if (pos > 0) {
            posFim = nameFile.lastIndexOf(".");
            if (posFim > 0) {
                nameFile = dir + "\\" + nameFile.substring(pos + 1, posFim) + "_string" + ".xlsx";
            } else {
                nameFile = dir + nameFile.substring(pos) + "_string" + ".xlsx";
            }
        }
    }

    FileOutputStream out;

    try {
        out = new FileOutputStream(nameFile);
        TableModel model = tableName.getModel();

        XSSFWorkbook wb = new XSSFWorkbook(); // Criando area de trabalho
        // para o excel
        XSSFSheet s = wb.createSheet(); // criando uma nova sheet

        XSSFFont f = wb.createFont();
        XSSFFont f2 = wb.createFont();

        XSSFCellStyle cs = wb.createCellStyle();
        XSSFCellStyle cs2 = wb.createCellStyle();
        XSSFCellStyle cs3 = wb.createCellStyle();
        XSSFCellStyle cs4 = wb.createCellStyle();

        f.setFontHeightInPoints((short) 8);
        f2.setFontHeightInPoints((short) 8);

        f.setBoldweight(Font.BOLDWEIGHT_BOLD);
        f2.setBoldweight(Font.BOLDWEIGHT_NORMAL);

        f.setFontName("Courier New");
        f2.setFontName("Courier New");

        XSSFRow r = null; // Criando uma referencia para Linha
        XSSFCell c = null; // Referencia para Celula

        cs.setFont(f);
        cs2.setFont(f2);
        cs3.setFont(f2);
        cs4.setFont(f);

        cs2.setAlignment(CellStyle.ALIGN_LEFT);
        cs3.setAlignment(CellStyle.ALIGN_RIGHT);
        cs4.setAlignment(CellStyle.ALIGN_RIGHT);

        r = s.createRow(lin);

        r = s.createRow(0); // Criando a primeira linha na LINHA zero, que
        // seria o nmero 1
        c = r.createCell(0); // Criando a celula na posicao ZERO, que seria
                             // A, com referencia na linha zero acima =
                             // A1
        c.setCellStyle(cs);
        c.setCellValue(Language.stringFieldName);

        c = r.createCell(1);
        c.setCellStyle(cs);
        c.setCellValue(Language.stringType);

        c = r.createCell(2);
        c.setCellStyle(cs);
        c.setCellValue(Language.stringLength);

        c = r.createCell(3);
        c.setCellStyle(cs);
        c.setCellValue(Language.stringDecimal);

        c = r.createCell(4);
        c.setCellStyle(cs);
        c.setCellValue(Language.stringTotal);

        c = r.createCell(5);
        c.setCellStyle(cs4);
        c.setCellValue(Language.stringContent);

        col = 0;
        lin = lin + 1;
        for (int i = 0; i < model.getColumnCount(); i++) {

            r = s.createRow(lin);
            c = r.createCell(col);
            c.setCellStyle(cs2);
            c.setCellValue(model.getColumnName(i));

            col = col + 1;

            c = r.createCell(col);
            c.setCellStyle(cs2);
            c.setCellValue(tipoConteudo.get(i));

            col = col + 1;
            c = r.createCell(col);
            c.setCellStyle(cs2);
            if (tamanhoConteudo.get(i).matches("-?\\d+(\\.\\d+)?")) {
                c.setCellValue(Double.parseDouble(tamanhoConteudo.get(i)));
                c.setCellType(Cell.CELL_TYPE_NUMERIC);
                c.setCellStyle(cs3);
            } else {
                c.setCellValue(tamanhoConteudo.get(i));
            }

            col = col + 1;
            c = r.createCell(col);
            c.setCellStyle(cs2);
            if (decimalConteudo.get(i).matches("-?\\d+(\\.\\d+)?")) {
                c.setCellValue(Integer.parseInt(decimalConteudo.get(i)));
                valDec = Integer.parseInt(decimalConteudo.get(i));
                c.setCellType(Cell.CELL_TYPE_NUMERIC);
                c.setCellStyle(cs3);
            } else {
                c.setCellValue(decimalConteudo.get(i));
                valDec = 0;
            }

            col = col + 1;
            c = r.createCell(col);
            c.setCellStyle(cs2);
            c.setCellValue(totalConteudo.get(i));

            col = col + 1;
            c = r.createCell(col);
            c.setCellStyle(cs2);

            String valor = model.getValueAt(0, i).toString().trim();
            if (valor == null) {
                valor = "";
            }

            if (valor.matches("-?\\d+(\\.\\d+)?")) {

                if (Double.parseDouble(valor) > 0 && valDec > 0) {
                    double val2 = Double.parseDouble(valor) / (Math.pow(10, valDec));
                    NumberFormat format = NumberFormat.getInstance();
                    format.setMinimumFractionDigits(2);
                    format.setMaximumFractionDigits(valDec);
                    c.setCellValue(format.format(val2).toString());

                } else {
                    c.setCellValue(Double.parseDouble(valor));
                }

                // c.setCellType(Cell.CELL_TYPE_NUMERIC);
                c.setCellStyle(cs3);
            } else {
                c.setCellValue(valor);
            }

            lin = lin + 1;
            col = 0;
        }

        for (int i = 0; i <= model.getColumnCount(); i++) {
            s.autoSizeColumn(i);
        }

        wb.write(out);
        wb.close();
        out.close();

        Desktop desktop = Desktop.getDesktop();
        desktop.open(new File(nameFile));

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
        //e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
        //e.printStackTrace();
    }

    return null;
}

From source file:br.com.sose.utils.BigGridDemo_temp.java

License:Apache License

/**
 * Create a library of cell styles./*from   ww w .jav a2s  .  co m*/
 */
private static Map<String, XSSFCellStyle> createStyles(XSSFWorkbook wb) {
    Map<String, XSSFCellStyle> styles = new HashMap<String, XSSFCellStyle>();
    XSSFDataFormat fmt = wb.createDataFormat();

    XSSFCellStyle style1 = wb.createCellStyle();
    style1.setAlignment(XSSFCellStyle.ALIGN_RIGHT);
    style1.setDataFormat(fmt.getFormat("0.0%"));
    styles.put("percent", style1);

    XSSFCellStyle style2 = wb.createCellStyle();
    style2.setAlignment(XSSFCellStyle.ALIGN_CENTER);
    style2.setDataFormat(fmt.getFormat("0.0X"));
    styles.put("coeff", style2);

    XSSFCellStyle style3 = wb.createCellStyle();
    style3.setAlignment(XSSFCellStyle.ALIGN_RIGHT);
    style3.setDataFormat(fmt.getFormat("$#,##0.00"));
    styles.put("currency", style3);

    XSSFCellStyle style4 = wb.createCellStyle();
    style4.setAlignment(XSSFCellStyle.ALIGN_RIGHT);
    style4.setDataFormat(fmt.getFormat("mmm dd"));
    styles.put("date", style4);

    XSSFCellStyle style5 = wb.createCellStyle();
    XSSFFont headerFont = wb.createFont();
    headerFont.setBold(true);
    style5.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    style5.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
    style5.setFont(headerFont);
    styles.put("header", style5);

    return styles;
}

From source file:br.com.techne.gluonsoft.eowexport.builder.ExcelBuilder.java

License:Apache License

/**
 * cria estilos de celulas//  www.  ja v a2  s.c  o m
 */
private static HashMap<String, CellStyle> createStyles(XSSFWorkbook wb) {
    HashMap<String, CellStyle> styles = new HashMap<String, CellStyle>();

    XSSFCellStyle style;
    Font headerFont = wb.createFont();
    headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style = createBorderedStyle(wb);
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setFillForegroundColor(new XSSFColor(new Color(167, 191, 222)));
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setFont(headerFont);
    styles.put("header", style);

    style = createBorderedStyle(wb);

    style.setAlignment(CellStyle.ALIGN_LEFT);
    style.setWrapText(false);
    style.setFillForegroundColor(new XSSFColor(new Color(211, 223, 238)));
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    styles.put("cell_normal_even", style);

    style = createBorderedStyle(wb);
    style.setAlignment(CellStyle.ALIGN_LEFT);
    style.setWrapText(false);
    style.setFillForegroundColor(new XSSFColor(new Color(237, 242, 248)));
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    styles.put("cell_normal_odd", style);

    return styles;
}

From source file:ch.admin.isb.hermes5.business.userszenario.projektstrukturplan.ProjektstrukturplanGeneratorExcel.java

License:Apache License

private XSSFCellStyle getErgebnisStyle(XSSFWorkbook wb) {
    XSSFCellStyle cellStyle = wb.createCellStyle();
    XSSFFont font = wb.createFont();
    font.setItalic(true);//from w w w  .  ja  v  a2 s  . c o m
    font.setFontHeight(10);
    font.setFontName(fontName.getStringValue());
    cellStyle.setFont(font);
    return cellStyle;
}

From source file:ch.admin.isb.hermes5.business.userszenario.projektstrukturplan.ProjektstrukturplanGeneratorExcel.java

License:Apache License

private XSSFCellStyle getModulStyle(XSSFWorkbook wb) {
    XSSFCellStyle cellStyle = wb.createCellStyle();
    XSSFFont font = wb.createFont();
    font.setBold(true);//  w  w w .j a  v  a 2s  .c o  m
    font.setFontHeight(10);
    font.setFontName(fontName.getStringValue());
    cellStyle.setFont(font);
    return cellStyle;
}