2014-02-21 53 views
2

您好,我正在嘗試使用HSSFWorkbook將Jtable數據導出到Excel工作表中。並且我得到了Table的所有內容,但是我沒有獲得Table Headers,任何人都可以爲此提供幫助。使用HSSFWorkbook將Jtable導出到Excelsheet中

這裏的命令用於獲取Jtable的內容。

 try { 
        HSSFWorkbook fWorkbook = new HSSFWorkbook(); 
        HSSFSheet fSheet = fWorkbook.createSheet("new Sheet"); 
        HSSFFont sheetTitleFont = fWorkbook.createFont(); 
        File file = new File("/home/kishan/NetBeansProjects/JavaChecking/src/com/verve/SwingChecking/book.xls"); 
        HSSFCellStyle cellStyle = fWorkbook.createCellStyle(); 

        sheetTitleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 
        //sheetTitleFont.setColor(); 
        TableModel model = jTable1.getModel(); 


        for (int i = 0; i < model.getRowCount(); i++) { 
         HSSFRow fRow = fSheet.createRow((short) i); 
         for (int j = 0; j < model.getColumnCount(); j++) { 
          HSSFCell cell = fRow.createCell((short) j); 
          cell.setCellValue(model.getValueAt(i, j).toString()); 
          cell.setCellStyle(cellStyle); 

         } 

        } 
    FileOutputStream fileOutputStream; 
       fileOutputStream = new FileOutputStream(file); 
       BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream); 
       fWorkbook.write(bos); 
       bos.close(); 
       fileOutputStream.close(); 
    }catch(Exception e){ 

     } 



for (int i = 0; i < model.getColumnCount(); i++) { 
       HSSFRow fRow = fSheet.createRow((short) i); 
       for(int j = 0; j < model.getColumnCount(); j++) { 
        HSSFCell cell = fRow.createCell((short) j); 
        cell.setCellValue(model.getValueAt(i, j).toString()); 

        System.out.println(model.getColumnName(j)); 
       } 
      } 

last for循環不是表頭的addind數據。 enter image description here

和我得到這個excel文件

enter image description here 如何與相處表頭?

+0

得到JTableHeader上從XxxTbaleModel/JTable中,取決於來源爲您導出....,一定要在這個表單,這個問題是不是聽命於SO – mKorbel

+0

必須(一點點)困擾官方的Oracle教程 - 如何使用表 – mKorbel

+0

ya last for循環,這是我試圖獲得Tableheader。並附上截圖以清楚說明。 – Krishna

回答

2

這樣的事情您的工作表的第一行中添加列名稱:

TableColumnModel tcm = jTable1.getColumnModel(); 
HSSFRow fRow = fSheet.createRow((short) 0); 

for(int j = 0; j < tcm.getColumnCount(); j++) { 

    HSSFCell cell = fRow.createCell((short) j); 
    cell.setCellValue(tcm.getColumn(j).getHeaderValue().toString());   

} 

您可以先運行它,然後從第二行開始添加表格數據。

+0

雅它的權利,我正在嘗試。:) – Krishna

+0

感謝兄弟我解決了這個參考。 :) – Krishna

+1

我很高興你沒有:) – Marco

2

您只將TableModel中的數據寫入工作簿。該模型不包含表頭。看看JTable.getTableHeader()

例如:

public class JTableExport { 

public static void main(String[] args) { 
    Object[] columnNames = new Object[] {"column1", "column2"}; 
    JTable table = new JTable(new Object[0][0], columnNames); 
    TableColumnModel model = table.getTableHeader().getColumnModel(); 

    for (int i = 0; i < model.getColumnCount(); i++) { 
     System.out.println(model.getColumn(i).getHeaderValue()); 
    } 
} 
} 

這個代碼打印

column1 
column2 
+0

我只是想這個。你可以更新小代碼嗎? – Krishna

+0

我添加了示例代碼。而不是'System.out.println(..)'你需要創建新的'HSSFCell'並將它們添加到你的工作簿 – Markus

+0

