gpt4 book ai didi

java - 使用 HSSFWorkbook 将 Jtable 导出到 Excelsheet

转载 作者:行者123 更新时间:2023-12-01 19:39:16 26 4
gpt4 key购买 nike

您好,我正在尝试使用 HSSFWorkbook 将 Jtable 数据导出到 Excel 工作表中。我得到了表中的所有内容,但没有得到表标题,请任何人提供同样的帮助。

这里是用于获取 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));
}
}

最后一个for循环没有添加表头数据。 enter image description here

我得到了这个 Excel 文件

enter image description here如何同时获取表头?

最佳答案

这是我根据本线程中的答案实现的 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();
}
}

关于java - 使用 HSSFWorkbook 将 Jtable 导出到 Excelsheet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21931113/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com