Example usage for org.apache.poi.ss.usermodel WorkbookFactory WorkbookFactory

List of usage examples for org.apache.poi.ss.usermodel WorkbookFactory WorkbookFactory

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel WorkbookFactory WorkbookFactory.

Prototype

WorkbookFactory

Source Link

Usage

From source file:com.mechatronika.trackmchtr.ExportToExcel.java

private static void writeToExcel(String path) throws FileNotFoundException, IOException {
    new WorkbookFactory();
    Workbook wb = new XSSFWorkbook(); //Excell workbook
    Sheet sheet = wb.createSheet(); //WorkSheet //wb.createSheet("sheetName");
    Row row = sheet.createRow(2); //Row created at line 3
    TableModel model = table.getModel(); //Table model

    Row headerRow = sheet.createRow(0); //Create row at line 0
    for (int headings = 0; headings < model.getColumnCount(); headings++) { //For each column
        headerRow.createCell(headings).setCellValue(model.getColumnName(headings));//Write column name
    }//from   ww w .  j a  va  2  s.  c o m

    for (int rows = 0; rows < model.getRowCount(); rows++) { //For each table row
        for (int cols = 0; cols < table.getColumnCount(); cols++) { //For each table column
            row.createCell(cols).setCellValue(model.getValueAt(rows, cols).toString()); //Write value
        }

        //Set the row to the next one in the sequence 
        row = sheet.createRow((rows + 3));
    }
    wb.write(new FileOutputStream(path));//Save the file  
    IJ.showMessage("Excel file created!");
}

From source file:cs.handmail.processtable.ExportExcel.java

public void export() {
    try {//w ww. j  av  a 2  s .  com
        new WorkbookFactory();
        Workbook wb = new XSSFWorkbook(); //Excell workbook
        Sheet sheet = wb.createSheet(); //WorkSheet
        Row row = sheet.createRow(2); //Row created at line 3
        TableModel model = _tableExport.getModel();
        String temp;

        Row headerRow = sheet.createRow(0); //Create row at line 0
        for (int headings = 0; headings < model.getColumnCount(); headings++) {
            headerRow.createCell(headings).setCellValue(model.getColumnName(headings));//Write column name
        }

        for (int rows = 0; rows < model.getRowCount(); rows++) { //For each table row
            for (int cols = 0; cols < _tableExport.getColumnCount(); cols++) { //For each table column
                if (model.getValueAt(rows, cols) != null)
                    temp = model.getValueAt(rows, cols).toString();
                else
                    temp = "";
                row.createCell(cols).setCellValue(temp);
            }

            //Set the row to the next one in the sequence
            row = sheet.createRow((rows + 3));
        }
        wb.write(new FileOutputStream(_pathFolder + "/" + _nameFile));//Save the file    
        JOptionPane.showMessageDialog(null, "Save file Success");
    } catch (IOException ex) {
        Logger.getLogger(ExportExcel.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:View.SummaryFrame.java

private static void importToExcel(JTable table, Path path) throws FileNotFoundException, IOException {
    new WorkbookFactory();
    Workbook wb = new XSSFWorkbook(); //Excel workbook
    Sheet sheet = wb.createSheet(); //WorkSheet
    Row row = sheet.createRow(2); //Row created at line 3
    TableModel model = table.getModel(); //Table model

    Row headerRow = sheet.createRow(0); //Create row at line 0
    for (int headings = 0; headings < model.getColumnCount(); headings++) { //For each column
        headerRow.createCell(headings).setCellValue(model.getColumnName(headings));//Write column name
    }//  w  w w  .j ava 2 s .c om

    for (int rows = 0; rows < model.getRowCount(); rows++) { //For each table row
        for (int cols = 0; cols < table.getColumnCount(); cols++) { //For each table column
            row.createCell(cols).setCellValue(String.valueOf(model.getValueAt(rows, cols)));
            //row.createCell(cols).setCellValue(model.getValueAt(rows, cols).toString()); //Write value
        }

        //Set the row to the next one in the sequence 
        row = sheet.createRow((rows + 3));
    }
    wb.write(new FileOutputStream(path.toString()));//Save the file     
}