exportex.ExportEx.java Source code

Java tutorial

Introduction

Here is the source code for exportex.ExportEx.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package exportex;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 *
 * @author 311015
 */
public class ExportEx {

    /**
     * @param args the command line arguments
     */
    static XSSFWorkbook workbook;
    static CellStyle style;
    static XSSFSheet sheet;
    static ArrayList<String[][]> List;

    public static void main(String[] args) {
        // TODO code application logic here
        //Create Workbook and Sheet
        workbook = new XSSFWorkbook();
        sheet = workbook.createSheet("Java Books");
        style = workbook.createCellStyle();

        List = new ArrayList<String[][]>();

        String[][] table = { { "Tech Number", "Name", "iPhone", "iPad", "iPod" },
                { "2273", "Vincent", "0", "0", "0" }, { "2273", "Vincent Esp", "0", "0", "0" },
                { "2273", "Vincent Espinoza", "0", "0", "0" }, { "2273", "Vincent Esp", "0", "0", "0" },
                { "2273", "Vincent Espinoza", "0", "0", "0" } };

        String[][] table2 = { { "Tech Number", "Name", "iPhone", "iPad", "iPod" },
                { "2277", "Vincent", "2", "3", "4" }, { "2277", "Vincent Esp", "8", "3", "5" },
                { "2277", "Vincent", "0", "0", "0" }, { "2nd Shift", "", "0", "0", "0" } };

        List.add(table);
        List.add(table2);
        List.add(table2);
        List.add(table2);
        List.add(table2);
        List.add(table);
        List.add(table2);
        List.add(table2);
        List.add(table2);
        List.add(table2);

        System.out.println("Creating excel");

        makeTables();

        //OUTPUT
        try (FileOutputStream outputStream = new FileOutputStream("JavaBooks.xlsx")) {
            workbook.write(outputStream);
            outputStream.close();
            System.out.println("Written successully");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    public static void cellFillBlue(CellStyle style) {
        style.setFillForegroundColor(IndexedColors.AQUA.getIndex());
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);

    }

    public static void cellBorderBlack(CellStyle style) {

        style.setBorderBottom(CellStyle.BORDER_THIN);
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderLeft(CellStyle.BORDER_THIN);
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderRight(CellStyle.BORDER_THIN);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderTop(CellStyle.BORDER_THIN);
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());

    }

    public static void makeTables() {
        int counter = 0;
        int colNum = 1;
        int rowNum = 1;
        int tableLength = List.get(0)[0].length;
        int tableHeight = List.get(0).length;

        Iterator<String[][]> it = List.iterator();

        System.out.println(tableLength + " " + tableHeight);

        while (it.hasNext()) {
            for (String[] tableRow : it.next()) {

                Row row = sheet.createRow(rowNum++);

                for (String tCell : tableRow) {

                    Cell cell = row.createCell(colNum++);

                    cell.setCellValue(tCell);
                    //cell.setCellStyle(style);
                    cellBorderBlack(style);
                    cellFillBlue(style);
                    cell.setCellStyle(style);

                }

                colNum = 1;
            }
            rowNum++;
        }

    }

}