Why does not my excel document come out the same as the JFrame?

0

This is the Jframe

and this is the excel

What can I do to make Excel the same Jframe? this is my code

import java.awt.BorderLayout; 
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import java.io.FileOutputStream;


public class Tabla {

private JTable t;
private static DefaultTableModel m;
private int i=0;
private int a=0;
public Tabla(){

//* Inicializar variables *//
JFrame v = new JFrame();
m = new DefaultTableModel();
t = new JTable(m);
t = new JTable(m);
t.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane s = new JScrollPane(t);
JScrollPane s1 = new JScrollPane(t);
s1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
s1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

//datos
m.addColumn("INDICADORES");
m.addColumn("Desarrollo Social");
m.addColumn("Recursos materiales");
m.addColumn("Des.agropecuario");
m.addColumn("Desarrollo economico");
m.addColumn("Educacion y cultura");
m.addColumn("Seguridad Publica");
m.addColumn(" DIF");
m.addColumn("Secretaria general");
m.addColumn("Archivo");
m.addColumn("Registro de lo familiar");
m.addColumn("Catastro");
m.addColumn("Turismo");
m.addColumn("Transparencia");
m.addColumn("Informatica");
m.addColumn("Teseroria");
m.addColumn("Contraloria");
m.addColumn("Conciliacion Municipal");
m.addColumn("Servicios generales");
m.addColumn("Ecologia");
m.addColumn("Reaglamentos y Espect");
m.addColumn("Obras publicas");
m.addColumn("Planeacion");
m.addColumn("Comunicacion social");
m.addColumn("IMDM");
m.addColumn("IMJUV");
m.addColumn("COMUDE");
m.addColumn("Poteccion civil");

m.addRow(new Object[]{"Informe mensual de los avances de POA.",});
m.addRow(new Object[]{"Entrega puntual del reporte mensual."});
m.addRow(new Object[]{"No tener Ausencia de su personal de ningun tipo."});
m.addRow(new Object[]{"Cumplimiento de uninforme institucional completo."});
m.addRow(new Object[]{"Asistencia a reuniones que sean de caracter obligatorio."});
m.addRow(new Object[]{"Cumplimiento de comisiones"});
m.addRow(new Object[]{"No contar con ningun tipo de quejas o notas negativas"});
m.addRow(new Object[]{"Cumplir con los indicadores de la ADM."});
m.addRow(new Object[]{"Cumplir en tiempo y forma sus declaraciones patrimoniales."});
m.addRow(new Object[]{"Cumplir con las evaluacionesdel sistema de control interno"});
m.addRow(new Object[]{"Cumplimiento con los procesos de salida"});
m.addRow(new Object[]{"Entrega de informacion en tiempo y forma."});
m.addRow(new Object[]{"No tener ningun tipo de observacion."});
m.addRow(new Object[]{"Cumplir con los indicadores de PBR."});
m.addRow(new Object[]{"Mantener su area limpia y ordenada."});
m.addRow(new Object[]{"No comer dentro de las areas exepto en cumpleaños."});
m.addRow(new Object[]{"Guardar orden y respeto en las areas."});
m.addRow(new Object[]{"Mantenerse en su area de servicio  ya que seran supervisados."});
m.addRow(new Object[]{"Cumplimiento del reglamentointerno de trabajo."});
//datos

//Diseño
t.setSelectionForeground( Color.white );
t.setSelectionBackground( Color.RED );
v.getContentPane().add(s1,BorderLayout.CENTER);



JPanel pN=new JPanel();
JButton aF = new JButton("Agregar indicador");
pN.add(aF);
aF.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
agregar(1);
} 
});

JButton bF = new JButton("Eliminar indicador");
pN.add(bF);
bF.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
eliminar(2);
} 
});
JButton aC = new JButton("Agrear departamento");
pN.add(aC);
aC.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
agregar(2);
}
});
JButton bC = new JButton("Eliminar departamento");
pN.add(bC);
bC.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
eliminar(1);
}
});

JButton ex = new JButton("Exportar");
pN.add(ex);
ex.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
        Exportar();
    }

