Example usage for org.apache.poi.ss.usermodel Row createCell

List of usage examples for org.apache.poi.ss.usermodel Row createCell

Introduction

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

Prototype

Cell createCell(int column);

Source Link

Document

Use this to create new cells within the row and return it.

Usage

From source file:aco.Utilities.java

License:Open Source License

static void writeInputDataPoints() {
    //the file already exists
    if (new File(filePath3).canRead()) {
        //System.out.println("File already exists..");
        try {/* w  w w .j ava  2  s  . com*/
            FileInputStream file = new FileInputStream(new File(filePath3));

            //Create Workbook instance holding reference to .xlsx file
            XSSFWorkbook workbook1 = new XSSFWorkbook(file);

            //Get first/desired sheet from the workbook
            XSSFSheet sheet1 = workbook1.getSheetAt(11);

            //define a cell style for bold font
            CellStyle style = workbook1.createCellStyle();
            Font font = workbook1.createFont();
            font.setBoldweight(Font.BOLDWEIGHT_BOLD);
            style.setFont(font);

            Row r1 = sheet1.getRow(0);
            if (r1 == null) {
                // First cell in the row, create
                r1 = sheet1.createRow(0);
            }

            Cell c = r1.getCell(0);
            if (c == null) {
                c = r1.createCell(0);
            }
            c.setCellValue("VRPTW instance - " + VRPTW.instance.name
                    + "; data point coordinates corresponding to customers' requests");
            c.setCellStyle(style);

            Row r = sheet1.getRow(2);
            if (r == null) {
                // First cell in the row, create
                r = sheet1.createRow(2);
            }

            Cell c1 = r.getCell(0);
            if (c1 == null) {
                c1 = r.createCell(0);
            }
            c1.setCellValue("Point #");
            c1.setCellStyle(style);

            c1 = r.getCell(1);
            if (c1 == null) {
                c1 = r.createCell(1);
            }
            c1.setCellValue("X Coord");
            c1.setCellStyle(style);

            c1 = r.getCell(2);
            if (c1 == null) {
                c1 = r.createCell(2);
            }
            c1.setCellValue("Y Coord");
            c1.setCellStyle(style);

            int size = VRPTW.instance.nodes.length;
            int rowIndex = 3;
            double x, y;
            for (int i = 0; i < size; i++) {
                x = VRPTW.instance.nodes[i].x;
                y = VRPTW.instance.nodes[i].y;
                r = sheet1.getRow(rowIndex + i);
                if (r == null) {
                    // First cell in the row, create
                    //System.out.println("Empty row, create new one");
                    r = sheet1.createRow(rowIndex + i);
                }

                c1 = r.getCell(0);
                if (c1 == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c1 = r.createCell(0);
                }
                c1.setCellValue(i);

                c1 = r.getCell(1);
                if (c1 == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c1 = r.createCell(1);
                }
                c1.setCellValue(x);

                c1 = r.getCell(2);
                if (c1 == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c1 = r.createCell(2);
                }
                c1.setCellValue(y);

            }

            //Write the workbook in file system
            FileOutputStream out = new FileOutputStream(new File(filePath3));
            workbook1.write(out);
            out.close();

            //System.out.println("Written successfully on disk.");
        } catch (Exception e) {
            e.printStackTrace();
        }

    } else {
        System.out.println("File not exists..");
    }
}

From source file:aco.Utilities.java

License:Open Source License

