Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Java
package Excel;
import jxl.*;
import jxl.Workbook;
import jxl.read.biff.*;
import java.io.File;
import java.io.IOException;
import jxl.write.WriteException;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.Label; 
  
public class CreateExcel {
 
	public static void main(String[] args) throws IOException, WriteException, BiffException
	{
			// TODO Auto-generated method stub
			int even=1,k=0;
			final File  outputWorkbook;
			try
			{
         
				File f1=new File("C:\\Users\\abc\\Desktop\\LetsCheck.xls");  //the excel sheet which contains data
		        WorkbookSettings ws=new WorkbookSettings();
		        //ws.setLocale(new Locale("er","ER"));
		        Workbook workbook=Workbook.getWorkbook(f1,ws); 
		          
		        Sheet readsheet=workbook.getSheet(0);
		       // System.out.println(readsheet.getName());  
		        File f2= new File("C:\\Users\\abc\\Desktop\\TestExcel.xls");
		       // outputWorkbook = new File("C:\\Users\\abc\\Desktop\\TestExcel.xls");  // the excel sheet where data is to copied
		       
//       WritableWorkbook workbook1=Workbook.createWorkbook(f2);
       WritableWorkbook workbook1= workbook.createWorkbook(f2);
		        WritableSheet sheet1 = workbook1.getSheet("Data");
		        int i=0,j=0;   //following code copies the data from LetsCheck.xls to testatul.xls 
		        for(j=0;j<readsheet.getColumns();j+=1)
		        {      
//		        	System.out.println(readsheet.getColumns());
//		        	System.out.println(readsheet.getRows());
		        	k=0;
		        	for(i=0;i<readsheet.getRows();i+=1)  
		        	{       
		        		
		        	/*	if(even%2==1)
		        		{
		        			even++;
		        			continue;
		        		}
		        		else
		        		{
//		        		*/	even++;
		        			/*if(readsheet.getCell(j,i).getType()==(CellType.LABEL))    
			        		{
		        				String s=readsheet.getCell(j,i).getContents();
		        				//System.out.println(s);
			        			Label Name=new Label(j,k,s);
			        			k++;
			        			System.out.println(Name.getContents());
			        			sheet1.addCell(Name);
			        			 
			        		}
		        			else if(readsheet.getCell(j,i).getType()==(CellType.NUMBER)) 
			        		{   
	                      
			        			String s=readsheet.getCell(j,i).getContents();      
			        			Label Name1=new Label(j,k,s);
			        			k++;
			        			System.out.println(Name1.getContents());
			        			sheet1.addCell(Name1);
			        			//System.out.println(s);
			        		}*/
	        			String s=readsheet.getCell(j,i).getContents();      

		        		   
                      if(i%2!=0) 
                      {
		        			Label Name1=new Label(j,k, s);
		        			k++;
		        			System.out.println(Name1.getString());
		        	
		        			
		        			sheet1.addCell(Name1);
		        			//System.out.println(s);
		        			//FileOutputStream outputStream = new FileOutputStream("TestExcel.xls");
		        			
		        			//workbook.Write(outputStream)   
		        	            
                      }
		        				        		          
		        	}
		        }
		        workbook1.write();   
		        workbook1.close();
            }
        	catch(ArrayIndexOutOfBoundsException e)
        	{
        		System.out.println("Array Error");
        	}        
            catch(IOException e)
            {
             e.printStackTrace();
            }
            catch(BiffException e)
            {
             e.printStackTrace();
            }
		}
    }


Well with the above code i am able to copy odd rows but along with that in the remaining rows old values are reflecting as it is which should not present in the copied file.
Posted
Updated 27-Sep-15 2:23am
v3
Comments
Richard Deeming 24-Sep-15 8:33am    
"Not working" is not enough information for anyone to diagnose the problem.

Use the "Improve question" button to update your question with the missing details. Include the full details of any exceptions, and a proper description of the problem.
Maciej Los 26-Sep-15 18:43pm    
What's the question?
atul_kadam21 27-Sep-15 3:57am    
Well with the above code i am able to copy odd rows but along with that in the remaining rows old values are reflecting as it is which should not present in the copied file.
Patrice T 27-Sep-15 9:58am    
Can you show a sample data along with result wanted and actual result ?
atul_kadam21 29-Sep-15 8:44am    
ahsan_khan@yahoo.com prateek@tmail.com
vipulkadam@gmail.com vardhan@hmail.com
ajayshinde@hmail.com vikas@jmail.com
hardik@email.com antarapatil@nmail.com
prajyotlad@kmail.com bob@amail.com
1 2
3 4
@ %
& !


Above is my input data file in excel and i want to fetch only odd rows in my newly created excel as a copy which is not happening i am getting following results--

ahsan_khan@yahoo.com prateek@tmail.com
ajayshinde@hmail.com vikas@jmail.com
prajyotlad@kmail.com bob@amail.com
3 4
& !
1 2
3 4
@ %
& !

wherein i want only 5 rows to be printed in my results as odd rows are 5...

1 solution

Obviously, your own code is like a blackbox to you. It don't do what you expect.
What follow is not directly a solution to your problem, but a key that will help you to understand by yourself what is wrong.
The debugger is your friend. It will show you what your code is really doing.
Follow the execution, check variables and you will see that there is a point where it stop doing what you expect.

This code will skip even rows
Java
for(i=1; i<readsheet.getRows(); i+=2)


Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900