Search code examples
javaapache-poiexport-to-excel

How to split Excel File into Multiple sheets?


Friends, I am new to Apache POI. I have been assigned a task of Splitting a Excel file(WorkBook) into Multiple Sheets depending on the data which are at Excel file. My Quest is,

Is it possible to split a Single Excel sheet into multiple Excel sheets? How to Input a Excel sheet to Apache POI? so far i have practiced to create a Excel file through Apache POI and no idea about how to input a Excel sheet to Apache POI.

Any sort of HELP will be appreciated. Thanks in advance.


Solution

  • This Tutorial will help you out to create Sheets in Excel File with Apache POI

    Although the Solution is Very simple.

    Tutorial Code Walk through for your help!

    public class CalendarDemo {
    
        private static final String[] days = {
                "Sunday", "Monday", "Tuesday",
                "Wednesday", "Thursday", "Friday", "Saturday"};
    
        private static final String[]  months = {
                "January", "February", "March","April", "May", "June","July", "August",
                "September","October", "November", "December"};
    
        public static void main(String[] args) throws Exception {
    
            Calendar calendar = Calendar.getInstance();
            boolean xlsx = true;
            for (int i = 0; i < args.length; i++) {
                if(args[i].charAt(0) == '-'){
                    xlsx = args[i].equals("-xlsx");
                } else {
                  calendar.set(Calendar.YEAR, Integer.parseInt(args[i]));
                }
            }
            int year = calendar.get(Calendar.YEAR);
    
    
            //Step #01 Creating Excel WorkBook
            Workbook wb = xlsx ? new XSSFWorkbook() : new HSSFWorkbook();
    
            Map<String, CellStyle> styles = createStyles(wb);
    
            for (int month = 0; month < 12; month++) {
                calendar.set(Calendar.MONTH, month);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                //create a sheet for each month
    
                //Step #02  Creating WorkSheets in WorkBook
                Sheet sheet = wb.createSheet(months[month]);
    

    enter image description here