Tuesday, March 15, 2016

Excel File Reader

This short program serves as an Excel reader. Using the Apache POI API, it fetches data from an .xls file and displays it on its panel.



  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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileInputStream;
import javax.swing.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelReader extends JFrame{
    private JTextField tftitle=null;
    private String filename=null;
    private JLabel ldata=null;
    
    public ExcelReader(){
        super("Excel Reader");
        setSize(400,240);
        
        setLayout(new BorderLayout());
        
        JPanel pleft=new JPanel();
        pleft.setSize(200,240);
        pleft.setLayout(new FlowLayout());
        tftitle=new JTextField();
        tftitle.setColumns(10);
        pleft.add(tftitle);
        JButton bfile=new JButton("File");
        bfile.addActionListener(new ButtonHandler());
        pleft.add(bfile);
        add("West",pleft);
        
        JPanel pright=new JPanel();
        pright.setSize(200,240);
        ldata=new JLabel();
        pright.add(ldata);
        add("Center",pright);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    
    class ButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(e.getActionCommand()=="File"){
                FileDialog fd=new FileDialog(ExcelReader.this,"Open File",FileDialog.LOAD);
                fd.setDirectory("C:\\");
                fd.setVisible(true);
                filename=fd.getFile();
                
                try{                    
                    FileInputStream inputStream = new FileInputStream(new File(filename));
                    Workbook workbook = new HSSFWorkbook(inputStream);
                    Sheet sheet = workbook.getSheetAt(0);
                    
                    int numrow=sheet.getLastRowNum();
                    int numcol=sheet.getRow(1).getLastCellNum();
                    
                    String content="<html><div align=\"center\">"+tftitle.getText()+"</div><table border=\"1\">";
                    
                    content=content+"<tr>";
                    for(int i=0;i<numcol;i++){
                        content=content+"<td align=\"center\">"+sheet.getRow(0).getCell(i).getStringCellValue()+"</td>";
                    }
                    content=content+"</tr>";
                    
                    
                    for(int i=1;i<=numrow;i++){
                        content=content+"<tr>";
                        for(int j=0;j<numcol;j++){
                            Cell cell=sheet.getRow(i).getCell(j);
                            switch (cell.getCellType()) {
                            case Cell.CELL_TYPE_STRING:
                                content=content+"<td align=\"center\">"+sheet.getRow(i).getCell(j).getStringCellValue()+"</td>";
                                break;
                            case Cell.CELL_TYPE_BOOLEAN:
                                content=content+"<td align=\"center\">"+sheet.getRow(i).getCell(j).getBooleanCellValue()+"</td>";
                                break;
                            case Cell.CELL_TYPE_NUMERIC:
                                content=content+"<td align=\"center\">"+sheet.getRow(i).getCell(j).getNumericCellValue()+"</td>";
                                break;
                            }
                        }
                        content=content+"</tr>";
                    }

                    content=content+"</table></html>";
                    
                    ldata.setText(content);
                    
                    inputStream.close();
                }catch(Exception ex){
                    
                }
            }
        }
    }
    
    public static void main(String[] args){
        new ExcelReader();
    }
}

No comments:

Post a Comment