将java表导出为ex​​cel的问题

我在从我的程序导出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); } 

我用这个方法:

 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(); } 

那么,这是行之有效的,但问题是,连续的每一列都写在第一列,intead写在不同的列,我以为“\ t”将解决这个问题,但没有任何想法我怎样才能解决这个问题?

所以,你的代码工作正常,所以这个问题是在你的代码中的哪个地方,你没有与我们分享。 考虑提供一个可运行的例子来说明你的问题。 这不是代码转储,而是您正在做的事情的一个例子,它突出显示了您遇到的问题。 这将导致更less的困惑和更好的反应

然而,我会build议你看一下“试用资源声明”和“ 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); } } } 

哪个输出

 ABCDEFGHIJ 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 

更新

我想我终于明白了你的难题,Excel用来提示你如何描述一个CSV,似乎没有这样做(至less不是我)

所以,replaceStringJoiner sj = new StringJoiner("\t");sj = new StringJoiner("\t");StringJoiner sj = new StringJoiner(",");sj = new StringJoiner(","); respectivly

高强

如果一行中的所有值都显示在第一列的Excel中,则可以标记第一列并使用Excels“text to columns”函数。 比你可以指定即将到来的对话框使用制表符作为分割字符。

如果导出逗号而不是制表符,则可以在Excel中打开它,行应该自动分割,但在某些Excel版本中,只有在文件从资源pipe理器中打开的情况下。

Interesting Posts