Example usage for org.apache.poi.ss.usermodel Workbook createSheet

List of usage examples for org.apache.poi.ss.usermodel Workbook createSheet

Introduction

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

Prototype

Sheet createSheet(String sheetname);

Source Link

Document

Create a new sheet for this Workbook and return the high level representation.

Usage

From source file:http.RequestHeadersSpreadsheetServlet.java

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    // create workbook and sheet for spreadsheet
    Workbook workbook = new HSSFWorkbook();
    Sheet sheet = workbook.createSheet("Request Headers");

    Enumeration headerNames = request.getHeaderNames();
    int i = 0;//from   w ww. j  a  v a  2 s .  c  o m
    while (headerNames.hasMoreElements()) {
        String name = (String) headerNames.nextElement();
        String value = request.getHeader(name);

        // create the row and store data in its cells
        Row row = sheet.createRow(i);
        row.createCell(0).setCellValue(name);
        row.createCell(1).setCellValue(value);
        i++;
    }

    // set the response headers to return an attached .xls file
    response.setHeader("content-disposition", "attachment; filename=request_headers.xls");
    response.setHeader("cache-control", "no-cache");

    // get the output stream and send the workbook to the browser
    OutputStream out = response.getOutputStream();
    workbook.write(out);
    out.close();
}

From source file:info.informationsea.java.excel2csv.Utilities.java

License:Open Source License

public static Sheet createUniqueNameSheetForWorkbook(Workbook workbook, String sheetName, boolean overwrite) {
    if (overwrite) {
        int index = workbook.getSheetIndex(workbook.getSheet(sheetName));
        if (index >= 0)
            workbook.removeSheetAt(index);
        return workbook.createSheet(sheetName);
    }/*w  ww . j  a  v  a 2s  . c o  m*/

    String realSheetName = sheetName;
    int index = 1;
    Sheet sheet;
    while (true) {
        try {
            sheet = workbook.createSheet(realSheetName);
            break;
        } catch (IllegalArgumentException e) {
            realSheetName = sheetName + "-" + index++;
            if (index > 20) {
                throw e;
            }
        }
    }
    return sheet;
}

From source file:info.informationsea.venn.CombinationImporterExporter.java

License:Open Source License

public static <T, U> void export(File file, CombinationSolver<T, U> combinationSolver,
        List<VennFigureParameters.Attribute<T>> keyList) throws Exception {
    if (file.getName().endsWith(".csv")) {
        try (FileWriter writer = new FileWriter(file); TableCSVWriter csvWriter = new TableCSVWriter(writer)) {
            export(csvWriter, combinationSolver, keyList);
        }//  w  w w.  ja  va2  s. c o m
    } else if (file.getName().endsWith(".xlsx") || file.getName().endsWith(".xls")) {
        Workbook workbook;
        if (file.getName().endsWith(".xlsx"))
            workbook = new SXSSFWorkbook();
        else
            workbook = new HSSFWorkbook();

        // Export dataset
        Sheet sheet = workbook.createSheet("Data");
        try (ExcelSheetWriter writer = new ExcelSheetWriter(sheet)) {
            writer.setPrettyTable(true);
            export(writer, combinationSolver, keyList);
        }

        // Export combinations
        sheet = workbook.createSheet("Combinations");
        try (ExcelSheetWriter writer = new ExcelSheetWriter(sheet)) {
            writer.setPrettyTable(false);
            writer.setEnableHeaderStyle(true);
            writer.setAlternativeBackground(true);

            writer.printRecord("Combination", "# of items", "items");

            for (Map.Entry<Set<T>, Set<U>> entry : combinationSolver.getCombinationResult().entrySet()) {
                String keys = "";
                for (T one : entry.getKey()) {
                    if (keys.length() > 0)
                        keys += ", ";
                    keys += one.toString();
                }

                List<Object> row = new ArrayList<>();
                row.add(keys);
                row.add(entry.getValue().size());

                for (U one : entry.getValue()) {
                    row.add(one.toString());
                }

                writer.printRecord(row.toArray());
            }
        }

        // Export venn diagram
        switch (combinationSolver.getValues().size()) {
        case 2:
        case 3:
        case 4:
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            VennExporter.exportAsPNG(new VennFigureParameters<T>(combinationSolver, keyList), outputStream, 800,
                    10);
            outputStream.close();

            sheet = workbook.createSheet("Venn");
            try (ExcelImageSheetWriter imageSheetWriter = new ExcelImageSheetWriter(sheet)) {
                imageSheetWriter.addImage(ImageSheetWriter.ImageType.TYPE_PNG, outputStream.toByteArray());
            }
            break;
        default: // Do nothing
            break;
        }

        try (FileOutputStream fos = new FileOutputStream(file)) {
            workbook.write(fos);
        }

    } else {
        throw new IllegalArgumentException("Unsupported file type");
    }
}

