2013-04-16 68 views
0

我想將我的JTable導出到excel文件。爲此,我使用apache POI-SS和apache POI-XSSF。首先,我用sax解析器解析我的xml文件,並將其轉換爲JTable,然後通過單擊導出按鈕,我想將我的JTable導出爲ex​​cel。我創建了2個類,第一個解析xml文件並創建一個Jtable和第二個Excel導出。從JTable到Excel

我的問題是與Excel文件,它僅包含2個元素:第一行包含200000第二行10萬......我試圖找到問題出在哪裏,但不成功..

這裏是類 「JTable_create」:

package jtable_excel11; 

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Vector; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.table.DefaultTableModel; 
import javax.xml.parsers.SAXParser; 
import javax.xml.parsers.SAXParserFactory; 
import org.xml.sax.Attributes; 
import org.xml.sax.SAXException; 
import org.xml.sax.helpers.DefaultHandler; 

public class JTable_create { 

public static Vector<Vector> rowData = new Vector<Vector>(); 
public static Vector<String> rowOne = new Vector<String>(); 
public static Vector<String> columnNames = new Vector<String>(); 
public static void main(String[] args) { 

try { 

SAXParserFactory factory = SAXParserFactory.newInstance(); 
SAXParser saxParser = factory.newSAXParser(); 

DefaultHandler handler; 
handler = new DefaultHandler() { 
boolean bstaff = false; 
boolean bfname = false; 
boolean blname = false; 
boolean bnname = false; 
boolean bsalary = false; 
private int i; 

public void startElement(String uri, String localName,String qName, 
Attributes attributes) throws SAXException { 

System.out.println("Start Element :" + qName); 

if (qName.equalsIgnoreCase("staff")) 
{ 
rowOne = new Vector<String>(); 
bstaff = true; 
} 

if (qName.equalsIgnoreCase("FIRSTNAME")) { 
bfname = true; 
} 

if (qName.equalsIgnoreCase("LASTNAME")) { 
blname = true; 
} 

if (qName.equalsIgnoreCase("NICKNAME")) { 
bnname = true; 
} 

if (qName.equalsIgnoreCase("SALARY")) { 
bsalary = true; 
} 

} 

public void endElement(String uri, String localName, 
String qName) throws SAXException { 

System.out.println("End Element :" + qName); 
if ("staff".equals(qName)){ 
rowData.addElement(rowOne); 
System.out.println("pffffffffff"); 
}; 

} 



@Override 
public void characters(char ch[], int start, int length) { 

if (bfname) { 
String s = new String(ch, start, length); 
rowOne.addElement(s); 
System.out.println("First Name : " + new String(ch, start, length)); 

bfname = false; 

} 

if (blname) { 
rowOne.addElement (new String(ch, start, length)); 
System.out.println("Last Name : " + new String(ch, start, length)); 
blname = false; 

} 

if (bnname) { 
rowOne.addElement (new String(ch, start, length)); 
System.out.println("Nick Name : " + new String(ch, start, length)); 
bnname = false; 

} 

if (bsalary) { 
rowOne.addElement (new String(ch, start, length)); 
System.out.println("Salary : " + new String(ch, start, length)); 
bsalary = false; 

} 

System.out.println("longueur" + rowOne.size()); 
} 
}; 

saxParser.parse("file.xml", handler); 
JFrame frame = new JFrame(); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
columnNames.addElement("firstname"); 
columnNames.addElement("lastname"); 
columnNames.addElement("nickname"); 
columnNames.addElement("salary"); 

DefaultTableModel model = new DefaultTableModel(rowData, columnNames); 
final JTable table = new JTable(model); 


JScrollPane scrollPane = new JScrollPane(table); 

JButton export = new JButton("Export"); 
export.addActionListener(new ActionListener(){ 
public void actionPerformed(ActionEvent evt){ 
try { 
Escel1 exp = new Escel1(); 
exp.exportTable(table, new File("C:\\Documents and    Settings\\Adm\\Bureau\\result.xlsx")); 
} catch (IOException ex) { 
System.out.println(ex.getMessage()); 
ex.printStackTrace(); 
} 
} 
}); 

frame.getContentPane().add("South", export); 
frame.pack(); 
frame.setVisible(true); 

frame.add(scrollPane, BorderLayout.CENTER); 
frame.setSize(300, 150); 
frame.setVisible(true); 
System.out.println("Fini!"); 

} catch (Exception e) { 
e.printStackTrace(); 
} 

} 

} 

而被稱爲 「Excel1」 第二類是如下因素:

package jtable_excel11; 


import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.Set; 
import javax.swing.JTable; 
import javax.swing.table.TableModel; 


import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 


public class Excel1 { 



public Excel1() {} 

public void exportTable(JTable table, File file) throws IOException { 


System.out.println(" c bon !"); 

XSSFWorkbook workbook = new XSSFWorkbook(); 
XSSFSheet sheet = workbook.createSheet("Sample sheet"); 

System.out.println(" workbook crée"); 
TableModel model= table.getModel(); 
HashMap<String, Object[]> data = new HashMap<String, Object[]>(); 

for(int i=0; i<model.getRowCount(); i++){ 
for(int j=0; j< model.getColumnCount(); j++){ 


data.put(i+"",new Object[]{model.getValueAt(i,j).toString()+"\t"}); 
System.out.println(model.getValueAt(i,j)); 
System.out.println(i); 
System.out.println(j); 


} 
//data.put("\n"); 


} 

Set<String> keyset = data.keySet(); 
int rownum = 0; 
for (String key : keyset) { 
Row row = sheet.createRow(rownum++); 
Object[] objArr = data.get(key); 
int cellnum = 0; 
for (Object obj : objArr) { 
Cell cell = row.createCell(cellnum++); 
if (obj instanceof Date) 
cell.setCellValue((Date) obj); 
else if (obj instanceof Boolean) 
cell.setCellValue((Boolean) obj); 
else if (obj instanceof String) 
cell.setCellValue((String) obj); 
else if (obj instanceof Double) 
cell.setCellValue((Double) obj); 
} 
} 

try { 
FileOutputStream out = new FileOutputStream(file); 
workbook.write(out); 
out.close(); 
System.out.println("Excel written successfully.."); 

} catch (FileNotFoundException e) { 
e.printStackTrace(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 

} 
} 

,這裏是我的XML文件:

<?xml version="1.0" encoding="UTF-8"?> 
<company> 
<staff > 
<firstname>yong</firstname> 
<lastname>mook kim</lastname> 
<nickname>mkyong</nickname> 
<salary>100000</salary> 
</staff> 
<staff > 
<firstname>low</firstname> 
<lastname>yin fong</lastname> 
<nickname>fong fong</nickname> 
<salary>200000</salary> 
</staff> 
</company> 

在Excel文件我只有2,其分別含有20萬和10萬行。

任何想法我怎麼能解決這個問題?在此先感謝

+0

1)這是什麼都處理[tag:hashmap]? 2)爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –

回答

0

我想你可以出口一個JTable通過使其簡單的filtype:「。csv」 Excel可以蘆葦它。

一個簡單的exaple是

kevin,johnson,49 
julia,roberts,56 

我認爲你正在使用一個JTable作爲表而不是樹(XML是樹的類型)

好運