简体   繁体   English

将Java表导出到Excel时出现问题

[英]Problems exporting java tables to excel

I'm having problems at exporting a Jtable from my program to Excel, I'm using this method, due the fact that I'm not allowed to use POI 在将Jtable从程序导出到Excel时遇到问题,由于无法使用POI,因此我正在使用此方法

public void exportTable(File file)throws IOException{
    FileWriter out = new FileWriter(file);
    BufferedWriter bw = new BufferedWriter(out);
    for(int i = 0; i < myTable.getColumnCount(); i++){
        bw.write(myTable.getColumnName(i) + "\t");
    }
    bw.write("\n");
    for(int i = 0; i < myTable.getRowCount(); i++){
        for(int j = 0; j < myTable.getColumnCount(); j++){
            bw.write(myTable.getValueAt(i, j).toString() +"\t");
        }
        bw.write("\n");
    }
    bw.close();
    JOptionPane.showMessageDialog(rootPane, "Your table have been exported to " + file);
}

And I'm using this method with this: 我正在使用这种方法:

try{
    Date date = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy_HH-mm-ss");
    String desktop = System.getProperty("user.home") + "/Desktop";
    String name = desktop + "/MyTable" + dateFormat.format(date)+ ".csv";
    exportTable(new File(name));
}catch(IOException e){
    e.getMessage();
}

Well, that's working good, but the problem is that every column in a row are being written in the first column, intead of being written in different columns, I thought the "\\t" would fix that problem, but no, any idea of how can I fix this? 很好,这很好,但是问题是一行中的每一列都写在第一列中,而不是写在不同的列中,我认为“ \\ t”可以解决该问题,但是没有,我怎样才能解决这个问题?

So, your code works okay for, so the problem is else where in your code, which you aren't sharing with us. 因此,您的代码可以正常工作,所以问题出在代码中的其他位置,您没有与我们共享。 Consider providing a runnable example which demonstrates your problem. 考虑提供一个可运行的示例来演示您的问题。 This is not a code dump, but an example of what you are doing which highlights the problem you are having. 这不是代码转储,而是您正在执行的操作的一个示例,突出显示了您所遇到的问题。 This will result in less confusion and better responses 这将减少混乱并改善响应

I would, however, suggest you have a look at The try-with-resources Statement and StringJoiner which will help make your life easier 但是,我建议您看一下try-with-resources语句StringJoiner ,它们将使您的生活更轻松

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringJoiner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        DefaultTableModel model = new DefaultTableModel(0, 10);
        for (int row = 0; row < 10; row++) {
            String[] cols = new String[10];
            for (int col = 0; col < cols.length; col++) {
                cols[col] = row + "x" + col;
            }
            model.addRow(cols);
        }

        try {
            exportTable(new JTable(model), new File("bananas.csv"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void exportTable(JTable myTable, File file) throws IOException {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
            StringJoiner sj = new StringJoiner("\t");
            for (int i = 0; i < myTable.getColumnCount(); i++) {
                sj.add(myTable.getColumnName(i));
            }
            bw.write(sj.toString());
            bw.newLine();
            for (int i = 0; i < myTable.getRowCount(); i++) {
                sj = new StringJoiner("\t");
                for (int j = 0; j < myTable.getColumnCount(); j++) {
                    sj.add(myTable.getValueAt(i, j).toString());
                }
                bw.write(sj.toString());
                bw.newLine();
            }
//          JOptionPane.showMessageDialog(rootPane, "Your table have been exported to " + file);
        }
    }

}

Which outputs 哪个输出

A   B   C   D   E   F   G   H   I   J
0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9
1x0 1x1 1x2 1x3 1x4 1x5 1x6 1x7 1x8 1x9
2x0 2x1 2x2 2x3 2x4 2x5 2x6 2x7 2x8 2x9
3x0 3x1 3x2 3x3 3x4 3x5 3x6 3x7 3x8 3x9
4x0 4x1 4x2 4x3 4x4 4x5 4x6 4x7 4x8 4x9
5x0 5x1 5x2 5x3 5x4 5x5 5x6 5x7 5x8 5x9
6x0 6x1 6x2 6x3 6x4 6x5 6x6 6x7 6x8 6x9
7x0 7x1 7x2 7x3 7x4 7x5 7x6 7x7 7x8 7x9
8x0 8x1 8x2 8x3 8x4 8x5 8x6 8x7 8x8 8x9
9x0 9x1 9x2 9x3 9x4 9x5 9x6 9x7 9x8 9x9

Updated 更新

I think I finally understand your conundrum, Excel use to prompt your about how a CSV was delineated, doesn't appear to do that any more (at least not for me) 我想我终于了解了您的难题,Excel用来提示您有关CSV的描述方式,似乎不再这样做了(至少对我而言不是)

So, replace StringJoiner sj = new StringJoiner("\\t"); 因此,替换StringJoiner sj = new StringJoiner("\\t"); and sj = new StringJoiner("\\t"); sj = new StringJoiner("\\t"); with StringJoiner sj = new StringJoiner(","); 使用StringJoiner sj = new StringJoiner(","); and sj = new StringJoiner(","); sj = new StringJoiner(","); respectivly respectivly

高强

If all values of a row are shown in Excel in the first column you can mark that first column and use Excels "text to columns" function. 如果一行的所有值都显示在Excel的第一列中,则可以标记该第一列,并使用Excel的“文本到列”功能。 Than you can specify the upcoming dialog to use the tab character as split character. 比您可以指定即将出现的对话框以将制表符用作分隔符。

If you export commas instead of tabs you can open it in Excel and rows should be splitted automatically, but in some Excel versions only when the file is opend from Explorer. 如果导出逗号而不是选项卡,则可以在Excel中打开它,并且行应自动拆分,但在某些Excel版本中,仅当从Explorer打开文件时才行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM