1

I have the follwing table in the excel spreadsheet

manager    salary
puk          2
puk          3
puk          4
puk          5
ser          3
ser          4
ser          5
sos          23
sos          24
sos          25
sos          26
sos          27

I need to split this spreadsheet into three different shpreadsheets using FileOutputStream . The first should contain all entries for manager puk, second ser . . .

I cannot come up with a good logic to split it . Should I create a temporary copy of the original spread sheet and then remove all extra rows and save it ? Than how can I remove the rows ?

(This could have been done easily with CSVReader , but I need to preserve the formatting )

2
  • Instead of removing... Shifting the cell will be better option. try this stackoverflow.com/a/19951595/624003 Commented Feb 20, 2014 at 7:28
  • You might want to divide template and data spreadsheet. The template has only formatting. Read datas from spreadsheets and put them into temporary list. When the datas output, copy the template spreadsheet. Commented Mar 3, 2014 at 10:11

1 Answer 1

2

here is the entire program on how to do what you're asking, I have tested it out and it works perfectly :) tell me what you think:

package additives;
import org.apache.poi.ss.usermodel.*; 
import org.apache.poi.xssf.usermodel.*;

import java.util.*;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import java.io.*;
public class StackOverflow {

    public static Workbook readWorkbook(){

        Workbook wb=null;
        try {
            wb = WorkbookFactory.create(new File("stackOverflow.xlsx"));
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return wb;
    }

    public static void writeToSheet(List<String> name, List<Double> salary, int sheetCounter, List<Sheet> outputSheets){


        for(int i=0;i<salary.size();i++){

            Row row=outputSheets.get(sheetCounter).createRow(i);

            Cell nameCell=row.createCell(0);
            nameCell.setCellValue(name.get(i));

            Cell salaryCell=row.createCell(1);
            salaryCell.setCellValue(salary.get(i));
        }
    }

    public static void writeToWorkbook(Workbook wb){

        try{
            FileOutputStream out=new FileOutputStream("new StackOverflow.xlsx");
            wb.write(out);
            out.close();
        } catch(Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args){

        Workbook inputWb=readWorkbook();
        Sheet inputWs=inputWb.getSheet("sheet1");

        List<String> name=new ArrayList<>();
        List<Double> salary=new ArrayList<>();

        Workbook outputWb=new XSSFWorkbook();
        List<Sheet> outputSheets=new ArrayList<>();

        int rowIndex=inputWs.getLastRowNum()+1;
        int sheetCounter=0;

        for(int i=1; i<rowIndex-1; i++){
            Row outerRow=inputWs.getRow(i);
            Row innerRow=null; 
            Cell outerCell=outerRow.getCell(0);
            Cell innerCell=null;

            int j=0;
            for(j=i+1;j<rowIndex;j++){

                innerRow=inputWs.getRow(j);
                innerCell=innerRow.getCell(0);

                if(outerCell.getStringCellValue().equals(innerCell.getStringCellValue())){
                    name.add(innerRow.getCell(0).getStringCellValue());
                    salary.add(innerRow.getCell(1).getNumericCellValue());
                }


                if(!outerCell.getStringCellValue().equals(innerCell.getStringCellValue())){

                    break;
                }


            }

            name.add(outerRow.getCell(0).getStringCellValue());
            salary.add(outerRow.getCell(1).getNumericCellValue());
            i=j;
            outputSheets.add(outputWb.createSheet("sheet"+sheetCounter));
            writeToSheet(name,salary,sheetCounter, outputSheets);
            sheetCounter++;
            name.clear();
            salary.clear();

            Row tempRow=inputWs.getRow(i);
            try{
                name.add(tempRow.getCell(0).getStringCellValue());
                salary.add(tempRow.getCell(1).getNumericCellValue());
            }catch(Exception e){
                continue;
            }
        }
        writeToWorkbook(outputWb);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Anish it is perfect

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.