[RESOLVIDO]Ajuda com POI ou Jexcel

6 respostas
I

Bom dia, eu estou fazendo um programa que compara o conteúdo das celulas do excel. Porem eu nunca usei POI, nem Jexcel. Já faz um tempo que eu to tentando, mas a falta de experiencia atrapalha. Eu consigo ler o arquivo e escreve nele usando POI, mas não tenho ideia de como comparar. Já com Jexcel eu consigo ler, mas não consigo escrever e tento comparar, mas ele compara errado. Alguém poderia me ajudar?
Eu tenho preferência por POI, mas se alguém puder me ajudar usa Jexcel não tem problema.

6 Respostas

E

debugar resolve

coloque seu codigo aew pra mim ver

I

Eu tenho esse com POI

public class Novamente extends JFrame{
	JButton procura;
	JFileChooser arq1;
	Sheet st;
	Permanente per;
	FileOutputStream out;
	String ji;
	

public Novamente() {	

		setLayout(null);
		
	
		
		procura = new JButton("Procurar");
		procura.setBounds(100, 50, 80, 30);
		add(procura);
		setVisible(true);
		setSize(500, 300);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		
		procura.addActionListener(new ActionListener() {
			
			
			public void actionPerformed(ActionEvent arg0) {
				  out = null;  
				 
				try{

					
			          JFileChooser fopen = new JFileChooser( );
			          fopen.showOpenDialog(null);
			          FileInputStream hi = new FileInputStream(fopen.getSelectedFile());
			          POIFSFileSystem arqq= new POIFSFileSystem(hi); 
			          HSSFWorkbook hwb = new HSSFWorkbook(arqq);

			        
			          Sheet sheet1 = hwb.getSheetAt(0);
			            
			     
			        	    for (Row row:sheet1) {
			        	  
			       
			        	for (Cell cell : row) {
			        		System.out.print(cell.toString());
					            	System.out.print("\t");
					            }
					       System.out.print("\n");
			        	  }
			           
			        	
			         
		
		try {
			ji=new String("/home/Documentos/workbook.xls");
			FileOutputStream out = new FileOutputStream(ji);

	        hwb.write(out);
	        
	       
			File file = new File(ji);
	        Desktop.getDesktop().open(file);
			
		} catch (Exception e) {
			// TODO: handle exception
		}
				 }catch(Exception e){
				        e.printStackTrace();
				    }
			}
		});
	}

}

E tambem tenho esse usando Jexcel

class Permanente {
	 Cell[] cel,cel2;
	 
		String a,b,conteud,conteudo;
		int i,ka,j;
		Label d;
		ComJXL com;
	public Permanente() {
		
		try {
			j=0;
			
			 Workbook workbook = Workbook.getWorkbook(new File("Arquivo.xls"));
		       
		        Sheet sheet = workbook.getSheet(0);
		    
		        File diret = new File("/home/Documentos");
		    	  File arq = new File(diret,"nossa.xls");
		    	  FileOutputStream fileWriter = new FileOutputStream(arq,false);
		    	  PrintWriter pw = new PrintWriter(fileWriter);
		    	  
		    	  

		        int linhas = sheet.getRows();
		        
				int colu = sheet.getColumns();
		   
				for(int i = 0;i<linhas;i++){
					
					
					cel=sheet.getRow(i);
					
					{		 
						
						for(int j=0;j<colu;++j){
		    			cel2=sheet.getColumn(j);
		    			conteud= cel[j].getContents();
		    			conteudo= cel2[i].getContents();
		    			
		    			
		    			 if (cel[i].getContents().equals(cel2[j].getContents())) {
		    			System.out.println(conteud+" -OK- "+conteud);
		    			
		    			 }
		    			}
		    			
		    			
		    		}  
		        }
				pw.write(conteud);
	    		  
			    pw.flush();
				
				 pw.close();
				
			
			
		} catch (Exception e) {
			
			
		}
		
	}

}
E

cara vc tem a celula, vc consegue pegar no poi celltype, getCellNum,getNumericCellValue e outros não?

se for numero primitivo compare assim ex: (getCellNum == getCellNum)
se for um objeto vc pode usar ‘compareTo’, se for String use o equals
acho que isso resolve
falow

ssh

com o apache POI:

Row row = sheet.getRow(2);
    Cell cell = row.getCell(3);
    Cell cell1 = row.getCell(4);
    if (cell.equalsIgnoreCase(cell1))
        //do something
I

Mas ai ele vai ficar como o Jexcel, comparando a mesma celula de forma que sempre sera igual. Eu queria uma forma de pegar e comparar uma linha e coluna. Mas como todos os valores estao salvos na mesma variavel eu nao estou conseguindo. E eu nao posso colocar a posiçao pois o arquivo xls tem mais de 5 mil linhas

I

Achei a solução eu usei Jxl mesmo, pode não ter sido a melhor resposta, mas foi a que funcionou.

linhas = sheet.getRows();
		        
   colu = sheet.getColumns();
				
		   
				
				for(int i = 0;i<linhas;i++){
				
						for(int j=0;j<colu;j++){
		    		
		    		   	          tu=sheet.getCell(j,i);
		    			
		    			
						
				}  
						if (sheet.getCell(0,i).getContents().equals(sheet.getCell(1,i).getContents())) {
    				                System.out.println(sheet.getCell(0,i).getContents()+" -OK- "+sheet.getCell(1,i).getContents());
    				
    				}
		        }
				
				
					
					for(int j=0;j<colu;j++){
						for(int i = 0;i<linhas;i++){
							eu=sheet.getCell(4,i);
							if (sheet.getCell(0,j).getContents().equals(sheet.getCell(1,i).getContents())) {
								System.out.println(sheet.getCell(0,j).getContents()+" -OK- "+sheet.getCell(1,i).getContents());
								
							}
							
						}
					}
Criado 28 de agosto de 2012
Ultima resposta 30 de ago. de 2012
Respostas 6
Participantes 3