0

Invalid header signature; read 0x6D78206C6D74683C, expected 0xE11AB1A1E011CFD0 getting the above error when I am trying to read excel sheet. xls format.

when I try to open the sheet manually I get this error : "The file format and extension of '*******.xls' don't match. The file could be corrupted or unsafe.

String strCompleteFilepath=this.strFilePath + File.separator + 
this.strFileName;
    int strRowIndex=0;
    int strColIndex=0;
    String strCompVal="";
    try{
    InputStream file = new FileInputStream(strCompleteFilepath);
    HSSFWorkbook workbook = new HSSFWorkbook(file);       
    HSSFSheet sheet = workbook.getSheetAt(0);
    int strRowCount= sheet.getLastRowNum();
    if(strRowCount>0)
    {
    Iterator<Row> rowIterator = sheet.iterator();
    while (rowIterator.hasNext())
    {
        Row row = (Row)rowIterator.next();
        Iterator<Cell> cellIterator = row.cellIterator();
        while (cellIterator.hasNext())
        {
            Cell cell = (Cell)cellIterator.next();
            String strCellValue=cell.toString();

            if (strCellValue.contentEquals(strKeyIdentifier))
            {
                strRowIndex=cell.getRowIndex();
                System.out.println("The row number where 
                "+strKeyIdentifier+"is found:  "+strRowIndex);                  
            }
            if (strCellValue.contentEquals(strColName))
            {
                strColIndex=cell.getColumnIndex();
                System.out.println("The column where "+strColName+" is 
                found: "+strColIndex);
            }
        }       
    }
    HSSFCell CompValue=sheet.getRow(strRowIndex).getCell(strColIndex);
    switch (CompValue.getCellType())
    {
    case 0:
        if (DateUtil.isCellDateFormatted(CompValue)) 
        {
            strCompVal = CompValue.getDateCellValue().toString();                   
        } else
        {
            strCompVal = String.valueOf(CompValue.getNumericCellValue());                     
        }
        break;
    case 2: 
        strCompVal = String.valueOf(CompValue.getBooleanCellValue());                   
        break;
    case 1: 
        strCompVal = CompValue.getStringCellValue();                    
    }
    //strCompVal=CompValue.toString();
    file.close();
    FileOutputStream out = new FileOutputStream(new ` 
    File(strCompleteFilepath));
    workbook.write(out);
    out.close();
    }
    else
    {
        System.out.println("There is no data in the excel, the row count is : "+strRowCount);
    }
    }
    catch (Exception ex)
    {
        System.out.println(ex.getMessage());
    }
    return strCompVal;
VidyaSN
  • 11
  • 4
  • 2
    Seems like your "xls" file is not a native Excel file. How did you obtain it? It's pretty common for downloaded "excel" files to be just a HTML table sent with an Excel MIME type header. – Tim Williams Jun 27 '17 at 21:51
  • 3
    0x6D78206C6D74683C is the little endian value for " – kiwiwings Jun 27 '17 at 23:26
  • @TimWilliams Yes you are right I think . I am an automation QA analyst , this is a report I get running a concurrent program. My job is to validate the report with the back end tables. So when I get the report mailed to me ( for testing purpose) , I am seeing this format and unable to read data in it. Please suggest here. How do I parse this to any readable format. I have tried Apache TIKA , could not go any further with that. –  VidyaSN Jun 28 '17 at 00:52
  • 1
    You could open the file in excel then save in a native Excel format. Or use an HTML parsing library. – Tim Williams Jun 28 '17 at 01:07
  • @TimWilliams . The first thing in automation is to avoid manual intervention , So I will have to see how to achieve the flow. HTML parsing library ? did you imply JSoup or Gumbo parsers ? –  VidyaSN Jun 28 '17 at 01:13
  • I don't imply. I don't even Java, but I do Excel. – Tim Williams Jun 28 '17 at 01:18

0 Answers0