Reputation: 111
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
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?
Upvotes: 0
Views: 1468
Reputation: 347244
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
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)
So, replace StringJoiner sj = new StringJoiner("\t");
and sj = new StringJoiner("\t");
with StringJoiner sj = new StringJoiner(",");
and sj = new StringJoiner(",");
respectivly
Upvotes: 3
Reputation: 956
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. 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.
Upvotes: 2