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

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

Introduction

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

Prototype

@Override
public XSSFSheet createSheet() 

Source Link

Document

Create an XSSFSheet for this workbook, adds it to the sheets and returns the high level representation.

Usage

From source file:Viewsale.java

private void writeToExcel() throws FileNotFoundException, IOException {
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet ws = wb.createSheet();
    TreeMap<String, Object[]> data = new TreeMap<>();
    data.put("-1",
            new Object[] { dm.getColumnName(0), dm.getColumnName(1), dm.getColumnName(2), dm.getColumnName(3),
                    dm.getColumnName(4), dm.getColumnName(5), dm.getColumnName(6), dm.getColumnName(7),
                    dm.getColumnName(8), dm.getColumnName(9), dm.getColumnName(10), dm.getColumnName(11),
                    dm.getColumnName(12), dm.getColumnName(13), dm.getColumnName(14), dm.getColumnName(15) });
    for (int i = 0; i < dm.getRowCount(); i++) {

        data.put(Integer.toString(i),
                new Object[] { getCellValue(i, 0), getCellValue(i, 1), getCellValue(i, 2), getCellValue(i, 3),
                        getCellValue(i, 4), getCellValue(i, 5), getCellValue(i, 6), getCellValue(i, 7),
                        getCellValue(i, 8), getCellValue(i, 9), getCellValue(i, 10), getCellValue(i, 11),
                        getCellValue(i, 12), getCellValue(i, 13), getCellValue(i, 14), getCellValue(i, 15) });

    }/*from   w  w  w  . j  a  v  a  2 s . c om*/
    Set<String> ids = data.keySet();

    XSSFRow row;
    int rowID = 0;
    for (String key : ids) {
        row = ws.createRow(rowID++);
        Object[] values = data.get(key);
        int cellID = 0;
        for (Object o : values) {
            XSSFCell cell = row.createCell(cellID++);
            cell.setCellValue(o.toString());
        }
    }

    FileOutputStream fos = new FileOutputStream(new File("D:/motors/saleview.xlsx"));
    wb.write(fos);
    fos.close();

}

From source file:spareexcel.java

private void writeToExcel() throws FileNotFoundException, IOException {
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet ws = wb.createSheet();
    TreeMap<String, Object[]> data = new TreeMap<>();
    data.put("-1", new Object[] { dm.getColumnName(0), dm.getColumnName(1), dm.getColumnName(2),
            dm.getColumnName(3), dm.getColumnName(4), dm.getColumnName(5), dm.getColumnName(6) });
    for (int i = 0; i < dm.getRowCount(); i++) {

        data.put(Integer.toString(i), new Object[] { getCellValue(i, 0), getCellValue(i, 1), getCellValue(i, 2),
                getCellValue(i, 3), getCellValue(i, 4), getCellValue(i, 5), getCellValue(i, 6) });

    }/*from  www  .ja v a2  s .c  om*/
    Set<String> ids = data.keySet();

    XSSFRow row;
    int rowID = 0;
    for (String key : ids) {
        row = ws.createRow(rowID++);
        Object[] values = data.get(key);
        int cellID = 0;
        for (Object o : values) {
            XSSFCell cell = row.createCell(cellID++);
            cell.setCellValue(o.toString());
        }
    }

    FileOutputStream fos = new FileOutputStream(new File("D:/motors/sparexcel.xlsx"));
    wb.write(fos);
    fos.close();

}

From source file:CreatePivotTable.java

License:Apache License

public static void main(String[] args) throws FileNotFoundException, IOException, InvalidFormatException {
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet = wb.createSheet();

    //Create some data to build the pivot table on
    setCellData(sheet);// www. ja  v  a  2s  .  c o m

    XSSFPivotTable pivotTable = sheet.createPivotTable(new AreaReference("A1:D4"), new CellReference("H5"));
    //Configure the pivot table
    //Use first column as row label
    pivotTable.addRowLabel(0);
    //Sum up the second column
    pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 1);
    //Set the third column as filter
    pivotTable.addColumnLabel(DataConsolidateFunction.AVERAGE, 2);
    //Add filter on forth column
    pivotTable.addReportFilter(3);

    FileOutputStream fileOut = new FileOutputStream("ooxml-pivottable.xlsx");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}

