-
Notifications
You must be signed in to change notification settings - Fork 0
/
WriteToCSV.java
59 lines (52 loc) · 1.59 KB
/
WriteToCSV.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.TableModel;
public class WriteToCSV{
public static JFrame frame;
public static void getPath()
{
frame = new JFrame();
JFileChooser file_chooser = new JFileChooser();
file_chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int returnVal = file_chooser.showOpenDialog(file_chooser);
if(returnVal == JFileChooser.APPROVE_OPTION) {
String path = file_chooser.getSelectedFile().getPath();
exportToCSV(Main.table,path);
}
else
{
JOptionPane.showMessageDialog(null, "You have not selected a directory to save file");
}
}
public static void exportToCSV(JTable tableToExport,String pathToExportTo) {
try {
TableModel model = tableToExport.getModel();
FileWriter excel = new FileWriter(pathToExportTo);
for(int i = 0; i < model.getColumnCount(); i++)
{
excel.write(model.getColumnName(i) + ",");
}
excel.write("\n");
for(int i=0; i< model.getRowCount(); i++)
{
for(int j=0; j < model.getColumnCount(); j++)
{
String data = (String)model.getValueAt(i, j);
if(data == "null")
{
data="";
}
excel.write(data+",");
}
excel.write("\n");
}
excel.close();
JOptionPane.showMessageDialog(null, "File saved");
} catch (IOException ex)
{
JOptionPane.showMessageDialog(null, "File not saved");
}
}
}