您好我正在尝试使用HSSFWorkbook将Jtable数据导出到Excel表格中。我得到的所有内容是Table有的,但我没有得到表头,请任何人都可以帮助。
此处用于获取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数据。
我正在获取这个excel文件
如何获得表头和那个??
答案 0 :(得分:3)
这是我在本主题中的答案中对HSSF工作簿的实现。
我创建了一个类ExcelWriter
,然后创建了一个带有两个参数的Method Writer; <{1}}和JTable
将被使用。
FileLocation
答案 1 :(得分: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
答案 2 :(得分: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());
}
您可以先运行此操作,然后从第二行开始添加表数据。
答案 3 :(得分:0)
for(int j = 0; j&lt; tcm.getColumnCount(); j ++){
HSSFCell cell = fRow.createCell((short)j); cell.setCellValue(tcm.getColumn(j)的.getHeaderValue()的toString());
}
for(int j = 0; j&lt; tcm.getRowCount(); j ++){
HSSFCell cell = fRow.createCell((short)j); cell.setCellValue(tcm.getColumn(j)的.getHeaderValue()的toString());
}
答案 4 :(得分: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);
}
}
}