sábado, 9 de fevereiro de 2008

Salvando dados sql para excel,world e etc

Fala galera...
No meu recente trabalho, eu estava procurando algo para jogar meus dados do banco para o excel..
Fuçando aqui e ali , achei algo muito interessante para jogar meus dados para excel e etc.

ai vai o arquivo
try {
StringBuffer contenu; //// acho que seria melhor aqui usar o StringBuilder
contenu = new StringBuffer("");
ResultSet rs; rs= /// aqui vc coloca seu sql...cara...
ResultSetMetaData rsMeta = rs.getMetaData();
for(int i = 1; i<=rsMeta.getColumnCount(); i++){
contenu.append(rsMeta.getColumnLabel(i)+"\t"); /// nesta linha imprime somente os nome dos campos da tabela
}
contenu.append("\n"); // and we put all de data in the StringBuffer
rs.beforeFirst();
while(rs.next()){
for(int i = 1; i<=rsMeta.getColumnCount(); i++){
contenu.append(rs.getString(i)+"\t"); /// aqui mostra todos os dados
} contenu.append("\n");

} //end while //now, just save the StringBuffer in a file
FileWriter excelFile = new FileWriter("myResultSet.xls"); // fraga o esquema excelFile.write(new String(contenu)); //aqui ele passa a String para salvar
excelFile.close();
} catch (Exception ex) {
ex.printStackTrace();

}
*******************************************************************
Ai galera...agora é com vcs... salvem em pdf, excel e etc.
Vamo que vamo....!!!!

2 comentários:

Anônimo disse...

Hello. This post is likeable, and your blog is very interesting, congratulations :-). I will add in my blogroll =). If possible gives a last there on my blog, it is about the Notebook, I hope you enjoy. The address is http://notebooks-brasil.blogspot.com. A hug.

Unknown disse...

Conheçam o portal DevMedia!
JAVA - SQL - .net - Delphi e mais
http://www.devmedia.com.br