//ReadExcel.java
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
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;
public class ReadExcel
{
public static String valueArray[][] = null;
public static void main(String args[]) throws Exception
{
FileInputStream excelSheetInput = new FileInputStream("sample.xls");
POIFSFileSystem myFileSystem = new POIFSFileSystem(excelSheetInput);
Workbook myWorkBook = new HSSFWorkbook(myFileSystem);
Sheet sheet = myWorkBook.getSheetAt(0);
int LastRow=sheet.getLastRowNum();
Row tempRow = sheet.getRow(0);
int LastCol=tempRow.getLastCellNum();
valueArray = new String[LastRow+1][LastCol];
System.out.println(LastRow);
System.out.println(LastCol);
for(int i=0;i<=LastRow;i++)
{
Row myRow = sheet.getRow(i);
for (int j=0;j<LastCol;j++)
{
Cell myCell = myRow.getCell(j);
int type = myCell.getCellType();
String value = "";
if(type == myCell.CELL_TYPE_STRING)
{
value = myCell.getStringCellValue();
}
else if(type == myCell.CELL_TYPE_NUMERIC)
{
value = new Double(myCell.getNumericCellValue()).toString();
}
valueArray[i][j] = value;
System.out.print(valueArray[i][j]+"\t");
} //CLOSING of J LOOP
System.out.println();
} // CLOSING of I loop
JFrame frame = new JFrame();
frame.setTitle("DrawRect");
frame.setSize(640, 480);
// private List<Rect> rectList = new ArrayList<Rect>();
String Name = valueArray[1][0];
DrawRectPanel rect =new DrawRectPanel(Name,240,320,280,350);
frame.add(rect);
// Rect r1 = new Rect(100, 100, 100, 100, Color.red);
// Rect r2 = new Rect(300, 100, 100, 100, Color.blue);
//r.addRect(r1);
// r.addRect(r2);
//frame.add(r);
String N = valueArray[1][3];
DrawRectPanel rect1 =new DrawRectPanel(N,10,10,15,15);
frame.add(rect1);
frame.setVisible(true);
/*for(int i=2;i<=2;i++)
{
String N = valueArray[i][3];
System.out.println(N);
DrawRectPanel rect1 =new DrawRectPanel(N,10,10,15,15);
frame.add(rect1);
} */
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}// CLOSING OF MAIN FUNCTION
/*class Rect {
int x;
int y;
int height;
int width;
Color color;
Rect(int x, int y, int width, int height, Color color)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = color;
}
public void draw(Graphics g)
{
g.setColor(color);
g.fillRect(x, y, width, height);
}
}*/
} //CLOSING OF CLASS
// DrawRectPanel.java
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class DrawRectPanel extends JPanel {
String mystring = "";
int x_cord,y_cord,str_x,str_y;
//private List<Rect> rectList = new ArrayList<Rect>();
//DrawRectPanel()
//{
//}
DrawRectPanel(String Name,int x ,int y,int a ,int b)
{
mystring=Name;
x_cord=x;
y_cord=y;
str_x=a;
str_y=b;
}
//public void addRect(Rect rect)
//{
//rectList.add(rect);
//}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.blue);
g.drawRect(x_cord,y_cord, 80, 30);
g.drawString(mystring, str_x,str_y);
//repaint();
}
}