雅我現在得到的表頭,但不能添加到Excel表:(但雅 – Krishna

0

對(INT J = 0;Ĵ< tcm.getColumnCount(); J ++){

HSSFCell細胞= fRow.createCell((短)j)的; cell.setCellValue(tcm.getColumn(j).getHeaderValue()。toString());

}

對(INT J = 0;Ĵ< tcm.getRowCount(); J ++){

HSSFCell細胞= fRow.createCell((短)j)的; cell.setCellValue(tcm.getColumn(j).getHeaderValue()。toString());

}

3

這是我在本主題中的答案的HSSF工作簿的實現。

我創建了一個類ExcelWriter然後一個方法編寫器,它需要兩個參數;將使用JTableFileLocation

import java.io.BufferedOutputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import javax.swing.JTable; 
import javax.swing.table.TableColumnModel; 
import javax.swing.table.TableModel; 
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; 

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/** 
* 
* @author oluwajayi 
*/ 
public class ExcelWriter { 

    public static void Writer (JTable jTable1, String Location) throws FileNotFoundException, IOException { 

       HSSFWorkbook fWorkbook = new HSSFWorkbook(); 
       HSSFSheet fSheet = fWorkbook.createSheet("new Sheet"); 
       HSSFFont sheetTitleFont = fWorkbook.createFont(); 
       HSSFCellStyle cellStyle = fWorkbook.createCellStyle(); 
       sheetTitleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 
       //sheetTitleFont.setColor(); 
       TableModel model = jTable1.getModel(); 

       //Get Header 
       TableColumnModel tcm = jTable1.getColumnModel(); 
       HSSFRow hRow = fSheet.createRow((short) 0); 
       for(int j = 0; j < tcm.getColumnCount(); j++) {      
        HSSFCell cell = hRow.createCell((short) j); 
      cell.setCellValue(tcm.getColumn(j).getHeaderValue().toString()); 
        cell.setCellStyle(cellStyle); 
       } 

       //Get Other details 
       for (int i = 0; i < model.getRowCount(); i++) { 
        HSSFRow fRow = fSheet.createRow((short) i+1); 
        for (int j = 0; j < model.getColumnCount(); j++) { 
         HSSFCell cell = fRow.createCell((short) j); 
         cell.setCellValue(model.getValueAt(i, j).toString()); 
         cell.setCellStyle(cellStyle); 
        } 
       } 
      FileOutputStream fileOutputStream; 
      fileOutputStream = new FileOutputStream(Location); 
    try (BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream)) { 
     fWorkbook.write(bos); 
    } 
      fileOutputStream.close(); 
} 
} 
0

我創造了這個代碼:

public void Export() { 

    JFileChooser save = new JFileChooser(); 
    save.setDialogTitle("Save as..."); 
    save.setFileFilter(new FileNameExtensionFilter("xls", "xlsx", "xlsm")); 
    int choose = save.showSaveDialog(null); 

    if(choose == JFileChooser.APPROVE_OPTION) { 
     XSSFWorkbook export = new XSSFWorkbook(); 
     XSSFSheet sheet1 = export.createSheet("new file"); 
     try{ 
      TableModel tableModel = showQuery.getModel(); 

      for(int i=0; i<tableModel.getRowCount(); i++) { 
       XSSFRow newRow = sheet1.createRow(i); 
       for(int j=0; j<tableModel.getColumnCount(); j++) { 
        XSSFCell newCell = newRow.createCell((short) j); 
        if(i==0){ 
         XSSFCellStyle style = export.createCellStyle(); 
         style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex()); 
         style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); 
         style.setBorderBottom(BorderStyle.THIN); 
         style.setBorderTop(BorderStyle.THIN); 
         style.setBorderLeft(BorderStyle.THIN); 
         style.setBorderRight(BorderStyle.THIN); 
         newCell.setCellStyle(style); 
         newCell.setCellValue(tableModel.getColumnName(j)); 
        } else { 
         XSSFCellStyle style = export.createCellStyle(); 
         style.setBorderBottom(BorderStyle.THIN); 
         style.setBorderTop(BorderStyle.THIN); 
         style.setBorderLeft(BorderStyle.THIN); 
         style.setBorderRight(BorderStyle.THIN); 
         newCell.setCellStyle(style); 
        newCell.setCellValue(tableModel.getValueAt(i, j).toString()); 
        } 
       } 
      } 

      FileOutputStream otp = new FileOutputStream(save.getSelectedFile()+".xlsx"); 
      BufferedOutputStream bos = new BufferedOutputStream(otp); 
      export.write(bos); 
      bos.close(); 
      otp.close(); 

      JOptionPane.showMessageDialog(null, "Arquivo exprtado com sucesso!"); 
     }catch(Exception e) { 
      JOptionPane.showMessageDialog(null, e); 
     } 
    } 
}