here's my code to display all the data from an excel data file:
package util;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.swing.*;
import java.io.IOException;
public class read {
public static void main(String[] args) {
excelRead();
}
public static void excelRead(){
try {
int i;
int j;
String path = "./data/data.xlsx";
XSSFWorkbook workbook = new XSSFWorkbook(path);
XSSFSheet sheet = workbook.getSheet("sheet1");
for (i = 0; i <=5;i++) {
for (j = 0; j<=2;j++) {
String data = sheet.getRow(i).getCell(j).getStringCellValue();
System.out.print(data + " " );
if(j == 2){
System.out.println(" ");
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e){
e.printStackTrace();
}
}
}
I have two problems here,
- it displays a
NullPointerException
after it displays all data - I don't know how to display the data to a
JTable
edit :
this is the code to display the excel file data on the Jtable but it gives me an ArrayOutofBounds exception :
package util;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
public class Jtable extends JPanel {
JTable table;
public Jtable() {
try {
String path = "./data/data.xlsx";
XSSFWorkbook workbook = new XSSFWorkbook(path);
XSSFSheet sheet = workbook.getSheet("sheet1");
String[] column = {"name", "age", "profession", "gender", "company"};
String[][] data = {
{ sheet.getRow(0).getCell(0).getStringCellValue(),
sheet.getRow(0).getCell(1).getStringCellValue(),
sheet.getRow(0).getCell(2).getStringCellValue()},
{sheet.getRow(1).getCell(0).getStringCellValue(),
sheet.getRow(1).getCell(1).getStringCellValue(),
sheet.getRow(1).getCell(2).getStringCellValue()},
{sheet.getRow(2).getCell(0).getStringCellValue(),
sheet.getRow(2).getCell(1).getStringCellValue(),
sheet.getRow(2).getCell(2).getStringCellValue()},
{sheet.getRow(3).getCell(0).getStringCellValue(),
sheet.getRow(3).getCell(1).getStringCellValue(),
sheet.getRow(3).getCell(2).getStringCellValue()},
{sheet.getRow(4).getCell(0).getStringCellValue(),
sheet.getRow(4).getCell(1).getStringCellValue(),
sheet.getRow(4).getCell(2).getStringCellValue()}
};
table = new JTable(data, column);
table.setPreferredScrollableViewportSize(new Dimension(450, 63));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
Jtable panel = new Jtable();
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
public static void excelRead() {
try {
int i;
int j;
String path = "./data/data.xlsx";
XSSFWorkbook workbook = new XSSFWorkbook(path);
XSSFSheet sheet = workbook.getSheet("sheet1");
for (i = 0; i <= 5; i++) {
for (j = 0; j <= 2; j++) {
String data = sheet.getRow(i).getCell(j).getStringCellValue();
System.out.print(data + " ");
if (j == 2) {
System.out.println(" ");
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
}catch (ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
}
}