2

i am trying to read an excel file using java. while compiling the program i am getting error as illegal character. help me to fix this problem. here is the code

import java.io.IOException;
import java.io.*;
import java.util.Iterator;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;



public class Readingexcel {

public static void main(String[] args) {
    try {
         System.out.println("before reading");
        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("input.xlsx"));
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        HSSFSheet sheet = wb.getSheet("first");

        HSSFRow row;
        HSSFCell cell;
        String s;

        int rows; // No of rows
        rows = sheet.getPhysicalNumberOfRows();
         System.out.println(rows);
         System.out.println(sheet.getRow(1).getPhysicalNumberOfCells());
        int cols = 0; // No of columns
        int tmp = 0;

// This trick ensures that we get the data properly even if it doesn’t start from first few rows
        for (int i = 0; i < 10 || i < rows; i++) {
            row = sheet.getRow(i);

            if (row != null) {
                tmp = sheet.getRow(i).getPhysicalNumberOfCells();


                if (tmp > cols) {
                    cols = tmp;

                }
            }
        }

        for (int r = 0; r < rows; r++) {
            row = sheet.getRow(r);
            if (row != null) {
                for (int c = 0; c < cols; c++) {
                    cell = row.getCell((short) c);
                    if (cell != null) {
// Your code here
// s = cell.getData();
                       System.out.println(cell.getStringCellValue());


                    }
                }
            }
        }
    } catch (Exception ioe) {
        ioe.printStackTrace();
    }

}
}

while compiling the program am getting error like

Microsoft Windows XP [Version 5.1.2600]
 (C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\vino>cd C:\Program Files\jdk1.6.0_10\bin

C:\Program Files\jdk1.6.0_10\bin>javac Readingexcel.java
Readingexcel.java:1: illegal character: \187
import java.io.IOException;
^
Readingexcel.java:1: illegal character: \191
import java.io.IOException;
^
2 errors

can anyone help me to fix this problem.

Nishant
  • 54,584
  • 13
  • 112
  • 127
darsha
  • 61
  • 1
  • 9
  • There are some invisible characters in the beginning of your file. If you can't be bothered to fix them, just copy the source into a new file and delete the old file. – kba Jan 30 '12 at 13:42
  • Looks like you've got `∩╗┐` in your source... What happens when you open your .java file in notepad? – beny23 Jan 30 '12 at 13:43
  • @L7ColWinters This is a compile-time error, this has nothing to do with input.xlsx. – kba Jan 30 '12 at 13:43
  • Did you cut and pasted the import? maybe there is a unprintable string before import. retype the import somewhere else and delete this line. – UmNyobe Jan 30 '12 at 13:46
  • @kristian i tried it but still am getting the error – darsha Jan 30 '12 at 13:48
  • @darsha Naturally, if you create a new file and do CTRL+A, then _everything_ gets copied, even the undesired character. Try to copy everything from the second line to the bottom, and rewrite the first line yourself in a new file. – kba Jan 30 '12 at 14:02

3 Answers3

5

I am pretty sure that this is BOMs (Byte Order Marks). Make sure you use the correct encoding. Maybe open the file in some other editor and re-save it in order to remove the BOMs. Nodepad ++, for instance, helps you.

Good luck, Max

Max
  • 1,000
  • 1
  • 11
  • 25
4

What program are you using to edit the source code? It looks like it's inserting a BOM, which javac dislikes. It should be possible to disable the BOM in the editor's settings.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
1

Looks like you have a BOM issue. Open your file in a hex editor and remove the first bytes that look strange and come before the first import line.

belgther
  • 2,544
  • 17
  • 15