[RESOLVIDO] Problemas ao usar Apache POI (arquivo XLS está sendo gravado errado)

2 respostas
E

Olá!

Estou tentando gravar alguns dados em arquivo XLS, mas estou encontrando um problema beeeemmm estranho.
Só pra vcs entenderem, vou mandar o meu código aqui:

package br.com.tdta.utils.exportacao.microsoft;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

/**
 *
 * @author Eliangela
 */
public class XlsFile {

	private FileInputStream arqModelo;
	private FileOutputStream arqSaida;
	private Workbook workbook;

	public XlsFile(String arqSaida) throws InvalidFormatException, IOException {
		this.arqModelo = new FileInputStream(getClass().getResource(
				"/br/com/tdta/utils/exportacao/microsoft/models/arquivo.xls").getPath());
		this.arqSaida = new FileOutputStream(arqSaida);
		workbook = WorkbookFactory.create(arqModelo);
	}

	public void writeLine(String... dados) throws IOException {
		Sheet sheet = workbook.getSheetAt(0);
		Row row = sheet.createRow(sheet.getLastRowNum());

		for (int col = 0; col < dados.length; col++) {
			Cell cell = row.createCell(col);
			cell.setCellValue(dados[col]);
		}

		workbook.write(arqSaida);
	}

	public void writeDados(String[][] dados) throws IOException {
		Sheet sheet = workbook.getSheetAt(0);

		for (int linha = 0; linha < dados.length; linha++) {
			Row row = sheet.createRow(linha);
			for (int coluna = 0; coluna < dados[linha].length; coluna++) {
				Cell cell = row.createCell(coluna);
				cell.setCellValue(dados[linha][coluna]);
			}
		}

		workbook.write(arqSaida);
	}

	public void close() throws IOException {
		arqModelo.close();
		arqSaida.close();
	}

	public static void main(String[] args) throws InvalidFormatException, IOException {
		String[][] ss = new String[30000][5];
		String conteudo = "aaaaaa";

		for (int linha = 0; linha < ss.length; linha++) {
			for (int col = 0; col < ss[linha].length; col++) {
				ss[linha][col] = conteudo;
			}
		}

		XlsFile xls = new XlsFile("c:/teste.xls");
		xls.writeLine("esta", "é", "a", "primeira", "linha");
		xls.writeDados(ss);
		xls.close();
	}
}
Quando eu executo este código, não acontece nenhum erro.

Daí eu vou na pasta onde o arquivo está gravado, e o Windows mostra que o arquivo está .

No caso desse exemplo, meu arquivo aparece com 2,67 Mb. (Até  tudo bem)

O problema está quando eu abro esse arquivo gerado: O excel mostra um arquivo praticamente vazio, com apenas o conteúdo que eu inseri em xls.writeLine("esta", "é", "a", "primeira", "linha" ) ;

O resto que eu inseri não aparece.

Daí, quando eu fecho o arquivo (sem salvar ou fazer qualquer modificação), o Windows mostra o tamanho do arquivo como sendo 6kb, ou seja, o arquivo diminuiu do nada!

Queria saber o que eu estou fazendo de errado aqui.

Obrigada

2 Respostas

E

alguém??

E
Resolvi fazendo uma gambi aqui:
package br.com.tdta.utils.exportacao.microsoft;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

/**
 *
 * @author Eliangela
 */
public class XlsFile {

	private FileOutputStream arqSaida;
	private HSSFWorkbook workbook;
	private HSSFSheet principalSheet;
	private List<String[]> lista = new ArrayList<String[]>();

	public XlsFile(String nomePlanilha, String arqSaida) throws InvalidFormatException, IOException {
		this.arqSaida = new FileOutputStream(arqSaida);
		workbook = new HSSFWorkbook();
		principalSheet = workbook.createSheet(nomePlanilha);
	}

	public XlsFile(String arqSaida) throws InvalidFormatException, IOException {
		this("sheet1", arqSaida);
	}

	public void writeLine(String... dados) throws IOException {
		lista.add(dados);
	}

	public void export(String[][] dados) throws IOException {
		lista.addAll(Arrays.asList(dados));
	}

	public void close() throws IOException {
		flush();
		arqSaida.close();
	}

	private void flush() throws IOException {
		String dados[] = null;
		for (int linha = 0; linha < lista.size(); linha++) {
			dados = lista.get(linha);
			HSSFRow row = principalSheet.createRow(linha);
			for (int col = 0; col < dados.length; col++) {
				HSSFCell cell = row.createCell(col);
				cell.setCellValue(dados[col]);
			}
		}
		workbook.write(arqSaida);
		arqSaida.flush();

		lista = new ArrayList<String[]>();
	}

	public static void main(String[] args) throws InvalidFormatException, IOException {
		String[][] ss = new String[30000][6];
		String conteudo = "aaaaaa";

		for (int linha = 0; linha < ss.length; linha++) {
			for (int col = 0; col < ss[linha].length; col++) {
				ss[linha][col] = conteudo;
			}
		}

		XlsFile xls = new XlsFile("c:/teste.xls");
		xls.writeLine("esta", "é", "a", "primeira", "linha");
		xls.writeLine("esta", "é", "a", "segunda", "linha");
		xls.writeLine("esta", "é", "a", "terceira", "linha");
		xls.writeLine("esta", "é", "a", "quarta", "linha");
		xls.export(ss);
		xls.close();

	}
}

Mas ainda não entendi porque estava acontecendo aquilo com meu XLS.
Se alguem conhecer uma maneira melhor, por favor, compartilhem!

Obrigada

Criado 13 de julho de 2011
Ultima resposta 14 de jul. de 2011
Respostas 2
Participantes 1