Java para Excel

4 respostas
manhanijava

Galera não é de minha autoria…e sim de algo denominado javajato, mas funciona, e como eu me matei pra achar isto achei legal compartilhar

import java.io.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.apache.poi.hssf.usermodel.*;

public class Exell
{

    public Exell()
    {
    }

    public static void exportar(ResultSet rs)
    {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet1 = wb.createSheet("Etiquetas Pimaco");
        int i = 0;
        try
        {
            while(rs.next()) 
            {
                HSSFRow row;
                if(i == 0)
                {
                    row = sheet1.createRow(i);
                    row.createCell((short)0).setCellValue("CODIGO");
                    row.createCell((short)1).setCellValue("DTCADASTRO");
                    row.createCell((short)2).setCellValue("NOME");
                    row.createCell((short)3).setCellValue("ENDERECO");
                    row.createCell((short)4).setCellValue("NUMERO");
                    row.createCell((short)5).setCellValue("COMPLEMENTO");
                    row.createCell((short)6).setCellValue("BAIRRO");
                    row.createCell((short)7).setCellValue("CIDADE");
                    row.createCell((short)8).setCellValue("UF");
                    row.createCell((short)9).setCellValue("CEP");
                    row.createCell((short)10).setCellValue("DTNASCIMENTO");
                    row.createCell((short)11).setCellValue("PAIS");
                    row.createCell((short)12).setCellValue("MATRICULA");
                    row.createCell((short)13).setCellValue("CONTATO");
                    i++;
                }
                row = sheet1.createRow(i);
                row.createCell((short)0).setCellValue((new StringBuilder()).append("").append(rs.getInt("CLIENTES_CODIGO")).toString());
                try
                {
                    row.createCell((short)1).setCellValue(getDataText(Face.getDataConvert((new StringBuilder()).append("").append(rs.getString("CLIENTES_DTCADASTRO")).toString())));
                }
                catch(Exception e)
                {
                    row.createCell((short)1).setCellValue("");
                }
                row.createCell((short)2).setCellValue(rs.getString("CLIENTES_NOME"));
                row.createCell((short)3).setCellValue(rs.getString("CLIENTES_ENDERECO"));
                row.createCell((short)4).setCellValue(rs.getString("CLIENTES_NUMERO"));
                row.createCell((short)5).setCellValue(rs.getString("CLIENTES_COMPLEMENTO"));
                row.createCell((short)6).setCellValue(rs.getString("CLIENTES_BAIRRO"));
                row.createCell((short)7).setCellValue(rs.getString("CLIENTES_CIDADE"));
                row.createCell((short)8).setCellValue(rs.getString("CLIENTES_UF"));
                row.createCell((short)9).setCellValue(rs.getString("CLIENTES_CEP"));
                try
                {
                    row.createCell((short)10).setCellValue(getDataText(Face.getDataConvert((new StringBuilder()).append("").append(rs.getString("CLIENTES_DTNASCIMENTO")).toString())));
                }
                catch(Exception e)
                {
                    row.createCell((short)10).setCellValue("");
                }
                row.createCell((short)11).setCellValue(rs.getString("CLIENTES_PAIS"));
                row.createCell((short)12).setCellValue(rs.getString("CLIENTES_MATRICULA"));
                row.createCell((short)13).setCellValue(rs.getString("CLIENTES_CONTATO"));
                i++;
            }
        }
        catch(SQLException ex)
        {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
        String caminho = "";
        open.setDialogType(1);
        open.setFileSelectionMode(1);
        int op = open.showOpenDialog(null);
        if(op == 0)
        {
            File arq = open.getSelectedFile();
            String path = arq.toString();
            caminho = path;
        }
        try
        {
            FileOutputStream stream = new FileOutputStream((new StringBuilder()).append(caminho).append("\\etiquetas.xls").toString());
            wb.write(stream);
            stream.close();
        }
        catch(IOException e)
        {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
        JOptionPane.showMessageDialog(null, (new StringBuilder()).append("Arquivo de Etiquetas Exportado com sucesso para ' ").append(caminho).append("\\etiquetas.xls '").toString());
    }

    public static String getDataText(Date dataOriginal)
    {
        if(dataOriginal != null)
        {
            SimpleDateFormat formatador = new SimpleDateFormat("yyyy-MM-dd");
            if(formatador.format(dataOriginal).equals("1901-01-01"))
            {
                return "";
            } else
            {
                DateFormat ndf = DateFormat.getDateInstance(2, locBr);
                return ndf.format(dataOriginal);
            }
        } else
        {
            return "";
        }
    }

    private static Locale locBr = new Locale("pt", "BR");
    private static JFileChooser open = new JFileChooser();

}

4 Respostas

luxu

o q isto faz amigo?

manhanijava

você leu o titulo?

S

Você teria algum exemplo de mesclar celula?

Tudo o que eu achei na documentação foi

planilha.addMergedRegion(new CellRangeAddress(x1,y1,x2,y2));

Mas aquele construtor de CellRangeAddress está deprecated.

luxu

li sim veio, e num sei onde isso vai chegar…

você leu o titulo?

Criado 3 de agosto de 2012
Ultima resposta 6 de ago. de 2012
Respostas 4
Participantes 3