Ler Planilha do EXCEL e salvar colunas em estrutura de XML [RESOLVIDO]

8 respostas
P

Pessoal,

Queria saber como leio um arquivo excel (2 colunas específicas) e salvo em estrutura de XML já definida:

<markers>
   <marker lat="-23.52904" lng="-46.64592" descr="teste1"/>
   <marker lat="-23.52895" lng="-46.64581" descr="teste2"/>
</markers>

8 Respostas

E

Dê uma olhada no Apache POI.

http://poi.apache.org/

P

ler o excel beleza… só falta salvar em xml

E

Você sabe como fazer e deu erro ou vc naum tem idéia de como começar?
Se não souber, existem APIs q fazem a gravação de XMLs. Temos o xstrem, dom e sax, acho que aqui no forum tem bastante coisa sobre estes assuntos.

P

Eu nem sei por onde começar…rsrs

beleza, vou dar uma lida…

valeu

Rodrigo_Sasaki

Já avaliou a possibilidade de salvar a planilha como XML? O Excel te dá essa opção.

P

não da… quero deixar nesse padrão específico maker

P
package br.com.localizador;

import java.io.FileWriter;
import java.io.IOException;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

public class GeradorXML {

	public void geraXML(String latitude, String longitude, String descr) {
		Document doc = new Document();
		XMLOutputter xout = new XMLOutputter();
		
		Element markers = new Element("markers");
		Element marker = new Element("marker");

		// "Setando" os atributos
		marker.setAttribute("lat", latitude);

		// "Setando" outro atributo agora utilizando da classe Attribute
		Attribute prioridade = new Attribute("lng", longitude);
		marker.setAttribute(prioridade);

		// "Setando" outro atributo agora utilizando da classe Attribute
		Attribute descricao = new Attribute("descr", descr);
		marker.setAttribute(descricao);

		markers.addContent(marker);

		// Criando o documento XML (montado)
		doc.setRootElement(markers);
		
		
		try {
			// Criando o arquivo de saida
			FileWriter aquivo = new FileWriter("c://arquivoXML.xml");
			// Imprimindo o XML no arquivo
			xout.output(doc, aquivo);
			System.out.println("Arquivo XML Gerado");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
P

Resolvi assim:

package br.com.localizador;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

public class GeradorXML {
	private static final int LINHA_INICIAL = 5;
	private Workbook workbook;
	private Sheet sheet;

	public GeradorXML(File vr) throws Exception,
			IOException, BiffException, ClassNotFoundException, SQLException {
		this.workbook = Workbook.getWorkbook(vr);
		this.sheet = workbook.getSheet(0);
		VerificadorCtr(vr);
	}

	public void VerificadorCtr(File vr) {
		int nuLinhas = sheet.getRows();
		Document doc = new Document();
		Element markers = new Element("markers");
		for (int nuLinhaAtual = LINHA_INICIAL; nuLinhaAtual < nuLinhas; nuLinhaAtual++) {
			LatitudeLongitude latlng = carregarLinha(nuLinhaAtual);

				if(!(latlng.getLatitude().equals("0") && latlng.getLongitude().equals("0"))){
				
					String descricao = Integer.toString(nuLinhaAtual);
					String latitude = latlng.getLatitude().replace(",",".");
					String longitude = latlng.getLongitude().replace(",",".");
					
					Element marker = new Element("marker");

					// "Setando" os atributos
					marker.setAttribute("lat", latitude);

					// "Setando" outro atributo agora utilizando da classe Attribute
					Attribute prioridade = new Attribute("lng", longitude);
					marker.setAttribute(prioridade);

					// "Setando" outro atributo agora utilizando da classe Attribute
					Attribute descr = new Attribute("descr", descricao);
					marker.setAttribute(descr);

					markers.addContent(marker);

					// Criando o documento XML (montado)
					doc.setRootElement(markers);
								       
					System.out.println("inserindo coordenadas: " + " Latitude: "+ latitude + " Longitude: " + longitude + " Linha: "+ descricao);
				
				}
		}
		
		XMLOutputter xout = new XMLOutputter();       

        try {
              //Criando o arquivo de saida
              FileWriter arquivo = new FileWriter(new File("C://data.xml"));

              //Imprimindo o XML no arquivo
              xout.output(doc, arquivo);

        } catch (IOException e) {
              e.printStackTrace();
        }   
	}

	private LatitudeLongitude carregarLinha(int nuLinhaAtual) {
		LatitudeLongitude linhaRelatorio = new LatitudeLongitude();
		Cell[] celulas = sheet.getRow(nuLinhaAtual);

		for (int nuCelulaAtual = 0; nuCelulaAtual < celulas.length; nuCelulaAtual++) {
			Cell celula = celulas[nuCelulaAtual];
			CelulasEnum celulaXML = CelulasEnum
					.obterCelula(nuCelulaAtual);

			switch (celulaXML) {
			case LATITUDE:
				linhaRelatorio.setLatitude(celula.getContents());
				break;
			case LONGITUDE:
				linhaRelatorio.setLongitude(celula.getContents());
				break;
			}
		}

		return linhaRelatorio;
	}

}
Criado 14 de dezembro de 2012
Ultima resposta 19 de dez. de 2012
Respostas 8
Participantes 3