static void writeExcel(int n, int m, int result) {
    //the file already exists; we should add a new row as the last one in the Excel file
    if (new File(filePath).canRead()) {
        //System.out.println("File already exists..");
        try {/*from ww  w  .j a v  a2s . com*/
            FileInputStream file = new FileInputStream(new File(filePath));

            //Create Workbook instance holding reference to .xlsx file
            XSSFWorkbook workbook1 = new XSSFWorkbook(file);

            //Get first/desired sheet from the workbook
            XSSFSheet sheet1 = workbook1.getSheetAt(2);
            int countRows = sheet1.getLastRowNum() + 1;
            Row newRow = sheet1.createRow(countRows++);

            int cellnum = 0;
            Cell cell = newRow.createCell(cellnum++);
            cell.setCellValue(n);
            cell = newRow.createCell(cellnum++);
            cell.setCellValue(m);
            cell = newRow.createCell(cellnum++);
            cell.setCellValue(result);

            //Write the workbook in file system
            FileOutputStream out = new FileOutputStream(new File(filePath));
            workbook1.write(out);
            out.close();

            //System.out.println("Written successfully on disk.");
        } catch (Exception e) {
            e.printStackTrace();
        }

    } else {
        //Blank workbook
        XSSFWorkbook workbook2 = new XSSFWorkbook();

        //Create a blank sheet
        XSSFSheet sheet2 = workbook2.createSheet("Results - 51 cities");

        //Iterate over data and write to sheet
        int rownum = 0, cellnum = 0;
        Row row = sheet2.createRow(rownum++);
        Cell cell = row.createCell(cellnum++);
        cell.setCellValue(n);
        cell = row.createCell(cellnum++);
        cell.setCellValue(m);
        cell = row.createCell(cellnum++);
        cell.setCellValue(result);

        try {
            //Write the workbook in file system
            FileOutputStream out = new FileOutputStream(new File(filePath));
            workbook2.write(out);
            out.close();

            //System.out.println("Written successfully on disk.");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

From source file:aco.Utilities.java

License:Open Source License

static void writeResultsExcel(int trialNumber, boolean saveIterCosts) {
    Row r, r1;
    Cell c;//from  w w  w  .j  av  a  2  s  .co  m
    int index1 = 0, index2 = 0, index3 = 0, index4 = 0, index5 = 0;
    //int index6 = 0;

    //the file already exists; we should add a new row as the last one in the Excel file
    if (new File(filePath).canRead()) {
        //System.out.println("File already exists..");
        try {
            FileInputStream file = new FileInputStream(new File(filePath));

            //Create Workbook instance holding reference to .xlsx file
            XSSFWorkbook workbook1 = new XSSFWorkbook(file);

            int startIndex = 0, rowIndex = 0;
            /*switch (VRPTW.m) {
               case 2: 
             startIndex = 0;
             rowIndex = 4;
             break;
               case 3: 
             startIndex = 2;
             rowIndex = 5;
             break;
               case 5: 
             startIndex = 4;
             rowIndex = 7;
             break;
               case 7: 
             startIndex = 6;
             rowIndex = 9;
             break;
               default:
             System.out.println("Unknown value for m");
             break;         
            }*/

            //Get desired sheet from the workbook
            XSSFSheet sheet1 = workbook1.getSheetAt(startIndex); //for tours
            /*XSSFSheet sheet2 = workbook1.getSheetAt(startIndex + 1);  //for number of assigned cities
            XSSFSheet sheet3 = workbook1.getSheetAt(startIndex + 2);  //for cost of individual subtours
            XSSFSheet sheet4 = workbook1.getSheetAt(startIndex + 3);  //for total cost of subtours
            XSSFSheet sheet5 = workbook1.getSheetAt(startIndex + 4);  //for verbose output of total cost at each 5 iteration
            */
            XSSFSheet sheet2 = workbook1.getSheetAt(startIndex + 1); //for verbose output of longest cost at each 5 iteration

            //define a cell style for bold font
            CellStyle style = workbook1.createCellStyle();
            Font font = workbook1.createFont();
            font.setBoldweight(Font.BOLDWEIGHT_BOLD);
            style.setFont(font);

            //define style with bold font and blue color for font
            CellStyle styleBoldBlue = workbook1.createCellStyle();
            font = workbook1.createFont();
            font.setBoldweight(Font.BOLDWEIGHT_BOLD);
            font.setColor(IndexedColors.BLUE.index);
            styleBoldBlue.setFont(font);

            index1 = 133;
            if (!saveIterCosts) {
                //write only once the name of the algorithm that was run
                if (trialNumber == 0) {
                    r = sheet1.getRow(index1);
                    if (r == null) {
                        // First cell in the row, create
                        //System.out.println("Empty row, create new one");
                        r = sheet1.createRow(index1);
                    }

                    c = r.getCell(0);
                    if (c == null) {
                        // New cell
                        //System.out.println("Empty cell, create new one");
                        c = r.createCell(0);
                    }
                    c.setCellValue(
                            "Obtained solutions (values) after running new version (ACS MinMax global, voiajor si oras alesi simultan) with local search");
                    c.setCellStyle(styleBoldBlue);
                }

                //write only once the table header
                index1 = index1 + 3;
                r = sheet1.getRow(index1);
                if (r == null) {
                    // First cell in the row, create
                    //System.out.println("Empty row, create new one");
                    r = sheet1.createRow(index1);
                }

                c = r.getCell(0);
                if (c == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c = r.createCell(0);
                }
                c.setCellValue("Run #");
                c.setCellStyle(style);

                c = r.getCell(1);
                if (c == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c = r.createCell(1);
                }
                c.setCellValue("MinMax (cost of longest subtour)");
                c.setCellStyle(style);

                c = r.getCell(2);
                if (c == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c = r.createCell(2);
                }
                c.setCellValue("Total Cost");
                c.setCellStyle(style);

                c = r.getCell(3);
                if (c == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c = r.createCell(3);
                }
                c.setCellValue("Amplitude");
                c.setCellStyle(style);

                //write number of run
                index1 = 137 + trialNumber;
                r = sheet1.getRow(index1);
                if (r == null) {
                    // First cell in the row, create
                    //System.out.println("Empty row, create new one");
                    r = sheet1.createRow(index1);
                }

                c = r.getCell(0);
                if (c == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c = r.createCell(0);
                }
                c.setCellValue(trialNumber + 1);

                //write MinMax (cost of longest subtour)
                double longestSubtour = getLongestSubtour();
                c = r.getCell(1);
                if (c == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c = r.createCell(1);
                }
                c.setCellValue(longestSubtour);

                //write total cost
                double totalCost = getTotalCost();
                c = r.getCell(2);
                if (c == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c = r.createCell(2);
                }
                c.setCellValue(totalCost);

                //write amplitude
                double amplitude = getAmplitude();
                c = r.getCell(3);
                if (c == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c = r.createCell(3);
                }
                c.setCellValue(amplitude);
            }

            index5 = 859;
            if (saveIterCosts) {
                //write only once the name of the algorithm that was run
                if (trialNumber == 0) {
                    r = sheet2.getRow(index5);
                    if (r == null) {
                        // First cell in the row, create
                        //System.out.println("Empty row, create new one");
                        r = sheet2.createRow(index5);
                    }

                    c = r.getCell(0);
                    if (c == null) {
                        // New cell
                        //System.out.println("Empty cell, create new one");
                        c = r.createCell(0);
                    }
                    c.setCellValue(
                            "Longest cost of subtour at each 5 iteration after running new version (ACS MinMax global, voiajor si oras alesi simultan) with local search");
                    c.setCellStyle(styleBoldBlue);

                    int tempIndex = index5 + 3;
                    r = sheet2.getRow(tempIndex);
                    if (r == null) {
                        // First cell in the row, create
                        //System.out.println("Empty row, create new one");
                        r = sheet2.createRow(tempIndex);
                    }
                    ArrayList<Integer> iterNumber = getIterNumber();

                    c = r.getCell(0);
                    if (c == null) {
                        // New cell
                        //System.out.println("Empty cell, create new one");
                        c = r.createCell(0);
                    }
                    c.setCellValue("Nr Iter");
                    c.setCellStyle(style);

                    int indexTemp = 0;
                    for (int j = 0; j < iterNumber.size(); j++) {
                        indexTemp = tempIndex + 1 + j;
                        r1 = sheet2.getRow(indexTemp);
                        if (r1 == null) {
                            // First cell in the row, create
                            //System.out.println("Empty row, create new one");
                            r1 = sheet2.createRow(indexTemp);
                        }

                        c = r1.getCell(0);
                        if (c == null) {
                            // New cell
                            //System.out.println("Empty cell, create new one");
                            c = r1.createCell(0);
                        }
                        c.setCellValue(iterNumber.get(j));
                    }
                }

                index5 = index5 + 3;
                r = sheet2.getRow(index5);
                if (r == null) {
                    // First cell in the row, create
                    //System.out.println("Empty row, create new one");
                    r = sheet2.createRow(index5);
                }

                //for each trial run save at each 5 iteration the best longest cost of a subtour so far
                ArrayList<Double> iterLongestCost = getIterLongestCost();
                int index;

                //for each run write the table header cell
                c = r.getCell(trialNumber + 1);
                if (c == null) {
                    // New cell
                    //System.out.println("Empty cell, create new one");
                    c = r.createCell(trialNumber + 1);
                }
                c.setCellValue("Run " + (trialNumber + 1));
                c.setCellStyle(style);

                for (int j = 0; j < iterLongestCost.size(); j++) {
                    index = index5 + 1 + j;
                    r1 = sheet2.getRow(index);
                    if (r1 == null) {
                        // First cell in the row, create
                        //System.out.println("Empty row, create new one");
                        r1 = sheet2.createRow(index);
                    }

                    c = r1.getCell(trialNumber + 1);
                    if (c == null) {
                        // New cell
                        //System.out.println("Empty cell, create new one");
                        c = r1.createCell(trialNumber + 1);
                    }
                    c.setCellValue(iterLongestCost.get(j));
                }
            }

            //Write the workbook in file system
            FileOutputStream out = new FileOutputStream(new File(filePath));
            workbook1.write(out);
            out.close();

            int nrOfRun = trialNumber + 1;
            System.out.println("\nRun #" + nrOfRun + " written successfully on disk.\n");
        } catch (Exception e) {
            e.printStackTrace();
        }

    } else {
        //Blank workbook
        System.out.println("File " + filePath + " doesn't exists..");

    }
}

From source file:aco.Utilities.java

License:Open Source License

static void writeParetoSet(ArrayList<Ant> bestSoFarPareto, int trial) {
    Row r;
    Cell c;//from  w w w  .j  a va 2s.c  o  m
    int lineNumber = 0;

    //filePath1 += InOut.max_iterations + " iter (ACO MinMax_vers noua).xlsx";
    //System.out.println("file path=" + filePath1);

    //the file already exists; we should add a new row as the last one in the Excel file
    if (new File(filePath1).canRead()) {
        //System.out.println("File already exists..");
        try {
            FileInputStream file = new FileInputStream(new File(filePath1));

            //Create Workbook instance holding reference to .xlsx file
            XSSFWorkbook workbook1 = new XSSFWorkbook(file);

            //Get first/desired sheet from the workbook
            XSSFSheet sheet1 = workbook1.getSheetAt(trial);

            //write table header cells
            r = sheet1.getRow(lineNumber);
            if (r == null) {
                // First cell in the row, create
                r = sheet1.createRow(lineNumber);
            }
            c = r.getCell(0);
            if (c == null) {
                // New cell
                c = r.createCell(0);
            }
            c.setCellValue("Point #");
            c = r.getCell(1);
            if (c == null) {
                // New cell
                c = r.createCell(1);
            }
            c.setCellValue("Total tours length");
            c = r.getCell(2);
            if (c == null) {
                // New cell
                c = r.createCell(2);
            }
            c.setCellValue("Amplitude of tours");
            c = r.getCell(3);
            if (c == null) {
                // New cell
                c = r.createCell(3);
            }
            c.setCellValue("List with cost of subtours");

            lineNumber++;
            for (int i = 0; i < bestSoFarPareto.size(); i++) {
                r = sheet1.getRow(i + lineNumber);
                if (r == null) {
                    // First cell in the row, create
                    r = sheet1.createRow(i + lineNumber);
                }
                //write point id
                c = r.getCell(0);
                if (c == null) {
                    // New cell
                    c = r.createCell(0, Cell.CELL_TYPE_NUMERIC);
                }
                c.setCellValue(i + 1);
                //write total cost and amplitude
                for (int indexObj = 0; indexObj < 2; indexObj++) {
                    c = r.getCell(indexObj + 1);
                    if (c == null) {
                        // New cell
                        c = r.createCell(indexObj + 1, Cell.CELL_TYPE_NUMERIC);
                    }
                    c.setCellValue(bestSoFarPareto.get(i).costObjectives[indexObj]);
                }
                //write cost of each individual subtour
                for (int j = 0; j < bestSoFarPareto.get(i).tour_lengths.size(); j++) {
                    c = r.getCell(j + 3);
                    if (c == null) {
                        // New cell
                        c = r.createCell(j + 3);
                    }
                    c.setCellValue(bestSoFarPareto.get(i).tour_lengths.get(j));
                }
            }

            //Write the workbook in file system
            FileOutputStream out = new FileOutputStream(new File(filePath1));
            workbook1.write(out);
            out.close();

            //System.out.println("\nWritten Pareto front points successfully on disk.\n");
            int nrOfRun = trial + 1;
            System.out.println("\nRun #" + nrOfRun + " written Pareto front points successfully on disk.\n");
        } catch (Exception e) {
            e.printStackTrace();
        }

    } else {
        System.out.println(" File " + filePath1 + " doesn't exists");
    }

}

From source file:aco.Utilities.java

License:Open Source License

static void writeExcelFinalSolution(int trial, double scalledValue) {
    Row r;
    Cell c;/*from   w w w  .j  a va 2 s  .  c om*/
    int index1 = 0;

    //the file already exists; we should add a new row as the last one in the Excel file
    if (new File(filePath5).canRead()) {
        //System.out.println("File already exists..");
        try {
            FileInputStream file = new FileInputStream(new File(filePath5));

            //Create Workbook instance holding reference to .xlsx file
            XSSFWorkbook workbook1 = new XSSFWorkbook(file);

            //Get desired sheet from the workbook
            XSSFSheet sheet1 = workbook1.getSheetAt(0);

            //define a cell style for bold font
            CellStyle style = workbook1.createCellStyle();
            Font font = workbook1.createFont();
            font.setBoldweight(Font.BOLDWEIGHT_BOLD);
            style.setFont(font);

            //define style with bold font and blue color for font
            CellStyle styleBoldBlue = workbook1.createCellStyle();
            font = workbook1.createFont();
            font.setBoldweight(Font.BOLDWEIGHT_BOLD);
            font.setColor(IndexedColors.BLUE.index);
            styleBoldBlue.setFont(font);

            index1 = 8; //8  //26

            index1 = index1 + trial;
            r = sheet1.getRow(index1);
            if (r == null) {
                // First cell in the row, create
                //System.out.println("Empty row, create new one");
                r = sheet1.createRow(index1);
            }

            int nrOfRun = trial + 1;
            //write trial number (Run #)
            c = r.getCell(15);
            if (c == null) {
                // New cell
                //System.out.println("Empty cell, create new one");
                c = r.createCell(15);
            }
            c.setCellValue(nrOfRun);

            //write number of used vehicles
            c = r.getCell(16);
            if (c == null) {
                // New cell
                //System.out.println("Empty cell, create new one");
                c = r.createCell(16);
            }
            c.setCellValue(Ants.best_so_far_ant.usedVehicles);

            //write total traveled distance
            c = r.getCell(17);
            if (c == null) {
                // New cell
                //System.out.println("Empty cell, create new one");
                c = r.createCell(17);
            }
            c.setCellValue(scalledValue);

            //write the total number of feasible solutions
            c = r.getCell(18);
            if (c == null) {
                // New cell
                //System.out.println("Empty cell, create new one");
                c = r.createCell(18);
            }
            c.setCellValue(InOut.noSolutions);

            //Write the workbook in file system
            FileOutputStream out = new FileOutputStream(new File(filePath5));
            workbook1.write(out);
            out.close();

            System.out.println("\nRun #" + nrOfRun + " written successfully on disk.\n");
        }

        catch (Exception e) {
            e.printStackTrace();
        }

    } else {
        //Blank workbook
        System.out.println("File " + filePath5 + " doesn't exists..");

    }
}

From source file:adams.data.io.output.ExcelSpreadSheetWriter.java

License:Open Source License

/**
 * Performs the actual writing. The caller must ensure that the writer gets
 * closed.//from w  w w . j  av a  2s .com
 *
 * @param content   the spreadsheet to write
 * @param out      the writer to write the spreadsheet to
 * @return      true if successfully written
 */
@Override
protected boolean doWrite(SpreadSheet[] content, OutputStream out) {
    boolean result;
    Workbook workbook;
    Sheet sheet;
    Row row;
    adams.data.spreadsheet.Row spRow;
    adams.data.spreadsheet.Cell spCell;
    Cell cell;
    int i;
    int n;
    int count;
    CellStyle styleDate;
    CellStyle styleDateTime;
    CellStyle styleTime;
    HashSet<String> names;
    String name;

    result = true;

    try {
        if (getWriteOOXML())
            workbook = new XSSFWorkbook();
        else
            workbook = new HSSFWorkbook();
        styleDate = ExcelHelper.getDateCellStyle(workbook, Constants.DATE_FORMAT);
        styleDateTime = ExcelHelper.getDateCellStyle(workbook, Constants.TIMESTAMP_FORMAT);
        styleTime = ExcelHelper.getDateCellStyle(workbook, Constants.TIME_FORMAT);

        count = 0;
        names = new HashSet<>();
        for (SpreadSheet cont : content) {
            if (m_Stopped)
                return false;

            sheet = workbook.createSheet();
            if (cont.getName() != null) {
                name = cont.getName().replace("'", "");
                if (names.contains(name))
                    name += (count + 1);
            } else {
                name = m_SheetPrefix + (count + 1);
            }
            names.add(name);
            workbook.setSheetName(count, name);

            // header
            row = sheet.createRow(0);
            for (i = 0; i < cont.getColumnCount(); i++) {
                cell = row.createCell(i);
                cell.setCellValue(cont.getHeaderRow().getCell(i).getContent());
            }

            // data
            for (n = 0; n < cont.getRowCount(); n++) {
                if (m_Stopped)
                    return false;
                row = sheet.createRow(n + 1);
                spRow = cont.getRow(n);
                for (i = 0; i < cont.getColumnCount(); i++) {
                    cell = row.createCell(i);
                    spCell = spRow.getCell(i);
                    if ((spCell == null) || spCell.isMissing()) {
                        if (m_MissingValue.length() > 0)
                            cell.setCellValue(m_MissingValue);
                        else
                            cell.setCellType(Cell.CELL_TYPE_BLANK);
                        continue;
                    }

                    if (spCell.isFormula() && !m_OutputAsDisplayed) {
                        cell.setCellFormula(spCell.getFormula().substring(1));
                    } else {
                        if (spCell.isDate()) {
                            cell.setCellValue(spCell.toDate());
                            cell.setCellStyle(styleDate);
                        } else if (spCell.isTime()) {
                            cell.setCellValue(spCell.toTime());
                            cell.setCellStyle(styleTime);
                        } else if (spCell.isDateTime()) {
                            cell.setCellValue(spCell.toDateTime());
                            cell.setCellStyle(styleDateTime);
                        } else if (spCell.isNumeric()) {
                            cell.setCellValue(Utils.toDouble(spCell.getContent()));
                        } else {
                            cell.setCellValue(spCell.getContent());
                        }
                    }
                }
            }

            // next sheet
            count++;
        }

        // save
        workbook.write(out);
    } catch (Exception e) {
        result = false;
        getLogger().log(Level.SEVERE, "Failed writing spreadsheet data", e);
    }

    return result;
}

From source file:adams.data.io.output.ExcelStreamingSpreadSheetWriter.java

License:Open Source License

/**
 * Performs the actual writing. The caller must ensure that the writer gets
 * closed.//  w w w  .j  a va 2s  .  c o m
 *
 * @param content   the spreadsheet to write
 * @param out      the writer to write the spreadsheet to
 * @return      true if successfully written
 */
@Override
protected boolean doWrite(SpreadSheet[] content, OutputStream out) {
    boolean result;
    SXSSFWorkbook workbook;
    Sheet sheet;
    Row row;
    adams.data.spreadsheet.Row spRow;
    adams.data.spreadsheet.Cell spCell;
    Cell cell;
    int i;
    int n;
    int count;
    CellStyle styleDate;
    CellStyle styleDateTime;
    CellStyle styleTime;
    HashSet<String> names;
    String name;

    result = true;

    try {
        workbook = new SXSSFWorkbook(m_MaxRows);
        styleDate = ExcelHelper.getDateCellStyle(workbook, Constants.DATE_FORMAT);
        styleDateTime = ExcelHelper.getDateCellStyle(workbook, Constants.TIMESTAMP_FORMAT);
        styleTime = ExcelHelper.getDateCellStyle(workbook, Constants.TIME_FORMAT);

        count = 0;
        names = new HashSet<>();
        for (SpreadSheet cont : content) {
            if (m_Stopped)
                return false;

            sheet = workbook.createSheet();
            if (cont.getName() != null) {
                name = cont.getName().replace("'", "");
                if (names.contains(name))
                    name += (count + 1);
            } else {
                name = m_SheetPrefix + (count + 1);
            }
            names.add(name);
            workbook.setSheetName(count, name);

            // header
            row = sheet.createRow(0);
            for (i = 0; i < cont.getColumnCount(); i++) {
                cell = row.createCell(i);
                cell.setCellValue(cont.getHeaderRow().getCell(i).getContent());
            }

            // data
            for (n = 0; n < cont.getRowCount(); n++) {
                if (m_Stopped)
                    return false;

                row = sheet.createRow(n + 1);
                spRow = cont.getRow(n);
                for (i = 0; i < cont.getColumnCount(); i++) {
                    cell = row.createCell(i);
                    spCell = spRow.getCell(i);
                    if ((spCell == null) || spCell.isMissing()) {
                        if (m_MissingValue.length() > 0)
                            cell.setCellValue(m_MissingValue);
                        else
                            cell.setCellType(Cell.CELL_TYPE_BLANK);
                        continue;
                    }

                    if (spCell.isFormula() && !m_OutputAsDisplayed) {
                        cell.setCellFormula(spCell.getFormula().substring(1));
                    } else {
                        if (spCell.isDate()) {
                            cell.setCellValue(spCell.toDate());
                            cell.setCellStyle(styleDate);
                        } else if (spCell.isTime()) {
                            cell.setCellValue(spCell.toTime());
                            cell.setCellStyle(styleTime);
                        } else if (spCell.isDateTime()) {
                            cell.setCellValue(spCell.toDateTime());
                            cell.setCellStyle(styleDateTime);
                        } else if (spCell.isNumeric()) {
                            cell.setCellValue(Utils.toDouble(spCell.getContent()));
                        } else {
                            cell.setCellValue(spCell.getContent());
                        }
                    }
                }
            }

            // next sheet
            count++;
        }

        // save
        workbook.write(out);
    } catch (Exception e) {
        result = false;
        getLogger().log(Level.SEVERE, "Failed writing spreadsheet data", e);
    }

    return result;
}

From source file:Algorithm.Method1.java

public String[] MethodTest(String Path) {
    StringBuffer keyword1 = new StringBuffer();
    try {//from w  w w  .  ja v a  2s  .  c o m
        Hashtable hash = new Hashtable();

        FileInputStream file = new FileInputStream(new File(Path));
        XSSFWorkbook workbook = new XSSFWorkbook(file);
        XSSFSheet sheet1 = workbook.getSheetAt(0);
        Iterator<Row> rowIterator = sheet1.iterator();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            //Row rowNew =sheetNew.createRow(rowNumNew++);
            //For each row, iterate through all the columns
            Iterator<org.apache.poi.ss.usermodel.Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                org.apache.poi.ss.usermodel.Cell cell = cellIterator.next();
                // Cell cellNew =rowNew.createCell(cellNumNew++);
                //Check the cell type and format accordingly
                switch (cell.getCellType()) {
                case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC:
                    break;
                case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING:
                    String result = cell.getStringCellValue();
                    if (hash.containsKey(result)) {
                        int f = Integer.parseInt(hash.get(result).toString());
                        f++;
                        hash.put(result, f);
                    } else {
                        hash.put(result, 1);
                    }

                }
            }

            //System.out.println("");
        }

        XSSFWorkbook workbookNew = new XSSFWorkbook();
        XSSFSheet sheetNew = workbookNew.createSheet("test");
        int rowNum = 0;

        Set s = hash.keySet();
        String key = new String();
        int t = 0;
        for (Iterator<String> i = s.iterator(); i.hasNext();) {
            key = i.next();
            Row rowNew = sheetNew.createRow(rowNum);
            org.apache.poi.ss.usermodel.Cell cellNew = rowNew.createCell(0);
            cellNew.setCellValue(key);
            keyword1.append(key + " ");
            org.apache.poi.ss.usermodel.Cell cellNew2 = rowNew.createCell(1);
            cellNew2.setCellValue(hash.get(key).toString());
            rowNum++;
            //sheet2.addCell(new Label(0,t , key));
            //System.out.println(hash.get(key));
            //sheet2.addCell(new Label(1,t , hash.get(key).toString()));
            t++;
        }

        FileOutputStream fileOut = new FileOutputStream(new File(Path.replace("???.xlsx", "method1.xlsx")));//new file
        workbookNew.write(fileOut);
        fileOut.close();

        file.close();

        // Workbook book = Workbook.getWorkbook(new File("n.xls"));

        //WritableWorkbook book2 = Workbook.createWorkbook(new File("method1.xls"));
        //
        // WritableSheet sheet2 = book2.createSheet("num1", 0);

        // 
        //Sheet sheet = book.getSheet(0);         
        //int rownum = sheet.getRows();// 
        /**
        Cell cell;
        for(int i = 0;i<rownum;i++){
           cell = sheet.getCell(0,i);
           String result = cell.getContents();
           if(hash.containsKey(result)){
          int f = Integer.parseInt(hash.get(result).toString());
          f++;
          hash.put(result, f);
           }else{
          hash.put(result, 1);
           }
        }
        */
        //??
        /*
        Set s=hash.keySet();
        String key = new String();
        int t = 0;
        for(Iterator<String> i=s.iterator();i.hasNext();){
           key = i.next();
           sheet2.addCell(new Label(0,t , key));
        //System.out.println(hash.get(key));
        sheet2.addCell(new Label(1,t , hash.get(key).toString()));
        t++;
        }
                
        book2.write();
        book2.close();
        */
        // book.close();
        System.out.print("method1");
    } catch (Exception e) {
        System.out.println(e);
    }
    return keyword1.toString().split(" ");
}

From source file:Algorithm.Method2.java

public String[] MethodTest(String Path) {
    StringBuffer keyword1 = new StringBuffer();
    try {// w ww  .  j a  va  2  s.c  o m
        /**
        * ??excel
        * src
        * map?
        * result
        */

        String srcFilePath = Path;
        String mapFilePath = "??.xlsx";
        String newFilePath = Path.replace("???.xlsx", "method2.xlsx");

        FileInputStream srcFile = new FileInputStream(new File(srcFilePath));
        FileInputStream mapFile = new FileInputStream(new File(mapFilePath));

        XSSFWorkbook srcWorkbook = new XSSFWorkbook(srcFile);
        XSSFWorkbook mapWorkbook = new XSSFWorkbook(mapFile);
        XSSFWorkbook newWorkbook = new XSSFWorkbook();

        XSSFSheet srcSheet = srcWorkbook.getSheetAt(0);
        XSSFSheet mapSheet = mapWorkbook.getSheetAt(0);
        XSSFSheet newSheet = newWorkbook.createSheet("test");

        Iterator<Row> srcRowIterator = srcSheet.iterator();
        Iterator<Row> mapRowIterator = mapSheet.iterator();

        //map.xlst???mapList
        ArrayList mapList = new ArrayList();
        while (mapRowIterator.hasNext()) {
            Row row = mapRowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();

            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                String test = cell.getStringCellValue();
                mapList.add(test);
            }
        }

        //???mapArray,?
        int mapListLength = mapList.size();
        String[] mapArray = (String[]) mapList.toArray(new String[mapListLength]);

        //??mapResult,mapArray?src
        //,mapArray[i]src,mapResult[i]1?0
        int[] mapResult = new int[mapListLength];
        for (int i = 0; i < mapListLength; i++) {
            mapResult[i] = 0;
        }

        //?src.xlsx,?mapArray
        while (srcRowIterator.hasNext()) {
            Row row = srcRowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                String test = cell.getStringCellValue().trim();

                for (int i = 0; i < mapListLength; i++) {
                    if (test.equals(mapArray[i]))
                        mapResult[i] = 1;
                }
            }
        }

        //?mapResult,mapArrayresult
        int newRowNum = 0;
        for (int i = mapListLength - 1; i >= 0; i--) {
            if (mapResult[i] == 1) {
                Row newRow = newSheet.createRow(newRowNum++);
                Cell newCell = newRow.createCell(0);
                String test = mapArray[i];
                keyword1.append(test + " ");
                newCell.setCellValue(test);
            }
        }

        srcFile.close();
        mapFile.close();

        //FileOutputStream fileOut = new FileOutputStream(new File(newFilePath));
        // newWorkbook.write(fileOut);
        // fileOut.close();
        // System.out.print("method2");
    } catch (Exception e) {
        System.out.println(e);
    }

    return keyword1.toString().split(" ");
}

