twitter
rss


   1:  /* 
   2:   * @author  Hariraj
   3:   * Calculate your love by entering your name and the people you have crush upon. 
   4:   * It predicts your relationship with them using FLAMES - Friends, Love, Affection, Marriage, Enemy, SweetHearts.
   5:   * This Love/FLAMES calculator is just a fun game to delight the young and old lovers in the world. 
   6:   * This is for fun, don't take anything serious. Enjoy the Game.
   7:   */
   8:  package com.ll.flames;
   9:   
  10:  import java.applet.Applet;
  11:  import java.awt.Button;
  12:  import java.awt.Color;
  13:  import java.awt.Font;
  14:  import java.awt.Label;
  15:  import java.awt.Rectangle;
  16:  import java.awt.TextField;
  17:  import java.util.ArrayList;
  18:  import java.util.List;
  19:   
  20:  public class FlamesApp extends Applet {
  21:   
  22:      private static final long serialVersionUID = 1L;
  23:      private Label name1Label = null;
  24:      private Label name2Label = null;
  25:      private TextField name1textField = null;
  26:      private TextField name2textField = null;
  27:      private Button calculatebutton = null;
  28:      private Label relationLabel = null;
  29:      private Label headinglabel = null;
  30:      private Label relnlabel = null;
  31:      private Label copyrtLabel = null;
  32:      private Label namesLabel = null;
  33:   
  34:      /**
  35:       * This is the default constructor
  36:       */
  37:      public FlamesApp() {
  38:          super();
  39:      }
  40:   
  41:      /**
  42:       * Calculate relationship.
  43:       * 
  44:       * @return the string
  45:       */
  46:      public String calculateRelationship() {
  47:          List<Character> letterList = new ArrayList<Character>();
  48:          String name1, name2;
  49:          name1 = name1textField.getText().trim().toUpperCase();
  50:          name2 = name2textField.getText().trim().toUpperCase();
  51:          for (int i = 0; i < name1.length(); i++) {
  52:              letterList.add(name1.charAt(i));
  53:          }
  54:          for (int i = 0; i < name2.length(); i++) {
  55:              if (!letterList.contains(name2.charAt(i))) {
  56:                   letterList.add(name2.charAt(i));
  58:              } 
  59:                 
  60:          }
  61:          int matchcnt = letterList.size();
  62:          List<Character> flamesArrayList = new ArrayList<Character>();
  63:          flamesArrayList.add('F');
  64:          flamesArrayList.add('L');
  65:          flamesArrayList.add('A');
  66:          flamesArrayList.add('M');
  67:          flamesArrayList.add('E');
  68:          flamesArrayList.add('S');
  69:          int i = 0;
  70:          int temp = 0;
  71:          if (matchcnt != 0) {
  72:              while (i < 5) {
  73:                  i++;
  74:                  temp = (temp + (matchcnt - 1) % flamesArrayList.size())
  75:                          % flamesArrayList.size();
  76:                  flamesArrayList.remove(temp);
  77:              }
  78:          }
  79:          String output[] = { "FRIENDS", "LOVE", "AFFECTION", "MARRIAGE",
  80:                  "ENEMY", "SWEETHEARTS" };
  81:          String tmpString = "FLAMES";
  82:          int index = tmpString.indexOf(flamesArrayList.get(0));
  83:          return output[index];
  84:      }
  85:   
  86:      /**
  87:       * This method initializes this
  88:       * 
  89:       * @return void
  90:       */
  91:      public void init() {
  92:          namesLabel = new Label();
  93:          namesLabel.setBounds(new Rectangle(5, 195, 357, 23));
  94:          namesLabel.setAlignment(Label.CENTER);
  95:          namesLabel.setForeground(Color.blue);
  96:          namesLabel.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 12));
  97:          namesLabel.setText("");
  98:          copyrtLabel = new Label();
  99:          copyrtLabel.setBounds(new Rectangle(276, 222, 102, 23));
 100:          copyrtLabel.setText("© Lioking Labs");
 101:          relnlabel = new Label();
 102:          relnlabel.setBounds(new Rectangle(89, 222, 181, 23));
 103:          relnlabel.setForeground(new Color(51, 150, 51));
 104:          relnlabel.setAlignment(Label.CENTER);
 105:          relnlabel.setText("");
 106:          headinglabel = new Label();
 107:          headinglabel.setBounds(new Rectangle(101, 5, 156, 23));
 108:          headinglabel.setForeground(Color.red);
 109:          headinglabel.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 12));
 110:          headinglabel.setText("FLAMES CALCULATOR");
 111:          relationLabel = new Label();
 112:          relationLabel.setBounds(new Rectangle(6, 169, 360, 22));
 113:          relationLabel.setForeground(Color.red);
 114:          relationLabel.setAlignment(Label.CENTER);
 115:          relationLabel.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 12));
 116:          relationLabel.setText("");
 117:          name2Label = new Label();
 118:          name2Label.setBounds(new Rectangle(16, 96, 113, 23));
 119:          name2Label.setText("Enter Name 2 :");
 120:          name1Label = new Label();
 121:          name1Label.setBounds(new Rectangle(17, 41, 112, 23));
 122:          name1Label.setText("Enter Name 1 :");
 123:          this.setLayout(null);
 124:          this.setSize(380, 255);
 125:   
 126:          this.setBackground(Color.white);
 127:          this.add(name1Label, null);
 128:          this.add(name2Label, null);
 129:          this.add(getName1textField(), null);
 130:          this.add(getName2textField(), null);
 131:          this.add(getCalculatebutton(), null);
 132:          this.add(relationLabel, null);
 133:          this.add(headinglabel, null);
 134:          this.add(relnlabel, null);
 135:          this.add(copyrtLabel, null);
 136:          this.add(namesLabel, null);
 137:      }
 138:   
 139:      /**
 140:       * This method initializes name1textField
 141:       * 
 142:       * @return java.awt.TextField
 143:       */
 144:      private TextField getName1textField() {
 145:          if (name1textField == null) {
 146:              name1textField = new TextField();
 147:              name1textField.setBounds(new Rectangle(149, 42, 159, 29));
 148:          }
 149:          return name1textField;
 150:      }
 151:   
 152:      /**
 153:       * This method initializes name2textField
 154:       * 
 155:       * @return java.awt.TextField
 156:       */
 157:      private TextField getName2textField() {
 158:          if (name2textField == null) {
 159:              name2textField = new TextField();
 160:              name2textField.setBounds(new Rectangle(147, 91, 160, 29));
 161:          }
 162:          return name2textField;
 163:      }
 164:   
 165:      /**
 166:       * This method initializes calculatebutton
 167:       * 
 168:       * @return java.awt.Button
 169:       */
 170:      private Button getCalculatebutton() {
 171:          if (calculatebutton == null) {
 172:              calculatebutton = new Button();
 173:              calculatebutton.setBounds(new Rectangle(105, 129, 149, 32));
 174:              calculatebutton.setLabel("Calculate Relationship");
 175:              calculatebutton
 176:                      .addMouseListener(new java.awt.event.MouseListener() {
 177:                          public void mouseClicked(java.awt.event.MouseEvent e) {
 178:                              String relation = calculateRelationship();
 179:                              relationLabel.setText("RelationShip between ");
 180:                              namesLabel.setText(name1textField.getText().trim()
 181:                                      + " and " + name2textField.getText().trim()
 182:                                      + " is ");
 183:                              relnlabel.setText(relation);
 184:                          }
 185:   
 186:                          public void mousePressed(java.awt.event.MouseEvent e) {
 187:                          }
 188:   
 189:                          public void mouseReleased(java.awt.event.MouseEvent e) {
 190:                          }
 191:   
 192:                          public void mouseEntered(java.awt.event.MouseEvent e) {
 193:                          }
 194:   
 195:                          public void mouseExited(java.awt.event.MouseEvent e) {
 196:                          }
 197:                      });
 198:          }
 199:          return calculatebutton;
 200:      }
 201:   
 202:  }

