public static void readExcel2007()
    {
        InputStream in = null;
        try
        {
            in = new FileInputStream("d:\\b.xlsx");
            // 创建work
            Workbook work = new XSSFWorkbook(in);//主要在这里报错
            // 得到第一个sheet
            Sheet sheet = work.getSheetAt(0);
            // 获取总行数
            int rowNum = sheet.getLastRowNum();
            
            Row row = null;
            // 循环每一行
            for (int i = 1; i <= rowNum; i++)
            {
                row = sheet.getRow(i);
                if (null == row)
                {
                    continue;
                }
                Cell cell = row.getCell(0);
                if (cell != null)
                {
                    System.out.println(cell.getStringCellValue());
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args)
    {
        readExcel2003();
    }异常信息:
java.lang.IllegalStateException: The hyperlink for cell A2 references relation rId1, but that didn't exist!
at org.apache.poi.xssf.usermodel.XSSFHyperlink.<init>(XSSFHyperlink.java:70)
at org.apache.poi.xssf.usermodel.XSSFSheet.initHyperlinks(XSSFSheet.java:250)
at org.apache.poi.xssf.usermodel.XSSFSheet.read(XSSFSheet.java:203)
at org.apache.poi.xssf.usermodel.XSSFSheet.onDocumentRead(XSSFSheet.java:175)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead(XSSFWorkbook.java:227)
at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:159)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:181)
at ImportExcel.ImportExcelDemo.readExcel2007(ImportExcelDemo.java:73)
at ImportExcel.ImportExcelDemo.main(ImportExcelDemo.java:103)Excel文件: 其中[email protected]是超链接(也不知道为什么,打上去自动就是超链接)Account     Name
[email protected]    张杰
jjke     gf

解决方案 »

  1.   

    单元格内容是HTML标记是不是都会出错?
      

  2.   

    我这里读取一个还有aa@aa的单元格没有任何问题
    你代码给出的是readExcel2007 但是 main里面运行的是readExcel2003();
    这是不是很奇怪呢?
      

  3.   

    System.out.println(cell.getStringCellValue());cell.getHyperlink()lz需要对每个cell的类型cell.getType()做check然后调用相应的方法,而不是一竿子打死都是String类型的。
      

  4.   

    知道原因了,你使用的WorkBook什么的都不是POI里面读取EXCEL的推荐对象,直接给你贴一个可以跑出来你数据的实例
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.sql.Timestamp;
    import java.util.Date;import org.apache.poi.xssf.usermodel.XSSFCell;
    import org.apache.poi.xssf.usermodel.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.InputStream;
    import java.io.FileInputStream;/**
     * Created by IntelliJ IDEA.
     * User: admin
     * Date: 2011-9-21
     * Time: 17:19:14
     * To change this template use File | Settings | File Templates.
     */
    public class ExcelPOITest {
        public static void readExcel2007()
    {
       String filepath = "d:\\b.xlsx";
    FileInputStream fis = null;
    try {
    fis = new FileInputStream(filepath);
    } catch (FileNotFoundException e1) {
    e1.printStackTrace();
    }
    XSSFWorkbook wb=null;
    try { wb = new XSSFWorkbook(fis); } catch (IOException e3) {
    e3.printStackTrace();
    }
    try {
    fis.close();
    } catch (IOException e1) {
    e1.printStackTrace();
    } XSSFSheet sheet = wb.getSheetAt(0);
    XSSFRow row = null;
    XSSFCell cell = null; String a1 = "";
    String a2 ="";
    int rowNum; rowNum = sheet.getLastRowNum();
    for (int i = 1; i <= rowNum; i++) {
    row = sheet.getRow(i);
    cell = row.getCell(0);
    a1 = cell.getStringCellValue();
    cell = row.getCell(1);
    a2 = cell.getStringCellValue();
    System.out.println(a1+"\t"+a2+"\t"); }
    }public static void main(String[] args)
    {
    readExcel2007();
    }
    }
      

  5.   

    代码贴错了! 实际是调用的 readExcel2007()
      

  6.   

    可是它在构造 XSSFWork对象的时候 就报错了,走不到那里
      

  7.   

    你用的WorkBook 等类有问题,解析2007以上版本应该用XSSF,你跑我的代码试过了没有两列的数据就可以,看看能解决问题么
      

  8.   

    谢谢!但是如果 单元格内容是 !#$%$#^%$#^%$&%^*&^*(*&(&*) 一些特殊字符,就一直报那个超链接错误。。
      

  9.   

    读取EXcel2003 特殊字符一点问题没有,就是2007 就有问题
      

  10.   

    对每个cell的类型cell.getType()做check然后调用相应的方法可不一定都是string的类型祝楼主好运
      

  11.   

    关键是 Workbook work = new XSSFWorkbook(in);这行代码就报错了,下面执行不到
      

  12.   


    你能不能参考我给你写的代码,以这个为基础改,这个对@ $$% 处理没有任何问题我已经说了N遍,Workbook 这一套不行, 要用XSSF对应代码是XSSFWorkbook work=new XSSFWorkbook(in)你直接Workbook work = new XSSFWorkbook(in);肯定报出来强制类型转换错误
      

  13.   


     public static void readExcelOf2007()
        {
            String filepath = "d:\\b.xlsx";
            FileInputStream fis = null;
            try
            {
                fis = new FileInputStream(filepath);
            }
            catch (FileNotFoundException e1)
            {
                e1.printStackTrace();
            }
            XSSFWorkbook wb = null;
            
            try
            {
                
                wb = new XSSFWorkbook(fis);
                
            }
            catch (IOException e3)
            {
                e3.printStackTrace();
            }
            try
            {
                fis.close();
            }
            catch (IOException e1)
            {
                e1.printStackTrace();
            }
            
            XSSFSheet sheet = wb.getSheetAt(0);
            XSSFRow row = null;
            XSSFCell cell = null;
            
            String a1 = "";
            String a2 = "";
            int rowNum;
            
            rowNum = sheet.getLastRowNum();
            for (int i = 1; i <= rowNum; i++)
            {
                row = sheet.getRow(i);
                if (row != null)
                {
                    
                    cell = row.getCell(0);
                    a1 = cell == null ? "" : cell.getStringCellValue();
                    
                    cell = row.getCell(1);
                    a2 = cell == null ? "" : cell.getStringCellValue();
                    
                    System.out.println(a1 + "\t" + a2 + "\t");
                }
            }
            
        }
        
        public static void main(String[] args)
        {
            readExcelOf2007();
        }
    异常信息:
    Exception in thread "main" java.lang.IllegalStateException: The hyperlink for cell A2 references relation rId1, but that didn't exist!
    at org.apache.poi.xssf.usermodel.XSSFHyperlink.<init>(XSSFHyperlink.java:70)
    at org.apache.poi.xssf.usermodel.XSSFSheet.initHyperlinks(XSSFSheet.java:250)
    at org.apache.poi.xssf.usermodel.XSSFSheet.read(XSSFSheet.java:203)
    at org.apache.poi.xssf.usermodel.XSSFSheet.onDocumentRead(XSSFSheet.java:175)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead(XSSFWorkbook.java:227)
    at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:159)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:181)
    at ImportExcel.ImportExcelDemo.readExcelOf2007(ImportExcelDemo.java:125)
    at ImportExcel.ImportExcelDemo.main(ImportExcelDemo.java:170)
      

  14.   

    我的07版本Excel

    Account Name User Type Region Representative Office Email Telephone IsResponsible Re
    !#$%$#^%$#^%$&%^*&^*(*&(&*) 姓名中包含特殊字符 Regional Software Service Director Northern Africa [email protected] 123 "备注含有中文、字母、特殊字符、空格、    换行
    !@#$%^&*()_+{}|:""<>?!@#¥%……&*()——+{}|:“《》?<script>alert();中意!</script><table></table>
    88<script type =""text/javascipt"" src =""/ci-master/d"">et=utf-8"" /><meta ht备注备注备注备注tp-equiv=""Content-Type"" content=""text/html; charset=utf-8"" /><meta ht备注备注备注备注tp-equiv=""Content-Type"" content=""text/html; charset=utf-8"" /><meta ht备注备注备注备注tp-equiv=""Content-Type"" content=""text/html; charset=utf-8"" />"
    jjke gfgfgf Regional Software Service Director Northern Africa [email protected] ggfgf gf

    jjkyetyuu gfgfgf Regional Software Service Director Northern Africa [email protected] ggfgf gf

    dffdedf 姓名中包含特殊字符 Regional Software Service Director Northern Africa [email protected] 123 "备注含有中文、字母、特殊字符、空格、    换行
    !@#$%^&*()_+{}|:""<>?!@#¥%……&*()——+{}|:“《》?<script>alert();中意!</script><table></table>
    88<script type =""text/javascipt"" src =""/ci-master/d"">et=utf-8"" /><meta ht备注备注备注备注tp-equiv=""Content-Type"" content=""text/html; charset=utf-8"" /><meta ht备注备注备注备注tp-equiv=""Content-Type"" content=""text/html; charset=utf-8"" /><meta ht备注备注备注备注tp-equiv=""Content-Type"" content=""text/html; charset=utf-8"" />"
    dffddfsss 姓名中包含特殊字符 Regional Software Service Director Northern Africa [email protected] 123 "备注含有中文、字母、特殊字符、空格、    换行
    !@#$%^&*()_+{}|:""<>?!@#¥%……&*()——+{}|:“《》?<script>alert();中意!</script><table></table>
    88<script type =""text/javascipt"" src =""/ci-master/d"">et=utf-8"" /><meta ht备注备注备注备注tp-equiv=""Content-Type"" content=""text/html; charset=utf-8"" /><meta ht备注备注备注备注tp-equiv=""Content-Type"" content=""text/html; charset=utf-8"" /><meta ht备注备注备注备注tp-equiv=""Content-Type"" content=""text/html; charset=utf-8"" />"
    002 1 Regional Software Service Director China [email protected]





















      

  15.   

    我怀疑是你做实验的Excel文件的问题,同样的代码我的一点问题都没有你重新建一个新的excel,然后加一个个人邮箱地址试试
      

  16.   

    我也肯定是 实验的Excel问题,你是试下,多个字段 都含有特殊字符。
      

  17.   

    试试这个Excel
    name                                  password
    !@$#@$#@%#$%^$#@@#%$@#%#$^$%&%^*&^(* !$@##$&%*^&(&*)(*&)(*_)*(_)(_%%#$@!$#@!$@#!
      

  18.   

    在Excel里面 $这个符号是有特殊含义的,加入你的单元格里面可能不小心$引用到一个单元格,而这个单元格本身又无法找到,就会报你这个问题我想你要是真正要读类似!@$#@$#@%#$%^$#@@#%$@#%#$^$%&%这种符号,确实就会有这个错误,我没什么更好的办法,就是会报错
      

  19.   

    urlCell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
    urlCell.setCellFormula("HYPERLINK(\"" + item.getUrl() + "\",\"" + name + "\")");
    公式中的单个字符串长度最大值为255 还有其他方法设置超链接的吗?
      

  20.   

    Validate address of hyperlinks in XSSF(poi-developers) 
    http://poi.apache.org/changes.htmllz试试POI BETA6不是$的问题,只是读取Workbook,与流有关