From source file:Viewservice.java

private void writeToExcel() throws FileNotFoundException, IOException {
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet ws = wb.createSheet();
    TreeMap<String, Object[]> data = new TreeMap<>();
    data.put("-1",
            new Object[] { dm.getColumnName(0), dm.getColumnName(1), dm.getColumnName(2), dm.getColumnName(3),
                    dm.getColumnName(4), dm.getColumnName(5), dm.getColumnName(6), dm.getColumnName(7),
                    dm.getColumnName(8), dm.getColumnName(9), dm.getColumnName(10), dm.getColumnName(11),
                    dm.getColumnName(12), dm.getColumnName(13), dm.getColumnName(14), dm.getColumnName(15),
                    dm.getColumnName(16), dm.getColumnName(17), dm.getColumnName(18) });
    for (int i = 0; i < dm.getRowCount(); i++) {

        data.put(Integer.toString(i),
                new Object[] { getCellValue(i, 0), getCellValue(i, 1), getCellValue(i, 2), getCellValue(i, 3),
                        getCellValue(i, 4), getCellValue(i, 5), getCellValue(i, 6), getCellValue(i, 7),
                        getCellValue(i, 8), getCellValue(i, 9), getCellValue(i, 10), getCellValue(i, 11),
                        getCellValue(i, 12), getCellValue(i, 13), getCellValue(i, 14), getCellValue(i, 15),
                        getCellValue(i, 16), getCellValue(i, 17), getCellValue(i, 18) });

    }//from   w ww.  j a  v a  2 s  . c  om
    Set<String> ids = data.keySet();

    XSSFRow row;
    int rowID = 0;
    for (String key : ids) {
        row = ws.createRow(rowID++);
        Object[] values = data.get(key);
        int cellID = 0;
        for (Object o : values) {
            XSSFCell cell = row.createCell(cellID++);
            cell.setCellValue(o.toString());
        }
    }

    FileOutputStream fos = new FileOutputStream(new File("D:/motors/serviceview.xlsx"));

    wb.write(fos);
    fos.close();

}

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();// w w w . ja  v a 2s  .  c  om
    }

    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  ww  w.j a  v a2s.  com*/
    }

    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();/*  w w  w.  j  a v a  2  s . c om*/
    }

    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:BUS.FileManager.java

public void exportToXLSXFile(String fileName) throws FileNotFoundException, IOException {
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet = wb.createSheet();
    List<LichThi> Data = LichThiManager.getInstance().getDsLichThi();
    int rowNum = 0;
    // set title/*from   ww  w  .  ja v  a2s .  c o  m*/
    Row row = sheet.createRow(rowNum++);
    Cell cell = row.createCell(0);
    cell.setCellValue("M Lp");
    cell = row.createCell(1);
    cell.setCellValue("Tn MH");
    cell = row.createCell(2);
    cell.setCellValue("SBD");
    cell = row.createCell(3);
    cell.setCellValue("Phng");
    cell = row.createCell(4);
    cell.setCellValue("Ngy");
    cell = row.createCell(5);
    cell.setCellValue("Ca");

    // set contents
    for (LichThi lt : Data) {
        row = sheet.createRow(rowNum++);
        int cellNum = 0;

        for (int i = 0; i < 6; i++) {
            cell = row.createCell(cellNum++);
            cell.setCellValue(lt.getValueAt(i));
        }
    }

    try (FileOutputStream fos = new FileOutputStream(new File(fileName))) {
        wb.write(fos);
        fos.close();
    }
}

From source file:chocanproject.MemberReportGUI.java