What is Apache POI?
        Apache POI (Poor Obfuscation Implementation) is a Java APIs for manipulating various file formats based upon the Office Open XML standards (OOXML) and Microsoft’s OLE 2 Compound Document format (OLE2). Apache POI is your java Excel solution which will let you to read and write MS Word, MS Excel and MS PowerPoint files using java.

        In this article let’s see how to read data from an uploaded file. Apache poi API has the following implementations for achieving this. Horrible Spreadsheet Format (HSSF) is the pure Java implementation of the Excel (97-2007) file format of POI.

HSSFWorkbook – This is the first object constructed used to write/read an excel file. This represents the entire Excel File.
HSSFSheet - This class is used to create new spreadsheet which is called by HSSFWorkbook. This represents a single sheet in an Excel Document.
HSSFRow - This class represents the rows of a spreadsheet.
HSSFCell - This class represents the cell in a row of a spreadsheet.

Structure of Excel Document to be uploaded and read :





Spring 3 controller Code Snippet to Read Data from the uploaded document :

@RequestMapping(value = "/processExcel", method = RequestMethod.POST)
    public String processExcel(
            @RequestParam("excelfile") MultipartFile excelfile) {
        try {
            int i = 0;
            //Creates a workbook object from the uploaded excelfile
            HSSFWorkbook workbook = new HSSFWorkbook(excelfile.getInputStream());
            //Creates a worksheet object representing the first sheet
            HSSFSheet worksheet = workbook.getSheetAt(0);
            //Reads the data in excel file until last row is encountered
            while (i < worksheet.getLastRowNum()) {
                //Creates an object for the Candidate  Model
                Candidate candidate=new Candidate();
                //Creates an object representing a single row in excel
                HSSFRow row = worksheet.getRow(i++);
                //Sets the Read data to the model class
                candidate.setCandidateId((int)row.getCell(0).getNumericCellValue());
                candidate.setName(row.getCell(1).getStringCellValue());
                candidate.setAddress(row.getCell(2).getStringCellValue());
                candidate.setEmailId(row.getCell(3).getStringCellValue());
                candidate.setPinCode((int)row.getCell(4).getNumericCellValue());
                candidate.setAboutCandidate(row.getCell(5).getStringCellValue());
                //Sends the model object to service layer for validation,
                //data processing and then to persist
                iCandidateService.saveCandidate(candidate);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "uploadSuccess";
    }
If you are using maven, add this dependency to pom.xml


<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.7</version>
</dependency>

That’s all folks. If you have any doubts please feel free to contact me and provide your suggestions through comments.




   1:  /**
   2:   *
   3:   * @author Hariraj
   4:   */
   5:  import java.applet.Applet;
   6:  import java.awt.*;
   7:  import java.awt.event.*;
   8:   
   9:  public class magic2 extends Applet implements ActionListener {
  10:   
  11:           Panel rules=new Panel(new GridLayout(5,1));
  12:           Panel magic=new Panel(new GridLayout(3,3));
  13:           Panel bottomp=new Panel(new GridLayout(2,1));
  14:           Panel nos =new Panel(new GridLayout(3,3));
  15:           TextField [] input = new TextField[10];
  16:           Label rule=new Label("What is a Magic Square?");
  17:           Label rule2=new Label( "A magic square of order n is an arrangement of n^2 numbers,usually distinct");
  18:           Label rule3=new Label("integers, in a square, such that the n numbers in all rows, all columns, ");
  19:           Label rule4=new Label("and both diagonals sum to the same constant.");
  20:           Label result=new Label();
  21:           Button reset=new Button("Reset");
  22:           Button[] no=new Button[9];
  23:   
  24:           public void init() {
  25:              this.setSize(440, 440);
  26:              this.setLayout(new BorderLayout());
  27:              this.add(rules,BorderLayout.NORTH);
  28:              this.add(magic,BorderLayout.CENTER);
  29:              bottomp.add(nos);
  30:              bottomp.add(reset);
  31:              reset.setBackground(Color.pink);
  32:              this.add(bottomp,BorderLayout.SOUTH);
  33:              rules.add(result);
  34:              rule.setBackground(Color.LIGHT_GRAY);
  35:              rules.add(rule);
  36:              rules.add(rule2);
  37:              rules.add(rule3);
  38:              rules.add(rule4);
  39:              result.setBackground(Color.yellow);
  40:              result.setText("WELCOME!!! Click the numbers to begin");
  41:              for(int i=0;i<9;i++)
  42:              {
  43:                 magic.add(no[i]=new Button());
  44:                 no[i].setLabel(""+(i+1));
  45:                 no[i].setBackground(Color.lightGray);
  46:                 nos.add(no[i]);
  47:                 no[i].addActionListener(this);
  48:                 magic.add(input[i]=new TextField("",1));
  49:                 input[i].setEditable(false);           
  50:               }reset.addActionListener(this);
  51:      }
  52:           int g=0;
  53:   
  54:           public void actionPerformed(ActionEvent e) {
  55:            
  56:               if(e.getSource().equals(reset)){
  57:                  for(int i=0;i<9;i++)
  58:                  {
  59:                      input[i].setText("");
  60:                      input[i].setEditable(false);
  61:                      no[i].setEnabled(true);
  62:                  }
  63:                  g=0;
  64:                  result.setBackground(Color.WHITE);
  65:                  result.setText("");
  66:              }
  67:              if(e.getSource().equals(no[0]))
  68:             {
  69:                  input[g].setText("1");
  70:                  no[0].setEnabled(false);
  71:                  g++;
  72:              }
  73:   
  74:             if(e.getSource().equals(no[1]))
  75:             {
  76:                  input[g++].setText("2");
  77:                   no[1].setEnabled(false);
  78:   
  79:              }
  80:                  if(e.getSource().equals(no[2]))
  81:             {
  82:                  input[g++].setText("3");
  83:                   no[2].setEnabled(false);
  84:              }
  85:                  if(e.getSource().equals(no[3]))
  86:             {
  87:                  input[g++].setText("4");
  88:                   no[3].setEnabled(false);
  89:              }
  90:                  if(e.getSource().equals(no[4]))
  91:             {
  92:                  input[g++].setText("5");
  93:                   no[4].setEnabled(false);
  94:              }
  95:                  if(e.getSource().equals(no[5]))
  96:             {
  97:                  input[g++].setText("6");
  98:                   no[5].setEnabled(false);
  99:              }
 100:                  if(e.getSource().equals(no[6]))
 101:             {
 102:                  input[g++].setText("7");
 103:                   no[6].setEnabled(false);
 104:              }
 105:                  if(e.getSource().equals(no[7]))
 106:             {
 107:                  input[g++].setText("8");
 108:                   no[7].setEnabled(false);
 109:              }
 110:              if(e.getSource().equals(no[8]))
 111:             {
 112:                  input[g++].setText("9");
 113:                   no[8].setEnabled(false);
 114:              }
 115:                if(g<9){
 116:                 result.setBackground(Color.yellow);
 117:                 result.setText("Click the remaining " +(9-g)+ " enabled button to see the result");
 118:             }
 119:             if(g==9){
 120:                  int r1=Integer.parseInt(input[0].getText())+Integer.parseInt(input[1].getText())+Integer.parseInt(input[2].getText());
 121:                  int r2=Integer.parseInt(input[3].getText())+Integer.parseInt(input[4].getText())+Integer.parseInt(input[5].getText());
 122:                  int r3=Integer.parseInt(input[6].getText())+Integer.parseInt(input[7].getText())+Integer.parseInt(input[8].getText());
 123:                  int d1=Integer.parseInt(input[0].getText())+Integer.parseInt(input[4].getText())+Integer.parseInt(input[8].getText());
 124:                  int d2=Integer.parseInt(input[2].getText())+Integer.parseInt(input[4].getText())+Integer.parseInt(input[6].getText());
 125:   
 126:      if((r1==r2)&&(r1==r3)&&(r1==d1)&&(r1==d2))
 127:      {
 128:                      result.setBackground(Color.GREEN);
 129:                      result.setText("                          CONGRATS!!! IT'S A MAGIC SQUARE");
 130:   
 131:      }else {         result.setBackground(Color.red);
 132:                      result.setText("            NOT A MAGIC SQUARE!!! Try Again :-(");
 133:   
 134:                 }
 135:             }
 136:   }
 137:  }