Hi,
with the following simple program,inside the test.xls file generated
the values of the all cell are repeated many times !
what's worng ?
please help me
TestExcel.java:
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class TestExcel extends JPanel implements ActionListener {
private boolean DEBUG = false;
private JTable table;
private JButton fileButton=new JButton("FILE");
public TestExcel() {
super(new GridLayout(1,0));
String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
Object[][] data = {
{"Mary", "Campione",
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Knitting", new Integer(2), new Boolean(false)},
{"Sharon", "Zakhour",
"Speed reading", new Integer(20), new Boolean(true)},
{"Philip", "Milne",
"Pool", new Integer(10), new Boolean(false)}
};
table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
this.fileButton.setActionCommand("FILE");
this.fileButton.addActionListener(this);
if (DEBUG) {
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
printDebugData(table);
}
});
}
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
this.add(this.fileButton);
//Add the scroll pane to this panel.
add(scrollPane);
}
private void printDebugData(JTable table) {
int numRows = table.getRowCount();
int numCols = table.getColumnCount();
javax.swing.table.TableModel model = table.getModel();
System.out.println("Value of data: ");
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + model.getValueAt(i, j));
}
System.out.println();
}
System.out.println("--------------------------");
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
TestExcel newContentPane = new TestExcel();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "FILE") {
new ExcelSaveJTable(this.table);
}
}
public class ExcelSaveJTable {
private int MAX_ROW=65000;
private short FATTORE_CONV_WIDTH=60;
private HSSFSheet foglioExcel;
private HSSFWorkbook documentoExcel;
private int colonne;
private int righeTab;
private JTable tabellaDati;
private HSSFCellStyle stileCella,stileCellaInt;
/**
*
* @param prmJTable JTable da cui leggere i dati
*
*/
public ExcelSaveJTable(JTable prmTabella){
documentoExcel = new HSSFWorkbook();
foglioExcel = documentoExcel.createSheet("test");
File FileOutput=new File("test.xls");
righeTab=Math.min(this.MAX_ROW,prmTabella.getRowCount());
colonne=prmTabella.getColumnCount();
tabellaDati=prmTabella;
stileCella = documentoExcel.createCellStyle();
stileCella.setAlignment(HSSFCellStyle.ALIGN_FILL);
stileCellaInt=documentoExcel.createCellStyle();
stileCellaInt.setAlignment(HSSFCellStyle.ALIGN_FILL);
scriviIntestazione();
int cnt=1;
while (cnt<=righeTab){
scriviRigoDati(cnt);
cnt++;
}
//scrittura del file
try{
FileOutputStream fileExcelOut = new
FileOutputStream(FileOutput);
documentoExcel.write(fileExcelOut);
fileExcelOut.close();
}catch (IOException e) {
System.out.println("Il file non e' stato scritto !
"+e.getMessage());
}
}
private void scriviIntestazione(){
//scrivo l'intestazione delle colonne
HSSFRow rigo = foglioExcel.createRow(0);
short i;
HSSFFont fontIntestazione = documentoExcel.createFont();
fontIntestazione.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
stileCellaInt.setFont(fontIntestazione);
for (i=0; i<colonne;i++){
foglioExcel.setColumnWidth((short)(i),(short)(tabellaDati.getColumnModel().getColumn(i).getPreferredWidth()*FATTORE_CONV_WIDTH));
HSSFCell cellaIntestazione = rigo.createCell((short)i);
String str=tabellaDati.getColumnName(i);
cellaIntestazione.setCellValue(str);
stileCellaInt.setBorderBottom(HSSFCellStyle.BORDER_THIN);
stileCellaInt.setBorderTop(HSSFCellStyle.BORDER_THIN);
stileCellaInt.setBorderLeft(HSSFCellStyle.BORDER_THIN);
stileCellaInt.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellaIntestazione.setCellStyle(stileCellaInt);
}
}
private void scriviRigoDati(int cnt){
HSSFRow rigoDati = foglioExcel.createRow(cnt);
int i;
for (i=0; i<colonne;i++){
HSSFCell cella = rigoDati.createCell((short)i);
Object datoCellaJTable
=tabellaDati.getValueAt(cnt-1,i);
if (datoCellaJTable instanceof Boolean){
cella.setCellValue(((Boolean)datoCellaJTable).booleanValue());
} else if (datoCellaJTable instanceof Calendar){
cella.setCellValue((Calendar)datoCellaJTable);
} else if (datoCellaJTable instanceof Double){
cella.setCellValue(((Double)datoCellaJTable).doubleValue());
} else if (datoCellaJTable instanceof Short){
cella.setCellValue(((Short)datoCellaJTable).shortValue());
} else if (datoCellaJTable instanceof Integer){
cella.setCellValue(((Integer)datoCellaJTable).intValue());
} else { String valstr=(String)datoCellaJTable.toString();
if ((!(valstr==null)) && (valstr.length()>0)){
cella.setCellValue(valstr);
} else {
cella.setCellValue("");
}
}
cella.setCellStyle(stileCella);
}
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
Mailing List: http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project: http://jakarta.apache.org/poi/