I have wriiten a code to create jTable and export to Excel.I works.But when I open the Excel file the file is not fully recovered. It shows the following problem:
Repaired Part: /xl/worksheets/sheet2.xml part with XML error. Load error. Line 2, column 0. Repaired Part: /xl/worksheets/sheet7.xml part with XML error. Load error. Line 2, column 0. Removed Records: Named range from /xl/workbook.xml part (Workbook) Removed Records: Table from /xl/tables/table1.xml part (Table)
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.event.ActionEvent;
public class GUI {
private static JTextField textField;
private static JTextField textField_1;
public static void main(String args[]) throws IOException {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Object rowData[][] = null ;
Object columnNames[] = { "Column One", "Column Two"};
DefaultTableModel model = new DefaultTableModel(rowData,columnNames);
frame.getContentPane().setLayout(null);
JTable table = new JTable(model);
table.setModel(model);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(76, 106, 300, 200);
scrollPane.setVisible(true);
frame.getContentPane().add(scrollPane);
textField = new JTextField();
textField.setBounds(76, 21, 86, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(76, 61, 86, 20);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
JLabel lblNewLabel = new JLabel("Name");
lblNewLabel.setBounds(20, 24, 46, 14);
frame.getContentPane().add(lblNewLabel);
JLabel lblAge = new JLabel("Age");
lblAge.setBounds(20, 64, 46, 14);
frame.getContentPane().add(lblAge);
JButton btnNewButton = new JButton("Get Data");
btnNewButton.setBounds(235, 40, 89, 23);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String name = textField.getText().toString();
int age = Integer.parseInt(textField_1.getText());
model.addRow(new Object[] {name,age});
Object obj1 = GetData(table, 0,0 );
System.out.println("Cell value of 1 column and 1 row :" + obj1);
Object obj2 = GetData(table, 0,1 );
System.out.println("Cell value of 2 column and 1 row :" + obj2);
}
});
frame.getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("Excel");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
writeToExcel(table);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnNewButton_1.setBounds(340, 40, 89, 23);
frame.getContentPane().add(btnNewButton_1);
frame.setSize(455, 356);
frame.setVisible(true);
}
protected static void writeToExcel(JTable table) throws Exception {
try {
File file = new File("IET.xlsx");
FileInputStream inputStream = new FileInputStream(file);
XSSFWorkbook workbook = (XSSFWorkbook) WorkbookFactory.create(inputStream);
XSSFSheet sh = workbook.getSheet("UserInputs");
TableModel model = table.getModel();
String value1 = model.getValueAt(0, 0).toString();
String value2 = model.getValueAt(0, 1).toString();
workbook.getSheet("UserInputs").getRow(8).getCell(1).setCellValue(value2);
FileOutputStream fileOut = new FileOutputStream(file);
workbook.write(fileOut);
fileOut.close();
workbook.close();
System.out.println("File written successfully");
}
catch(Exception e){
System.out.print(e);}
}
private static Object GetData(JTable table, int x, int y) {
return table.getValueAt(x, y).toString();
}
}
Is there something wrong with the code?