From source file:Algorithm.SegmentationAndNounFilter.java

public String[] SegmentationNounFilter(String filepath, String filename) {
    try {//w  ww  .j  av a  2  s.  c om

        String mingciPath = filepath.replace(filename, "???.xlsx");
        ICTCLAS50 testICTCLAS50 = new ICTCLAS50();
        String argu = ".";
        if (testICTCLAS50.ICTCLAS_Init(argu.getBytes("GB2312")) == false) {
            System.out.println("Init Fail");
        } else {
            System.out.println("Init Succeed!");
        }

        StringBuffer input = new StringBuffer();
        FileInputStream file = new FileInputStream(new File(filepath));
        XSSFWorkbook workbook = new XSSFWorkbook(file);
        XSSFSheet sheet1 = workbook.getSheetAt(0);
        Iterator<Row> rowIterator = sheet1.iterator();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            //Row rowNew =sheetNew.createRow(rowNumNew++);
            //For each row, iterate through all the columns
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                // Cell cellNew =rowNew.createCell(cellNumNew++);
                //Check the cell type and format accordingly
                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_NUMERIC:
                    break;
                case Cell.CELL_TYPE_STRING:
                    input.append(cell.getStringCellValue());

                }
            }

            //System.out.println("");
        }
        //?
        byte nativeBytes[] = testICTCLAS50.ICTCLAS_ParagraphProcess(input.toString().getBytes("GB2312"), 0, 1);

        String nativeStr = new String(nativeBytes, 0, nativeBytes.length, "GB2312");

        //xlsx
        XSSFWorkbook workbookNew = new XSSFWorkbook();
        XSSFSheet sheetNew = workbookNew.createSheet("test");
        int rowNum = 0;

        // WritableWorkbook book = Workbook.createWorkbook(new File("n.xls"));
        // WritableSheet sheet = book.createSheet("num1", 0);
        Scanner in = new Scanner(nativeStr);
        int i = 0;//
        while (in.hasNext()) {
            String ss = in.next();

            Pattern pattern = Pattern.compile("(.+?)/n.*");
            Matcher matcher = pattern.matcher(ss);
            if (matcher.find() && matcher.group(1).length() > 1 && !isDigit(matcher.group(1))) {

                //label = new jxl.write.Label(0, i, matcher.group(1));//?
                //sheet.addCell(label);
                Row rowNew = sheetNew.createRow(rowNum++);
                Cell cellNew = rowNew.createCell(0);
                cellNew.setCellValue(matcher.group(1));
                //i++;

            }
        }

        //    book.write();
        //   book.close();
        FileOutputStream fileOut = new FileOutputStream(new File(mingciPath));//new file
        workbookNew.write(fileOut);
        fileOut.close();

        //??            

        file.close();

        testICTCLAS50.ICTCLAS_Exit();

    } catch (Exception ex) {

    }

    return null;

}