From source file:instancegenerator.ExcelGenerator.java

public static void makeInstancesN100S1() {
    int weight = 0;
    int capacity = 0;
    int cost = 0;
    int[] items = new int[100];
    try {/*from   ww  w.j  av a  2  s. co  m*/
        //**************** Class A ***************************************************
        //instance A1
        FileOutputStream fileout = new FileOutputStream(new File("src/instances/Set1/N100/N100A1.xls"));
        Workbook ficheroWb = new HSSFWorkbook();
        Sheet sheet = ficheroWb.createSheet("Instance A1 for VSBPP");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        //for the items
        int reqCapacity = 0;
        for (int i = 1; i <= 100; i++) {
            weight = (int) randVal(1, 20);
            items[i - 1] = weight;
            reqCapacity += weight;
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(weight);
        }
        // for the bins
        int cap = 100;
        int j = 1;
        for (int i = 1; i <= 3; i++) {
            int availableCap = 0;
            capacity = cap * i;
            cost = (int) (100 * Math.sqrt(capacity));
            while (availableCap < reqCapacity) {
                row = sheet.getRow(j);
                if (row != null) {
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                } else {
                    row = sheet.createRow(j);
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                }
                j++;
                availableCap = availableCap + capacity;
            }
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

        //instance A2
        fileout = new FileOutputStream(new File("src/instances/Set1/N100/N100A2.xls"));
        ficheroWb = new HSSFWorkbook();
        sheet = ficheroWb.createSheet("Instance A2 for VSBPP");
        row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        // for the items
        for (int i = 1; i <= 100; i++) {
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(items[i - 1]);
        }
        //for the bins
        cap = 50;
        j = 1;
        for (int i = 1; i <= 6; i++) {
            int availableCap = 0;
            capacity = cap * i;
            cost = (int) (100 * Math.sqrt(capacity));
            while (availableCap < reqCapacity) {
                row = sheet.getRow(j);
                if (row != null) {
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                } else {
                    row = sheet.createRow(j);
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                }
                j++;
                availableCap = availableCap + capacity;
            }
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

        //instance A3
        fileout = new FileOutputStream(new File("src/instances/Set1/N100/N100A3.xls"));
        ficheroWb = new HSSFWorkbook();
        sheet = ficheroWb.createSheet("Instance A3 for VSBPP");
        row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        // for the items
        for (int i = 1; i <= 100; i++) {
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(items[i - 1]);
        }
        //for the bins
        cap = 25;
        j = 1;
        for (int i = 1; i <= 12; i++) {
            int availableCap = 0;
            capacity = cap * i;
            cost = (int) (100 * Math.sqrt(capacity));
            while (availableCap < reqCapacity) {
                row = sheet.getRow(j);
                if (row != null) {
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                } else {
                    row = sheet.createRow(j);
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                }
                j++;
                availableCap = availableCap + capacity;
            }
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

        //instance A4
        fileout = new FileOutputStream(new File("src/instances/Set1/N100/N100A4.xls"));
        ficheroWb = new HSSFWorkbook();
        sheet = ficheroWb.createSheet("Instance A4 for VSBPP");
        row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        // for the items
        for (int i = 1; i <= 100; i++) {
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(items[i - 1]);
        }
        //for the bins
        cap = 60;
        j = 1;
        for (int i = 1; i <= 55; i++) {
            capacity = cap;
            cost = (int) (100 * Math.sqrt(capacity));
            row = sheet.getRow(i);
            row.createCell(0).setCellValue(capacity);
            row.createCell(1).setCellValue(cost);
            cap = cap + 5;
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

    }

    catch (IOException ex) {
        System.out.println(ex.getMessage());
    }
}

From source file:instancegenerator.ExcelGenerator.java

public static void makeInstancesN200S1() {
    int weight = 0;
    int capacity = 0;
    int cost = 0;
    int[] items = new int[200];
    try {// w  ww.  j  ava  2  s. c  o  m
        //**************** Class A ***************************************************
        //instance A1
        FileOutputStream fileout = new FileOutputStream(new File("src/instances/Set1/N200/N200A1.xls"));
        Workbook ficheroWb = new HSSFWorkbook();
        Sheet sheet = ficheroWb.createSheet("Instance A1 for VSBPP");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        //for the items
        int reqCapacity = 0;
        for (int i = 1; i <= 200; i++) {
            weight = (int) randVal(1, 20);
            items[i - 1] = weight;
            reqCapacity += weight;
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(weight);
        }
        // for the bins
        int cap = 100;
        int j = 1;
        for (int i = 1; i <= 3; i++) {
            int availableCap = 0;
            capacity = cap * i;
            cost = (int) (100 * Math.sqrt(capacity));
            while (availableCap < reqCapacity) {
                row = sheet.getRow(j);
                if (row != null) {
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                } else {
                    row = sheet.createRow(j);
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                }
                j++;
                availableCap = availableCap + capacity;
            }
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

        //instance A2
        fileout = new FileOutputStream(new File("src/instances/Set1/N200/N200A2.xls"));
        ficheroWb = new HSSFWorkbook();
        sheet = ficheroWb.createSheet("Instance A2 for VSBPP");
        row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        // for the items
        for (int i = 1; i <= 200; i++) {
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(items[i - 1]);
        }
        //for the bins
        cap = 50;
        j = 1;
        for (int i = 1; i <= 6; i++) {
            int availableCap = 0;
            capacity = cap * i;
            cost = (int) (100 * Math.sqrt(capacity));
            while (availableCap < reqCapacity) {
                row = sheet.getRow(j);
                if (row != null) {
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                } else {
                    row = sheet.createRow(j);
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                }
                j++;
                availableCap = availableCap + capacity;
            }
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

        //instance A3
        fileout = new FileOutputStream(new File("src/instances/Set1/N200/N200A3.xls"));
        ficheroWb = new HSSFWorkbook();
        sheet = ficheroWb.createSheet("Instance A3 for VSBPP");
        row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        // for the items
        for (int i = 1; i <= 200; i++) {
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(items[i - 1]);
        }
        //for the bins
        cap = 25;
        j = 1;
        for (int i = 1; i <= 12; i++) {
            int availableCap = 0;
            capacity = cap * i;
            cost = (int) (100 * Math.sqrt(capacity));
            while (availableCap < reqCapacity) {
                row = sheet.getRow(j);
                if (row != null) {
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                } else {
                    row = sheet.createRow(j);
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                }
                j++;
                availableCap = availableCap + capacity;
            }
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

        //instance A4
        fileout = new FileOutputStream(new File("src/instances/Set1/N200/N200A4.xls"));
        ficheroWb = new HSSFWorkbook();
        sheet = ficheroWb.createSheet("Instance A4 for VSBPP");
        row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        // for the items
        for (int i = 1; i <= 200; i++) {
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(items[i - 1]);
        }
        //for the bins
        cap = 60;
        j = 1;
        for (int i = 1; i <= 55; i++) {
            capacity = cap;
            cost = (int) (100 * Math.sqrt(capacity));
            row = sheet.getRow(i);
            row.createCell(0).setCellValue(capacity);
            row.createCell(1).setCellValue(cost);
            cap = cap + 5;
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;
    }

    catch (IOException ex) {
        System.out.println(ex.getMessage());
    }
}

From source file:instancegenerator.ExcelGenerator.java

public static void makeInstancesN500S1() {
    int weight = 0;
    int capacity = 0;
    int cost = 0;
    int[] items = new int[500];
    try {//from  w  w  w  .ja  va 2s . c  o m
        //**************** Class A ***************************************************
        //instance A1
        FileOutputStream fileout = new FileOutputStream(new File("src/instances/Set1/N500/N500A1.xls"));
        Workbook ficheroWb = new HSSFWorkbook();
        Sheet sheet = ficheroWb.createSheet("Instance A1 for VSBPP");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        //for the items
        int reqCapacity = 0;
        for (int i = 1; i <= 500; i++) {
            weight = (int) randVal(1, 20);
            items[i - 1] = weight;
            reqCapacity += weight;
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(weight);
        }
        // for the bins
        int cap = 100;
        int j = 1;
        for (int i = 1; i <= 3; i++) {
            int availableCap = 0;
            capacity = cap * i;
            cost = (int) (100 * Math.sqrt(capacity));
            while (availableCap < reqCapacity) {
                row = sheet.getRow(j);
                if (row != null) {
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                } else {
                    row = sheet.createRow(j);
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                }
                j++;
                availableCap = availableCap + capacity;
            }
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

        //instance A2
        fileout = new FileOutputStream(new File("src/instances/Set1/N500/N500A2.xls"));
        ficheroWb = new HSSFWorkbook();
        sheet = ficheroWb.createSheet("Instance A2 for VSBPP");
        row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        // for the items
        for (int i = 1; i <= 500; i++) {
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(items[i - 1]);
        }
        //for the bins
        cap = 50;
        j = 1;
        for (int i = 1; i <= 6; i++) {
            int availableCap = 0;
            capacity = cap * i;
            cost = (int) (100 * Math.sqrt(capacity));
            while (availableCap < reqCapacity) {
                row = sheet.getRow(j);
                if (row != null) {
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                } else {
                    row = sheet.createRow(j);
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                }
                j++;
                availableCap = availableCap + capacity;
            }
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

        //instance A3
        fileout = new FileOutputStream(new File("src/instances/Set1/N500/N500A3.xls"));
        ficheroWb = new HSSFWorkbook();
        sheet = ficheroWb.createSheet("Instance A3 for VSBPP");
        row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        // for the items
        for (int i = 1; i <= 500; i++) {
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(items[i - 1]);
        }
        //for the bins
        cap = 25;
        j = 1;
        for (int i = 1; i <= 12; i++) {
            int availableCap = 0;
            capacity = cap * i;
            cost = (int) (100 * Math.sqrt(capacity));
            while (availableCap < reqCapacity) {
                row = sheet.getRow(j);
                if (row != null) {
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                } else {
                    row = sheet.createRow(j);
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                }
                j++;
                availableCap = availableCap + capacity;
            }
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

        //instance A4
        fileout = new FileOutputStream(new File("src/instances/Set1/N500/N500A4.xls"));
        ficheroWb = new HSSFWorkbook();
        sheet = ficheroWb.createSheet("Instance A4 for VSBPP");
        row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        // for the items
        for (int i = 1; i <= 500; i++) {
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(items[i - 1]);
        }
        //for the bins
        cap = 60;
        j = 1;
        for (int i = 1; i <= 55; i++) {
            capacity = cap;
            cost = (int) (100 * Math.sqrt(capacity));
            row = sheet.getRow(i);
            row.createCell(0).setCellValue(capacity);
            row.createCell(1).setCellValue(cost);
            cap = cap + 5;
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;
    }

    catch (IOException ex) {
        System.out.println(ex.getMessage());
    }
}

From source file:instancegenerator.ExcelGenerator.java

public static void makeInstancesN1000S1() {
    int weight = 0;
    int capacity = 0;
    int cost = 0;
    int[] items = new int[1000];
    try {//  w ww .  ja  v a2  s  .  co m
        //**************** Class A ***************************************************
        //instance A1
        FileOutputStream fileout = new FileOutputStream(new File("src/instances/Set1/N1000/N1000A1.xls"));
        Workbook ficheroWb = new HSSFWorkbook();
        Sheet sheet = ficheroWb.createSheet("Instance A1 for VSBPP");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        //for the items
        int reqCapacity = 0;
        for (int i = 1; i <= 1000; i++) {
            weight = (int) randVal(1, 20);
            items[i - 1] = weight;
            reqCapacity += weight;
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(weight);
        }
        // for the bins
        int cap = 100;
        int j = 1;
        for (int i = 1; i <= 3; i++) {
            int availableCap = 0;
            capacity = cap * i;
            cost = (int) (100 * Math.sqrt(capacity));
            while (availableCap < reqCapacity) {
                row = sheet.getRow(j);
                if (row != null) {
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                } else {
                    row = sheet.createRow(j);
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                }
                j++;
                availableCap = availableCap + capacity;
            }
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

        //instance A2
        fileout = new FileOutputStream(new File("src/instances/Set1/N1000/N1000A2.xls"));
        ficheroWb = new HSSFWorkbook();
        sheet = ficheroWb.createSheet("Instance A2 for VSBPP");
        row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        // for the items
        for (int i = 1; i <= 1000; i++) {
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(items[i - 1]);
        }
        //for the bins
        cap = 50;
        j = 1;
        for (int i = 1; i <= 6; i++) {
            int availableCap = 0;
            capacity = cap * i;
            cost = (int) (100 * Math.sqrt(capacity));
            while (availableCap < reqCapacity) {
                row = sheet.getRow(j);
                if (row != null) {
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                } else {
                    row = sheet.createRow(j);
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                }
                j++;
                availableCap = availableCap + capacity;
            }
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

        //instance A3
        fileout = new FileOutputStream(new File("src/instances/Set1/N1000/N1000A3.xls"));
        ficheroWb = new HSSFWorkbook();
        sheet = ficheroWb.createSheet("Instance A3 for VSBPP");
        row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        // for the items
        for (int i = 1; i <= 1000; i++) {
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(items[i - 1]);
        }
        //for the bins
        cap = 25;
        j = 1;
        for (int i = 1; i <= 12; i++) {
            int availableCap = 0;
            capacity = cap * i;
            cost = (int) (100 * Math.sqrt(capacity));
            while (availableCap < reqCapacity) {
                row = sheet.getRow(j);
                if (row != null) {
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                } else {
                    row = sheet.createRow(j);
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                }
                j++;
                availableCap = availableCap + capacity;
            }
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

        //instance A4
        fileout = new FileOutputStream(new File("src/instances/Set1/N1000/N1000A4.xls"));
        ficheroWb = new HSSFWorkbook();
        sheet = ficheroWb.createSheet("Instance A4 for VSBPP");
        row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        // for the items
        for (int i = 1; i <= 1000; i++) {
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(items[i - 1]);
        }
        //for the bins
        cap = 60;
        j = 1;
        for (int i = 1; i <= 55; i++) {
            capacity = cap;
            cost = (int) (100 * Math.sqrt(capacity));
            row = sheet.getRow(i);
            row.createCell(0).setCellValue(capacity);
            row.createCell(1).setCellValue(cost);
            cap = cap + 5;
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

    }

    catch (IOException ex) {
        System.out.println(ex.getMessage());
    }
}

From source file:instancegenerator.ExcelGenerator.java

/******************* No function asociated to cost of bins ********************************/
public static void makeInstancesN100S2() {
    int weight = 0;
    int capacity = 0;
    int cost = 0;
    int[] items = new int[100];
    try {//from w w w.  j  ava  2 s. com
        //**************** Class B ***************************************************
        //instance B1
        FileOutputStream fileout = new FileOutputStream(new File("src/instances/Set2/N100/N100B1.xls"));
        Workbook ficheroWb = new HSSFWorkbook();
        Sheet sheet = ficheroWb.createSheet("Instance B1 for VSBPP");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        //for the items
        int reqCapacity = 0;
        for (int i = 1; i <= 100; i++) {
            weight = (int) randVal(1, 20);
            items[i - 1] = weight;
            reqCapacity += weight;
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(weight);
        }
        // for the bins
        int cap = 100;
        int j = 1;
        for (int i = 1; i <= 3; i++) {
            int availableCap = 0;
            capacity = cap * i;
            while (availableCap < reqCapacity) {
                row = sheet.getRow(j);
                double y = randValueReal(0.05, 0.3);
                cost = (int) ((100 * Math.sqrt(capacity)) * (1 + y));
                if (row != null) {
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                } else {
                    row = sheet.createRow(j);
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                }
                j++;
                availableCap = availableCap + capacity;
            }
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

        //instance B2
        fileout = new FileOutputStream(new File("src/instances/Set2/N100/N100B2.xls"));
        ficheroWb = new HSSFWorkbook();
        sheet = ficheroWb.createSheet("Instance B2 for VSBPP");
        row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        // for the items
        for (int i = 1; i <= 100; i++) {
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(items[i - 1]);
        }
        //for the bins
        cap = 50;
        j = 1;
        for (int i = 1; i <= 6; i++) {
            int availableCap = 0;
            capacity = cap * i;
            while (availableCap < reqCapacity) {
                row = sheet.getRow(j);
                double y = randValueReal(0.05, 0.3);
                cost = (int) ((100 * Math.sqrt(capacity)) * (1 + y));
                if (row != null) {
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                } else {
                    row = sheet.createRow(j);
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                }
                j++;
                availableCap = availableCap + capacity;
            }
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

        //instance B3
        fileout = new FileOutputStream(new File("src/instances/Set2/N100/N100B3.xls"));
        ficheroWb = new HSSFWorkbook();
        sheet = ficheroWb.createSheet("Instance B3 for VSBPP");
        row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        // for the items
        for (int i = 1; i <= 100; i++) {
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(items[i - 1]);
        }
        //for the bins
        cap = 25;
        j = 1;
        for (int i = 1; i <= 12; i++) {
            int availableCap = 0;
            capacity = cap * i;
            while (availableCap < reqCapacity) {
                row = sheet.getRow(j);
                double y = randValueReal(0.05, 0.3);
                cost = (int) ((100 * Math.sqrt(capacity)) * (1 + y));
                if (row != null) {
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                } else {
                    row = sheet.createRow(j);
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                }
                j++;
                availableCap = availableCap + capacity;
            }
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

        //instance B4
        fileout = new FileOutputStream(new File("src/instances/Set2/N100/N100B4.xls"));
        ficheroWb = new HSSFWorkbook();
        sheet = ficheroWb.createSheet("Instance B4 for VSBPP");
        row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        // for the items
        for (int i = 1; i <= 100; i++) {
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(items[i - 1]);
        }
        //for the bins
        cap = 60;
        j = 1;
        for (int i = 1; i <= 55; i++) {
            capacity = cap;
            double y = randValueReal(0.05, 0.3);
            cost = (int) ((100 * Math.sqrt(capacity)) * (1 + y));
            row = sheet.getRow(i);
            row.createCell(0).setCellValue(capacity);
            row.createCell(1).setCellValue(cost);
            cap = cap + 5;
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

    }

    catch (IOException ex) {
        System.out.println(ex.getMessage());
    }
}

From source file:instancegenerator.ExcelGenerator.java

public static void makeInstancesN200S2() {
    int weight = 0;
    int capacity = 0;
    int cost = 0;
    int[] items = new int[200];
    try {//from   w w  w . j  av  a 2s .c  om
        //**************** Class B ***************************************************
        //instance B1
        FileOutputStream fileout = new FileOutputStream(new File("src/instances/Set2/N200/N200B1.xls"));
        Workbook ficheroWb = new HSSFWorkbook();
        Sheet sheet = ficheroWb.createSheet("Instance B1 for VSBPP");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        //for the items
        int reqCapacity = 0;
        for (int i = 1; i <= 200; i++) {
            weight = (int) randVal(1, 20);
            items[i - 1] = weight;
            reqCapacity += weight;
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(weight);
        }
        // for the bins
        int cap = 100;
        int j = 1;
        for (int i = 1; i <= 3; i++) {
            int availableCap = 0;
            capacity = cap * i;
            double y = randValueReal(0.05, 0.3);
            cost = (int) ((100 * Math.sqrt(capacity)) * (1 + y));
            while (availableCap < reqCapacity) {
                row = sheet.getRow(j);
                if (row != null) {
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                } else {
                    row = sheet.createRow(j);
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                }
                j++;
                availableCap = availableCap + capacity;
            }
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

        //instance B2
        fileout = new FileOutputStream(new File("src/instances/Set2/N200/N200B2.xls"));
        ficheroWb = new HSSFWorkbook();
        sheet = ficheroWb.createSheet("Instance B2 for VSBPP");
        row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        // for the items
        for (int i = 1; i <= 200; i++) {
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(items[i - 1]);
        }
        //for the bins
        cap = 50;
        j = 1;
        for (int i = 1; i <= 6; i++) {
            int availableCap = 0;
            capacity = cap * i;
            double y = randValueReal(0.05, 0.3);
            cost = (int) ((100 * Math.sqrt(capacity)) * (1 + y));
            while (availableCap < reqCapacity) {
                row = sheet.getRow(j);
                if (row != null) {
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                } else {
                    row = sheet.createRow(j);
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                }
                j++;
                availableCap = availableCap + capacity;
            }
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

        //instance B3
        fileout = new FileOutputStream(new File("src/instances/Set2/N200/N200B3.xls"));
        ficheroWb = new HSSFWorkbook();
        sheet = ficheroWb.createSheet("Instance B3 for VSBPP");
        row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        // for the items
        for (int i = 1; i <= 200; i++) {
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(items[i - 1]);
        }
        //for the bins
        cap = 25;
        j = 1;
        for (int i = 1; i <= 12; i++) {
            int availableCap = 0;
            capacity = cap * i;
            double y = randValueReal(0.05, 0.3);
            cost = (int) ((100 * Math.sqrt(capacity)) * (1 + y));
            while (availableCap < reqCapacity) {
                row = sheet.getRow(j);
                if (row != null) {
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                } else {
                    row = sheet.createRow(j);
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                }
                j++;
                availableCap = availableCap + capacity;
            }
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

        //instance B4
        fileout = new FileOutputStream(new File("src/instances/Set2/N200/N200B4.xls"));
        ficheroWb = new HSSFWorkbook();
        sheet = ficheroWb.createSheet("Instance B4 for VSBPP");
        row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        // for the items
        for (int i = 1; i <= 200; i++) {
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(items[i - 1]);
        }
        //for the bins
        cap = 60;
        j = 1;
        for (int i = 1; i <= 55; i++) {
            capacity = cap;
            double y = randValueReal(0.05, 0.3);
            cost = (int) ((100 * Math.sqrt(capacity)) * (1 + y));
            row = sheet.getRow(i);
            row.createCell(0).setCellValue(capacity);
            row.createCell(1).setCellValue(cost);
            cap = cap + 5;
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;
    }

    catch (IOException ex) {
        System.out.println(ex.getMessage());
    }
}

From source file:instancegenerator.ExcelGenerator.java

public static void makeInstancesN500S2() {
    int weight = 0;
    int capacity = 0;
    int cost = 0;
    int[] items = new int[500];
    try {//  ww w .  j a v  a  2s .  co  m
        //**************** Class B ***************************************************
        //instance B1
        FileOutputStream fileout = new FileOutputStream(new File("src/instances/Set2/N500/N500B1.xls"));
        Workbook ficheroWb = new HSSFWorkbook();
        Sheet sheet = ficheroWb.createSheet("Instance B1 for VSBPP");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        //for the items
        int reqCapacity = 0;
        for (int i = 1; i <= 500; i++) {
            weight = (int) randVal(1, 20);
            items[i - 1] = weight;
            reqCapacity += weight;
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(weight);
        }
        // for the bins
        int cap = 100;
        int j = 1;
        for (int i = 1; i <= 3; i++) {
            int availableCap = 0;
            capacity = cap * i;
            while (availableCap < reqCapacity) {
                row = sheet.getRow(j);
                double y = randValueReal(0.05, 0.3);
                cost = (int) ((100 * Math.sqrt(capacity)) * (1 + y));
                if (row != null) {
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                } else {
                    row = sheet.createRow(j);
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                }
                j++;
                availableCap = availableCap + capacity;
            }
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

        //instance B2
        fileout = new FileOutputStream(new File("src/instances/Set2/N500/N500B2.xls"));
        ficheroWb = new HSSFWorkbook();
        sheet = ficheroWb.createSheet("Instance B2 for VSBPP");
        row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        // for the items
        for (int i = 1; i <= 500; i++) {
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(items[i - 1]);
        }
        //for the bins
        cap = 50;
        j = 1;
        for (int i = 1; i <= 6; i++) {
            int availableCap = 0;
            capacity = cap * i;
            while (availableCap < reqCapacity) {
                row = sheet.getRow(j);
                double y = randValueReal(0.05, 0.3);
                cost = (int) ((100 * Math.sqrt(capacity)) * (1 + y));
                if (row != null) {
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                } else {
                    row = sheet.createRow(j);
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                }
                j++;
                availableCap = availableCap + capacity;
            }
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

        //instance B3
        fileout = new FileOutputStream(new File("src/instances/Set2/N500/N500B3.xls"));
        ficheroWb = new HSSFWorkbook();
        sheet = ficheroWb.createSheet("Instance B3 for VSBPP");
        row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        // for the items
        for (int i = 1; i <= 500; i++) {
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(items[i - 1]);
        }
        //for the bins
        cap = 25;
        j = 1;
        for (int i = 1; i <= 12; i++) {
            int availableCap = 0;
            capacity = cap * i;
            while (availableCap < reqCapacity) {
                row = sheet.getRow(j);
                double y = randValueReal(0.05, 0.3);
                cost = (int) ((100 * Math.sqrt(capacity)) * (1 + y));
                if (row != null) {
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                } else {
                    row = sheet.createRow(j);
                    row.createCell(0).setCellValue(capacity);
                    row.createCell(1).setCellValue(cost);
                }
                j++;
                availableCap = availableCap + capacity;
            }
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;

        //instance B4
        fileout = new FileOutputStream(new File("src/instances/Set2/N500/N500B4.xls"));
        ficheroWb = new HSSFWorkbook();
        sheet = ficheroWb.createSheet("Instance A4 for VSBPP");
        row = sheet.createRow(0);
        row.createCell(0).setCellValue("Capacity");
        row.createCell(1).setCellValue("Cost");
        row.createCell(2).setCellValue("Weight");
        // for the items
        for (int i = 1; i <= 500; i++) {
            row = sheet.createRow(i);
            row.createCell(2).setCellValue(items[i - 1]);
        }
        //for the bins
        cap = 60;
        j = 1;
        for (int i = 1; i <= 55; i++) {
            capacity = cap;
            double y = randValueReal(0.05, 0.3);
            cost = (int) ((100 * Math.sqrt(capacity)) * (1 + y));
            row = sheet.getRow(i);
            row.createCell(0).setCellValue(capacity);
            row.createCell(1).setCellValue(cost);
            cap = cap + 5;
        }
        ficheroWb.write(fileout);
        fileout.flush();
        capacity = 0;
    }

    catch (IOException ex) {
        System.out.println(ex.getMessage());
    }
}