Return-Path: Delivered-To: apmail-jakarta-poi-user-archive@www.apache.org Received: (qmail 14662 invoked from network); 3 Oct 2005 23:25:32 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 3 Oct 2005 23:25:32 -0000 Received: (qmail 20036 invoked by uid 500); 3 Oct 2005 23:25:20 -0000 Delivered-To: apmail-jakarta-poi-user-archive@jakarta.apache.org Received: (qmail 20017 invoked by uid 500); 3 Oct 2005 23:25:20 -0000 Mailing-List: contact poi-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "POI Users List" Reply-To: "POI Users List" Delivered-To: mailing list poi-user@jakarta.apache.org Received: (qmail 20006 invoked by uid 99); 3 Oct 2005 23:25:20 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 03 Oct 2005 16:25:20 -0700 X-ASF-Spam-Status: No, hits=0.2 required=10.0 tests=NO_REAL_NAME X-Spam-Check-By: apache.org Received-SPF: neutral (asf.osuosl.org: local policy) Received: from [66.35.175.110] (HELO set.superlinksoftware.com) (66.35.175.110) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 03 Oct 2005 16:25:24 -0700 Received: from [192.168.1.100] (cpe-024-211-135-006.nc.res.rr.com 24.211.135.6) by set.superlinksoftware.com/JBossMail 1.0M3 (66.35.175.110) with SMTP id 112837868970653.61419095574331; Mon, 3 Oct 2005 18:31:29 -0400 (EDT) Message-ID: <72351607.1128378716434.JavaMail.root@set.superlinksoftware.com> Date: Mon, 03 Oct 2005 19:24:50 -0400 From: acoliver@apache.org Reply-To: acoliver@apache.org To: poi-user@jakarta.apache.org Subject: Re: wrong Cell Value, help ! In-Reply-To: <43419F5B.2050601@tiscali.it> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit User-Agent: Mozilla Thunderbird 1.0.6 (Macintosh/20050716) X-Accept-Language: en-us, en References: <43419F5B.2050601@tiscali.it> X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N While it doesn't allow you to save, you do realize we have a renderer/table thing in scratchpad called "sheetviewer"...don't you? Daniele Artini wrote: > 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 > 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 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: poi-user-unsubscribe@jakarta.apache.org > Mailing List: http://jakarta.apache.org/site/mail2.html#poi > The Apache Jakarta Poi Project: http://jakarta.apache.org/poi/ > > -- Andrew C. Oliver SuperLink Software, Inc. Java to Excel using POI http://www.superlinksoftware.com/services/poi Commercial support including features added/implemented, bugs fixed. --------------------------------------------------------------------- To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org Mailing List: http://jakarta.apache.org/site/mail2.html#poi The Apache Jakarta Poi Project: http://jakarta.apache.org/poi/