private void Exportar() {
    JFileChooser seleccionar = new JFileChooser();
    File archivo;
    if (seleccionar.showDialog(null, "Exportar a Excel") == JFileChooser.APPROVE_OPTION){
        archivo = seleccionar.getSelectedFile();
        int cantFila = m.getRowCount();
        int cantColumna = m.getColumnCount();
        HSSFWorkbook wb;
        wb = new HSSFWorkbook();
        Sheet hoja = ((org.apache.poi.ss.usermodel.Workbook) wb).createSheet(" ");

        try {
            for (int i = 0; i < cantFila; i++) {
                Row fila = hoja.createRow(i + 1);
                for (int j = 0; j < cantColumna; j++) {
                    Cell celda = fila.createCell(j);
                    if (i == -1) {
                        celda.setCellValue(String.valueOf(m.getColumnName(j)));
                    } else {
                        celda.setCellValue(String.valueOf(m.getValueAt(i, j)));
                    }
                    wb.write(new FileOutputStream(archivo + ".xls"));
                }
            }
            JOptionPane.showMessageDialog(null, "Exportacion exitosa");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Vuelve a intentarlo");
        }
    }
}
});


//Diseño
TableColumnModel columnModel = t.getColumnModel();
columnModel.getColumn(0).setPreferredWidth(420);
columnModel.getColumn(1).setPreferredWidth(130);
columnModel.getColumn(2).setPreferredWidth(150);
columnModel.getColumn(3).setPreferredWidth(130);
columnModel.getColumn(4).setPreferredWidth(140);
columnModel.getColumn(5).setPreferredWidth(130);
columnModel.getColumn(6).setPreferredWidth(130);
columnModel.getColumn(7).setPreferredWidth(90);
columnModel.getColumn(8).setPreferredWidth(150);
columnModel.getColumn(9).setPreferredWidth(100);
columnModel.getColumn(10).setPreferredWidth(150);
columnModel.getColumn(11).setPreferredWidth(100);
columnModel.getColumn(12).setPreferredWidth(100);
columnModel.getColumn(13).setPreferredWidth(130);
columnModel.getColumn(14).setPreferredWidth(120);
columnModel.getColumn(15).setPreferredWidth(120);
columnModel.getColumn(16).setPreferredWidth(120);
columnModel.getColumn(17).setPreferredWidth(140);
columnModel.getColumn(18).setPreferredWidth(150);
columnModel.getColumn(19).setPreferredWidth(130);
columnModel.getColumn(20).setPreferredWidth(150);
columnModel.getColumn(21).setPreferredWidth(130);
columnModel.getColumn(22).setPreferredWidth(120);
columnModel.getColumn(23).setPreferredWidth(140);
columnModel.getColumn(24).setPreferredWidth(100);
columnModel.getColumn(25).setPreferredWidth(100);
columnModel.getColumn(26).setPreferredWidth(100);
columnModel.getColumn(27).setPreferredWidth(140);
v.getContentPane().add(pN,BorderLayout.NORTH);
v.pack();
v.setBounds(-10,-8,8000,745);
v.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
v.setVisible(true);
t.setVisible(true);
t.setFont(new java.awt.Font("Arial", 0, 15)); 
}



protected static int getRowCount() {
    // TODO Auto-generated method stub
    return 0;
}



private void eliminar(int caso){
try{
switch(caso){
case 1:
t.removeColumn(t.getColumnModel().getColumn(t.getSelectedColumn()));
break;
case 2:
m.removeRow(t.getSelectedRow());
break;
}
}catch(ArrayIndexOutOfBoundsException aIE){}
}


private void agregar(int caso){
try{
switch(caso){
case 1:
m.addRow(new Object[]{a++,"",""});
break;
case 2:
m.addColumn(i=0);
break;
} 
}catch(ArrayIndexOutOfBoundsException aIE){}
}

public static void main(String [] args){
new Tabla();

        }
    }
    
asked by nataly paola avila pineda 11.07.2018 в 19:34
source

2 answers

0

In for (int i = 0; i < cantFila; i++) change the initialization of i

by for (int i = -1; i < cantFila; i++)

Greetings.

    
answered by 11.07.2018 / 20:56
source
0

The problem is that you do

celda.setCellValue(String.valueOf(m.getColumnName(j)));

(and similar).

Javadoc says :

  

if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString () is returned.

That is, null passes to "null" .

If you assign a cell null , Excel sees it empty / blank, but if you assign "null" , it is a String value that will show (exactly as if you wrote it by hand in the Excel sheet).

celda.setCellValue(m.getColumnName(j) == null ? null : m.getColumnName(j).toString());
    
answered by 11.07.2018 в 19:58