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:controller.DAORequest.java

public void saveResolution() {
    XSSFWorkbook reqWB = new XSSFWorkbook();
    XSSFSheet sheet = reqWB.createSheet();
    Row rowZ = sheet.createRow(0);/*  ww  w .ja  v a 2  s.  c  o m*/
    sheet.getRow(0).createCell(0).setCellValue("Id");
    sheet.getRow(0).createCell(1).setCellValue("Attention");
    sheet.getRow(0).createCell(2).setCellValue("Title");
    sheet.getRow(0).createCell(3).setCellValue("Intro");
    sheet.getRow(0).createCell(4).setCellValue("Result");
    sheet.getRow(0).createCell(5).setCellValue("Resolve");
    sheet.getRow(0).createCell(6).setCellValue("Notify");
    sheet.getRow(0).createCell(7).setCellValue("Considerations");
    ArrayList<Resolution> resolutions = new ArrayList<>();
    for (Object o : School.getInstance().selectAllRequests()) {
        Request r = (Request) o;
        if (r.getResolution() != null) {
            resolutions.add(r.getResolution());

        }
    }

    int rowI = 1;
    for (Resolution r : resolutions) {
        Row row = sheet.createRow(rowI);
        //
        Cell cellId = row.createCell(0);
        cellId.setCellValue(r.getId());
        Cell cellAttention = row.createCell(1);
        cellAttention.setCellValue(r.getAttention());
        Cell cellTitle = row.createCell(2);
        cellTitle.setCellValue(r.getTitle());
        Cell cellIntro = row.createCell(3);
        cellIntro.setCellValue(r.getIntro());
        Cell cellResult = row.createCell(4);
        cellResult.setCellValue(r.getResult());
        Cell cellResolve = row.createCell(5);
        cellResolve.setCellValue(r.getResolve());
        Cell cellNotify = row.createCell(6);
        cellNotify.setCellValue(r.getNotify());
        Cell cellCons = row.createCell(7);
        cellCons.setCellValue(r.getConsider());
        rowI++;

    }

    // Save to excel file 
    try {

        FileOutputStream out = new FileOutputStream(new File("src//files//DatosResolucion.xlsx"));

        reqWB.write(out);

        reqWB.close();
        out.close();

    } catch (FileNotFoundException ex) {

        Logger.getLogger(DAORequest.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {

        Logger.getLogger(DAORequest.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:edu.cmu.emfta.actions.CutSet.java

License:Open Source License

public XSSFWorkbook toSingleSheetWorkbook() {
    XSSFWorkbook workbook = new XSSFWorkbook();
    int cutSetIdentifier = 0;
    double cutsetProbability;

    XSSFSheet sheet = workbook.createSheet();

    XSSFTable table = sheet.createTable();
    table.setDisplayName("Cutsets");
    CTTable cttable = table.getCTTable();

    // Set which area the table should be placed in
    AreaReference reference = new AreaReference(new CellReference(0, 0), new CellReference(2, 2));
    cttable.setRef(reference.formatAsString());
    cttable.setId((long) 1);
    cttable.setName("Cutsets");
    cttable.setTotalsRowCount((long) 1);

    CTTableColumns columns = cttable.addNewTableColumns();
    columns.setCount((long) 3);
    CTTableColumn column;//from   ww w. j a  v  a 2 s . c  o  m
    XSSFRow row;
    XSSFCell cell;

    column = columns.addNewTableColumn();

    // Create row
    row = sheet.createRow(0);
    CellStyle headingCellStyle = workbook.createCellStyle();
    XSSFFont headingFont = workbook.createFont();
    headingFont.setBold(true);
    headingCellStyle.setFont(headingFont);
    row.setRowStyle(headingCellStyle);

    CellStyle normalCellStyle = workbook.createCellStyle();
    XSSFFont normalFont = workbook.createFont();
    normalFont.setBold(false);
    normalCellStyle.setFont(normalFont);

    for (int j = 0; j < 3; j++) {
        // Create cell
        cell = row.createCell(j);

        switch (j) {
        case 0: {
            cell.setCellValue("Identifier");
            break;
        }
        case 1: {
            cell.setCellValue("Description");
            break;
        }
        case 2: {
            cell.setCellValue("Probability");
            break;
        }

        }
    }

    int rowId = 1;

    for (List<Event> events : cutset) {
        row = sheet.createRow(rowId++);
        row = sheet.createRow(rowId++);
        row.setRowStyle(normalCellStyle);

        cell = row.createCell(0);
        cell.setCellValue("Cutset #" + cutSetIdentifier);

        cutsetProbability = 1;
        for (int i = 0; i < events.size(); i++) {
            cutsetProbability = cutsetProbability * events.get(i).getProbability();
        }

        cell = row.createCell(2);
        if (cutsetProbability != 1) {
            cell.setCellValue("" + cutsetProbability);
        } else {
            cell.setCellValue("" + cutsetProbability);
        }
        //         System.out.println("[CutSet] cutset id=" + cutSetIdentifier);

        for (int i = 0; i < events.size(); i++) {
            Event e = events.get(i);

            //            System.out.println("[CutSet] event name=" + e.getName());

            // Create row
            row = sheet.createRow(rowId++);
            row.setRowStyle(normalCellStyle);
            for (int j = 0; j < 3; j++) {
                // Create cell
                cell = row.createCell(j);

                switch (j) {
                case 0: {
                    cell.setCellValue(e.getName());
                    break;
                }
                case 1: {
                    cell.setCellValue(e.getDescription());
                    break;
                }
                case 2: {
                    cell.setCellValue(e.getProbability());
                    break;
                }

                }
            }
        }
        cutSetIdentifier = cutSetIdentifier + 1;
    }

    return workbook;

}

From source file:edu.cmu.emfta.actions.CutSet.java

License:Open Source License

public XSSFWorkbook toMultiSheetsWorkbook() {
    XSSFWorkbook workbook = new XSSFWorkbook();
    int cutSetIdentifier = 0;
    double cutsetProbability;

    for (List<Event> events : cutset) {

        cutsetProbability = 1;/*from w w  w. j a v a2  s.  c o m*/
        for (int i = 0; i < events.size(); i++) {
            cutsetProbability = cutsetProbability * events.get(i).getProbability();
        }

        //         System.out.println("[CutSet] cutset id=" + cutSetIdentifier);
        XSSFSheet sheet = workbook.createSheet();

        XSSFTable table = sheet.createTable();
        table.setDisplayName("Cutset");
        CTTable cttable = table.getCTTable();

        // Set which area the table should be placed in
        AreaReference reference = new AreaReference(new CellReference(0, 0), new CellReference(2, 2));
        cttable.setRef(reference.formatAsString());
        cttable.setId((long) 1);
        cttable.setName("Cutset " + cutSetIdentifier);
        cttable.setTotalsRowCount((long) 1);

        CTTableColumns columns = cttable.addNewTableColumns();
        columns.setCount((long) 3);
        CTTableColumn column;
        XSSFRow row;
        XSSFCell cell;

        column = columns.addNewTableColumn();

        // Create row
        row = sheet.createRow(0);
        CellStyle headingCellStyle = workbook.createCellStyle();
        XSSFFont headingFont = workbook.createFont();
        headingFont.setBold(true);
        headingCellStyle.setFont(headingFont);
        row.setRowStyle(headingCellStyle);

        CellStyle normalCellStyle = workbook.createCellStyle();
        XSSFFont normalFont = workbook.createFont();
        normalFont.setBold(false);
        normalCellStyle.setFont(normalFont);

        for (int j = 0; j < 3; j++) {
            // Create cell
            cell = row.createCell(j);

            switch (j) {
            case 0: {
                cell.setCellValue("Identifier");
                break;
            }
            case 1: {
                cell.setCellValue("Description");
                break;
            }
            case 2: {
                if (cutsetProbability == 1) {
                    cell.setCellValue("Probability");
                } else {
                    cell.setCellValue("Probability (" + cutsetProbability + ")");
                }
                break;
            }

            }
        }

        for (int i = 0; i < events.size(); i++) {
            Event e = events.get(i);

            System.out.println("[CutSet] event name=" + e.getName());
            // Create column
            column = columns.addNewTableColumn();
            column.setName("Column");
            column.setId((long) i + 1);
            // Create row
            row = sheet.createRow(i + 1);
            row.setRowStyle(normalCellStyle);
            for (int j = 0; j < 3; j++) {
                // Create cell
                cell = row.createCell(j);

                switch (j) {
                case 0: {
                    cell.setCellValue(e.getName());
                    break;
                }
                case 1: {
                    cell.setCellValue(e.getDescription());
                    break;
                }
                case 2: {
                    cell.setCellValue(e.getProbability());
                    break;
                }

                }
            }
        }
        cutSetIdentifier = cutSetIdentifier + 1;
    }

    return workbook;

}

From source file:exportToExcel.ResultSetToExcelTest.java

License:Open Source License

public void Test() {

    XSSFWorkbook xlsxWorkbook = new XSSFWorkbook();
    XSSFSheet xlsSheet = xlsxWorkbook.createSheet();
    short rowIndex = 1;

    ResultSet rs = null;//from  w w w  .  j  a  v  a2  s  . c o  m

    try {

        //Get the list of column names and store them as the first
        //row of the spreadsheet.
        ResultSetMetaData colInfo = rs.getMetaData();
        ArrayList<String> colNames = new ArrayList<>();
        XSSFRow titleRow = xlsSheet.createRow(rowIndex++);

        for (int i = 1; i <= colInfo.getColumnCount(); i++) {

            colNames.add(colInfo.getColumnName(i));
            titleRow.createCell((short) (i - 1)).setCellValue(new XSSFRichTextString(colInfo.getColumnName(i)));
            xlsSheet.setColumnWidth((short) (i - 1), (short) 4000);

        }

        //Save all the data from the database table rows

        while (rs.next()) {

            XSSFRow dataRow = xlsSheet.createRow(rowIndex++);

            short colIndex = 0;

            for (String colName : colNames) {
                dataRow.createCell(colIndex++).setCellValue(new XSSFRichTextString(rs.getString(colName)));
            }
        }

        //Write to disk

        xlsxWorkbook.write(new FileOutputStream("data.xlsx"));

    } catch (SQLException | IOException e) {

        System.exit(1);

    }

}

From source file:forme.FrmPocetna.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet ws = wb.createSheet();

    TreeMap<String, Object[]> data = new TreeMap<>();
    TblModelPrikazUcinaka dtm = (TblModelPrikazUcinaka) jtblUcinci.getModel();

    Object[] array = new Object[dtm.getColumnCount()];
    for (int i = 0; i < dtm.getColumnCount(); i++) {
        array[i] = dtm.getColumnName(i);
    }//from  w  ww. ja v  a2  s  .c o m
    data.put("0", array);

    for (int i = 0; i < dtm.getRowCount(); i++) {
        Object[] niz = new Object[dtm.getColumnCount()];
        for (int j = 0; j < dtm.getColumnCount(); j++) {
            niz[j] = dtm.getValueAt(i, j);
        }
        int rb = i + 1;
        data.put("" + rb, niz);
    }

    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) {
            Cell cell = row.createCell(cellID++);
            cell.setCellValue(o.toString());
        }

    }

    try {
        FileOutputStream out = new FileOutputStream(new File("../Export/"
                + ((Utakmica) jlistUtakmice.getSelectedValue()).getDomacin().getNaziv()
                + ((Utakmica) jlistUtakmice.getSelectedValue()).getGost().getNaziv()
                + ((Utakmica) jlistUtakmice.getSelectedValue()).getDatumOdigravanja().toString() + ".xlsx"));
        wb.write(out);
        out.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(FrmPocetna.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(FrmPocetna.class.getName()).log(Level.SEVERE, null, ex);
    }

    JOptionPane.showMessageDialog(this, "Excel fajl uspeno sa?uvan na vaem kompjuteru!",
            "Preuzimanje dovreno", JOptionPane.INFORMATION_MESSAGE);
    try {
        Runtime.getRuntime().exec("cmd.exe /C start ../Export/"
                + ((Utakmica) jlistUtakmice.getSelectedValue()).getDomacin().getNaziv()
                + ((Utakmica) jlistUtakmice.getSelectedValue()).getGost().getNaziv()
                + ((Utakmica) jlistUtakmice.getSelectedValue()).getDatumOdigravanja().toString() + ".xlsx");
        System.out.println("Otvorio");
    } catch (IOException ex) {
        Logger.getLogger(FrmPocetna.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:Import.Utils.XSSFConvert.java

public static XSSFWorkbook convertWorkbookHSSFToXSSF(HSSFWorkbook source) {
    XSSFWorkbook retVal = new XSSFWorkbook();
    for (int i = 0; i < source.getNumberOfSheets(); i++) {
        XSSFSheet xssfSheet = retVal.createSheet();
        HSSFSheet hssfsheet = source.getSheetAt(i);
        copySheets(hssfsheet, xssfSheet);
    }//from  w w  w  .j ava 2 s .c o m
    return retVal;
}

From source file:info.informationsea.tableio.excel.test.ExcelSheetWriterTest.java

License:Open Source License

@Test
public void testWriter() throws Exception {
    XSSFWorkbook workbook = new XSSFWorkbook();
    ExcelSheetWriter excelSheetWriter = new ExcelSheetWriter(workbook.createSheet());
    excelSheetWriter.printAll(Arrays.asList(data));

    ExcelSheetReader excelSheetReader = new ExcelSheetReader(workbook.getSheetAt(0));
    commonAssert(excelSheetReader);// w w  w .  j  a v a  2  s. c  o  m
}

From source file:Interface.interemploi.java

private void writetoxel() {

    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet ws = wb.createSheet();

    TreeMap<String, Object[]> data = new TreeMap<>();
    data.put("0",
            new Object[] { mod.getColumnName(0), mod.getColumnName(1), mod.getColumnName(2),
                    mod.getColumnName(3), mod.getColumnName(4), mod.getColumnName(5), mod.getColumnName(6),
                    mod.getColumnName(7), mod.getColumnName(8), mod.getColumnName(9), mod.getColumnName(10),
                    mod.getColumnName(11), mod.getColumnName(12), mod.getColumnName(13), mod.getColumnName(14),
                    mod.getColumnName(15), mod.getColumnName(16), mod.getColumnName(17), mod.getColumnName(18),
                    mod.getColumnName(19) });
    int nb = mod.getRowCount();
    int s = 0;/*from  w  w  w.j a  v a 2s  .c o m*/
    for (int i = 1; i <= nb; i++) {
        data.put(Integer.toString(i),
                new Object[] { getvlue(s, 0), getvlue(s, 1), getvlue(s, 2), getvlue(s, 3), getvlue(s, 4),
                        getvlue(s, 5), getvlue(s, 6), getvlue(s, 7), getvlue(s, 8), getvlue(s, 9),
                        getvlue(s, 10), getvlue(s, 11), getvlue(s, 12), getvlue(s, 13), getvlue(s, 14),
                        getvlue(s, 15), getvlue(s, 16), getvlue(s, 17), getvlue(s, 18), getvlue(s, 19) });

        s++;
    }
    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) {
            Cell cell = row.createCell(cellID++);
            cell.setCellValue(o.toString());
        }
    }

    try {
        FileOutputStream fs;

        fs = new FileOutputStream(new File("bdd_Fonctionaire.xlsx"));
        wb.write(fs);
        fs.close();
        JOptionPane.showConfirmDialog(null, "Votre Base De Donnee Est Bien Expoter", "Valider",
                JOptionPane.CLOSED_OPTION);
        Desktop desktop = Desktop.getDesktop();
        if (desktop.isSupported(Desktop.Action.OPEN)) {
            desktop.open(new File("bdd_Fonctionaire.xlsx"));
        } else {
            System.out.println("Open is not supported");
        }
    } catch (FileNotFoundException ex) {
        System.out.println("eruer fichier");
    } catch (IOException ex) {
        System.out.println("eruer fichier");
    }

}

From source file:Interface.interProf.java

private void writetoxel() {

    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet ws = wb.createSheet();

    TreeMap<String, Object[]> data = new TreeMap<>();
    data.put("0",
            new Object[] { mod.getColumnName(0), mod.getColumnName(1), mod.getColumnName(2),
                    mod.getColumnName(3), mod.getColumnName(4), mod.getColumnName(5), mod.getColumnName(6),
                    mod.getColumnName(7), mod.getColumnName(8), mod.getColumnName(9), mod.getColumnName(10),
                    mod.getColumnName(11), mod.getColumnName(12), mod.getColumnName(13), mod.getColumnName(14),
                    mod.getColumnName(15), mod.getColumnName(16), mod.getColumnName(17), mod.getColumnName(18),
                    mod.getColumnName(19), mod.getColumnName(20) });
    int nb = mod.getRowCount();
    int s = 0;/*from ww  w.  ja v a 2s .c  o m*/
    for (int i = 1; i <= nb; i++) {
        data.put(Integer.toString(i), new Object[] { getvlue(s, 0), getvlue(s, 1), getvlue(s, 2), getvlue(s, 3),
                getvlue(s, 4), getvlue(s, 5), getvlue(s, 6), getvlue(s, 7), getvlue(s, 8), getvlue(s, 9),
                getvlue(s, 10), getvlue(s, 11), getvlue(s, 12), getvlue(s, 13), getvlue(s, 14), getvlue(s, 15),
                getvlue(s, 16), getvlue(s, 17), getvlue(s, 18), getvlue(s, 19), getvlue(s, 20) });

        s++;
    }
    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) {
            Cell cell = row.createCell(cellID++);
            cell.setCellValue(o.toString());
        }
    }

    try {
        FileOutputStream fs;

        fs = new FileOutputStream(new File("bdd_prof.xlsx"));
        wb.write(fs);
        fs.close();
        JOptionPane.showConfirmDialog(null, "Votre Base De Donnee Est Bien Expoter", "Valider",
                JOptionPane.CLOSED_OPTION);

        Desktop desktop = Desktop.getDesktop();
        if (desktop.isSupported(Desktop.Action.OPEN)) {
            desktop.open(new File("bdd_prof.xlsx"));
        } else {
            System.out.println("Open is not supported");
        }

    } catch (FileNotFoundException ex) {
        System.out.println("eruer fichier");
    } catch (IOException ex) {
        System.out.println("eruer fichier");
    }

}

From source file:ke.co.tawi.babblesms.server.servlet.export.excel.inbox.ExportExcel.java

License:Open Source License

/**
 * Returns MS Excel file of the data specified for exporting.
 * @param List<IncomingLog>/*from w ww . jav a2s. co  m*/
 * Method create excelSheets and sends them
 ****/
public void createExcelSheets(List<IncomingLog> InLog) throws IOException {
    List<Phone> phoneList;
    //String cont = null;

    XSSFWorkbook xf = new XSSFWorkbook();
    XSSFCreationHelper ch = xf.getCreationHelper();

    XSSFSheet s = xf.createSheet();
    //create the first row
    XSSFRow r1 = s.createRow(0);
    XSSFCell c11 = r1.createCell(0);
    c11.setCellValue(ch.createRichTextString("*"));
    XSSFCell c12 = r1.createCell(1);
    c12.setCellValue(ch.createRichTextString("Message"));
    XSSFCell c13 = r1.createCell(2);
    c13.setCellValue(ch.createRichTextString("Source"));
    XSSFCell c14 = r1.createCell(3);
    c14.setCellValue(ch.createRichTextString("Destination"));
    XSSFCell c15 = r1.createCell(4);
    c15.setCellValue(ch.createRichTextString("Network"));
    XSSFCell c16 = r1.createCell(5);
    c16.setCellValue(ch.createRichTextString("Time (" + timezoneFormatter.format(new Date()) + ") Time Zone"));
    XSSFCell c17 = r1.createCell(6);
    c17.setCellValue(ch.createRichTextString("Message Id"));

    int i = 1;
    //create other rows
    for (IncomingLog log : InLog) {
        phoneList = phnDAO.getPhones(log.getOrigin());

        XSSFRow r = s.createRow(i);
        //row number
        XSSFCell c1 = r.createCell(0);
        c1.setCellValue(i + pageno);

        //get message  
        XSSFCell c2 = r.createCell(1);
        c2.setCellValue(ch.createRichTextString(log.getMessage()));

        //get phone numbers
        XSSFCell c3 = r.createCell(2);
        if (phoneList.size() > 0) {
            for (Phone phone : phoneList) {
                Contact contacts = ctDAO.getContact(phone.getContactUuid());
                c3.setCellValue(ch.createRichTextString(contacts.getName()));
            }
        } else {
            c3.setCellValue(ch.createRichTextString(log.getOrigin()));
        }

        //get destination   
        XSSFCell c4 = r.createCell(3);
        c4.setCellValue(ch.createRichTextString(log.getDestination()));

        //get network name    
        XSSFCell c5 = r.createCell(4);
        c5.setCellValue(ch.createRichTextString(networkHash.get(log.getNetworkUuid())));

        //get date 
        XSSFCell c6 = r.createCell(5);
        c6.setCellValue(ch.createRichTextString("" + dateFormatter.format(log.getLogTime())));

        //get message id
        XSSFCell c7 = r.createCell(6);
        c7.setCellValue(ch.createRichTextString(log.getUuid()));
        i++;

    }
    xf.write(out);
    out.flush();
    out.close();
}