I read the excel file using the Apache POI library. The contents of the file are displayed on the console. Is it possible to display the contents not on the console, but in a separate window (for example, using the javax.swing library)?

Very confused while trying to do something.

Thanks in advance for your help.

Part 1 - reading excel

public class Readsheet{ static XSSFRow row; public static void main(String[] args) throws Exception{ FileInputStream fis = new FileInputStream(new File("1_2016_September.xlsx")); XSSFWorkbook workbook = new XSSFWorkbook(fis); XSSFSheet spreadsheet = workbook.getSheetAt(0); Iterator < Row > rowIterator = spreadsheet.iterator(); while (rowIterator.hasNext()){ row = (XSSFRow) rowIterator.next(); Iterator < Cell > cellIterator = row.cellIterator(); while (cellIterator.hasNext()){ Cell cell = cellIterator.next(); switch (cell.getCellType()){ case Cell.CELL_TYPE_NUMERIC: System.out.print(cell.getNumericCellValue() + " \t" ); break; case Cell.CELL_TYPE_STRING: System.out.print(cell.getStringCellValue() + " \t" ); break; } } System.out.println(); CompletedWorks History = new CompletedWorks(); //? History.displayFieldHistory.setText(???); //? } fis.close(); } } 

Part 2 is a window

 public class CompletedWorks { JPanel panelComplitedWorks; JPanel pTop; JPanel pCenter; JLabel thisDay; JCheckBox paidCheck; public JTextArea displayFieldHistory; //? public CompletedWorks (){ panelComplitedWorks= new JPanel(); BorderLayout panelCW = new BorderLayout(1,1); panelComplitedWorks.setLayout(panelCW); displayFieldHistory = new JTextArea(10, 10); displayFieldHistory.setLineWrap(true); displayFieldHistory.setWrapStyleWord(true); pCenter = new JPanel(); GridLayout glH =new GridLayout(1,1); pCenter.setLayout(glH); pCenter.add(displayFieldHistory); panelComplitedWorks.add("Center",pCenter); thisDay = new JLabel("Date"); paidCheck = new JCheckBox("salary"); pTop = new JPanel(); GridLayout gl2 =new GridLayout(1,2); pTop.setLayout(gl2); pTop.add(thisDay); pTop.add(paidCheck); panelComplitedWorks.add("North",pTop); JFrame frameHistory = new JFrame("A list of completed orders"); frameHistory.setContentPane(panelComplitedWorks); frameHistory.pack(); frameHistory.setLocation(500, 205); frameHistory.setVisible(true); } public static void main(String[] args) { CompletedWorks CW = new CompletedWorks(); } } 

    1 answer 1

    Since the setText method accepts a string, it is reasonable to generate a string for it.

     StringBuilder text = new StringBuilder(); while (rowIterator.hasNext()){ row = (XSSFRow) rowIterator.next(); Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: text.append(cell.getNumericCellValue() + "\t"); break; case Cell.CELL_TYPE_STRING: text.append(cell.getStringCellValue() + "\t"); break; } } text.append("\n"); } CompletedWorks History = new CompletedWorks(); History.displayFieldHistory.setText(text.toString()); 

    True, it is better to use JTable instead of JTextArea. And make the displayFieldHistory field private, and write data to it with a setter or pass it to the form constructor. Yes, and the file reading code can be transferred to the main method or some other form class.

    • I see. I'll try to do it all. Many things I do while intuitively and, I understand, far from the best way, if not with errors and jambs, like this time. I will sort this out. Exhaustive answer. You really helped, thank you very much! - Yaraslau Khamenka