private void writeToExcel() {
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet ws = wb.createSheet();
    //Load data to Treemap
    TreeMap<String, Object[]> data = new TreeMap<>();
    //Add column headers
    data.put("-1",
            new Object[] { dm.getColumnName(0), dm.getColumnName(1), dm.getColumnName(2), dm.getColumnName(3),
                    dm.getColumnName(4), dm.getColumnName(5), dm.getColumnName(6), dm.getColumnName(7),
                    dm.getColumnName(8), dm.getColumnName(9), dm.getColumnName(10) });
    //Add rows and cells
    for (int i = 0; i < dm.getRowCount(); i++) {
        data.put(Integer.toString(i),
                new Object[] { getCellValue(i, 0), getCellValue(i, 1), getCellValue(i, 2), getCellValue(i, 3),
                        getCellValue(i, 4), getCellValue(i, 5), getCellValue(i, 6), getCellValue(i, 7),
                        getCellValue(i, 8), getCellValue(i, 9), getCellValue(i, 10) });
    }//from w ww  . j av  a 2s  .  c o  m
    //write to excel file
    Set<String> ids = data.keySet();
    XSSFRow row;
    int rowID = 0;
    for (String key : ids) {
        row = ws.createRow(rowID++);
        //get data as per key
        Object[] values = data.get(key);
        int cellID = 0;
        for (Object o : values) {
            Cell cell = row.createCell(cellID++);
            cell.setCellValue(o.toString());
        }
    }
    //write to filesystem
    try {
        String MName, fName;
        DateFormat dateFormat = new SimpleDateFormat("MM-dd-YYYY");
        Date date = new Date();
        String Cdate;
        MName = fn.toString();
        Cdate = (dateFormat.format(date));
        fName = MName + "_" + Cdate;
        FileOutputStream fos = new FileOutputStream(new File("D:/Excel/" + fName + ".xlsx"));
        wb.write(fos);
        fos.close();
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
        // Logger.getLogger(WorkBookNSheet.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        ex.printStackTrace();
        //Logger.getLogger(MemberReportGUI.class.getName()).log(Level.SEVERE, null, ex);

    }
}

From source file:chocanproject.ProviderReportGUI.java

private void writeToExcel() {
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet ws = wb.createSheet();
    //Load data to Treemap
    TreeMap<String, Object[]> data = new TreeMap<>();
    //Add column headers
    data.put("-1",
            new Object[] { dp.getColumnName(0), dp.getColumnName(1), dp.getColumnName(2), dp.getColumnName(3),
                    dp.getColumnName(4), dp.getColumnName(5), dp.getColumnName(6), dp.getColumnName(7),
                    dp.getColumnName(8), dp.getColumnName(9), dp.getColumnName(10), dp.getColumnName(11),
                    dp.getColumnName(12), dp.getColumnName(13) });
    //Add rows and cells
    for (int i = 0; i < dp.getRowCount(); i++) {
        data.put(Integer.toString(i),
                new Object[] { getCellValue(i, 0), getCellValue(i, 1), getCellValue(i, 2), getCellValue(i, 3),
                        getCellValue(i, 4), getCellValue(i, 5), getCellValue(i, 6), getCellValue(i, 7),
                        getCellValue(i, 8), getCellValue(i, 9), getCellValue(i, 10), getCellValue(i, 11),
                        getCellValue(i, 12), getCellValue(i, 13) });
    }//from  ww  w.  j  a  v  a 2s  .  c o  m
    //write to excel file
    Set<String> ids = data.keySet();
    XSSFRow row;
    int rowID = 0;
    for (String key : ids) {
        row = ws.createRow(rowID++);
        //get data as per key
        Object[] values = data.get(key);
        int cellID = 0;
        for (Object o : values) {
            Cell cell = row.createCell(cellID++);
            cell.setCellValue(o.toString());
        }
    }
    //write to filesystem
    try {
        String fName;
        DateFormat dateFormat = new SimpleDateFormat("MM-dd-YYYY");
        Date date = new Date();
        String Cdate;
        Cdate = (dateFormat.format(date));
        fName = pfn + "_" + Cdate;
        FileOutputStream fos = new FileOutputStream(new File("D:/Excel/" + fName + ".xlsx"));
        wb.write(fos);
        fos.close();
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
        // Logger.getLogger(WorkBookNSheet.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        ex.printStackTrace();
        //Logger.getLogger(MemberReportGUI.class.getName()).log(Level.SEVERE, null, ex);

    }
}