Erro programa Apache POI ler dados de uma planilha e escrever em outra

0 respostas
ibotirama

Fiz um programa com para ler uma planilha e escrever os dados em outra porém quando aumento o número de colunas, ele não copia os dados das primeiras colunas :

package importaanalise.classes;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.*;

public class Importar {
    public static HSSFWorkbook getWorkBook( String nomeArquivo ) throws IOException{
        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(nomeArquivo));
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        return wb;
    }
    
    public static void criaPlanilha( String nome) throws IOException{
        FileOutputStream fsDestino = new FileOutputStream(nome);
        Workbook wbDestino = new HSSFWorkbook();
        Sheet sheet = wbDestino.createSheet("Planilha Dados");
        wbDestino.write(fsDestino);
    }
        
    public static void main(String[] args) throws IOException{
        String arquivoOrigem  = "C:/Sistemas/ImportaAnalise/PlanilhaOrigem.xls";
        String arquivoDestino = "C:/Sistemas/ImportaAnalise/PlanilhaDestino.xls";
        
        POIFSFileSystem fsOrigem = new POIFSFileSystem(new FileInputStream(arquivoOrigem));
        HSSFWorkbook wbOrigem = new HSSFWorkbook(fsOrigem);
        HSSFSheet sheet1 = wbOrigem.getSheetAt(0);
        
        File arq = new File(arquivoDestino);
        if (!arq.exists()){
            criaPlanilha(arquivoDestino);
        }
        
        POIFSFileSystem fsDestino = new POIFSFileSystem( new FileInputStream(arquivoDestino) );
        Workbook wbDestino = new HSSFWorkbook(fsDestino);  
        Sheet sheet2 = wbDestino.getSheetAt(0);
        int ultimaLinha = sheet2.getLastRowNum();
        
        CellStyle cellStyle = wbDestino.createCellStyle();
        CreationHelper creationHelper = wbDestino.getCreationHelper();
        cellStyle.setDataFormat( creationHelper.createDataFormat().getFormat("dd/mm/yyyy") );
        
        for (Row row : sheet1){
            for (Cell cell: row){
                CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
		System.out.print(cellRef.formatAsString());
		System.out.print(" - ");
		
		switch(cell.getCellType()) {
                      case Cell.CELL_TYPE_STRING:
                        String valor = cell.getStringCellValue();
                        Cell cellNova = sheet2.createRow(cell.getRowIndex()+ultimaLinha).createCell((short) cell.getColumnIndex());
                        cellNova.setCellValue(valor);
                        System.out.println(valor);
                        break;
                      case Cell.CELL_TYPE_NUMERIC:
                        if(DateUtil.isCellDateFormatted(cell)) {
                          System.out.println(cell.getDateCellValue());
                          
                          sheet2.createRow(cell.getRowIndex()+ultimaLinha).createCell((short) cell.getColumnIndex()).setCellValue(cell.getDateCellValue());
                          sheet2.getRow(cell.getRowIndex()+ultimaLinha).getCell((short) cell.getColumnIndex()).setCellStyle(cellStyle);
                        } else {
                          System.out.println(cell.getNumericCellValue());
                          sheet2.createRow(cell.getRowIndex()+ultimaLinha).createCell((short) cell.getColumnIndex()).setCellValue(cell.getNumericCellValue());
                        }
                        break;
                      case Cell.CELL_TYPE_BOOLEAN:
                        System.out.println(cell.getBooleanCellValue());
                        sheet2.createRow(cell.getRowIndex()+ultimaLinha).createCell((short) cell.getColumnIndex()).setCellValue(cell.getBooleanCellValue());
                        break;
                      case Cell.CELL_TYPE_FORMULA:
                        System.out.println(cell.getCellFormula());
                        sheet2.createRow(cell.getRowIndex()+ultimaLinha).createCell((short) cell.getColumnIndex()).setCellValue("="+cell.getCellFormula());
                        break;
                      default:
                        System.out.println();
		}
            }
        }
        FileOutputStream fsGravar = new FileOutputStream(arquivoDestino);
        wbDestino.write(fsGravar);
        fsGravar.close();
        
    }
}
Criado 28 de setembro de 2011
Respostas 0
Participantes 1