无法从错误公式单元格中获取文本值

时间:2018-01-30 06:11:07

标签: java excel-formula apache-poi

我发现错误

  

无法从错误公式单元格中获取文本值

获取具有公式的单元格的值。我正在使用Apache poi 3.17

以下是我的代码: -

 public ArrayList<String> excelread(int sheetnum, int rownum, int lastcell) throws IOException{  // to read the expected values from the excel sheet 

            ArrayList<String>expected= new ArrayList <String>();

                File src= new File(filepath);               //Writing in excel 
                FileInputStream fis=new FileInputStream(src);
                XSSFWorkbook wb=new XSSFWorkbook(fis);
                XSSFSheet sh=wb.getSheetAt(sheetnum);
                XSSFRow row=sh.getRow(rownum);
                XSSFFormulaEvaluator evaluator= wb.getCreationHelper().createFormulaEvaluator();

            for (int currentcell=1;currentcell<=lastcell;currentcell +=2){
                    XSSFCell cell= row.getCell(currentcell);
                    evaluator.evaluateFormulaCell(cell);
                    String expectedresult = sh.getRow(rownum).getCell(currentcell).getStringCellValue(); // getting the value from a particular row and cell 
                    expected.add(expectedresult);  
                }
             wb.close();
            return expected;
}

1 个答案:

答案 0 :(得分:-3)

在C#中使用ExcelDataReader

string savepath = HttpContext.Current.Server.MapPath(&#34;〜/ Modules / Test / Excel / excel.xls&#34;);

var excelData = new ExcelDataReaderCaller(savepath);

public class ExcelDataReaderCaller     {         string _path;         public ExcelDataReaderCaller(字符串路径)         {             _path = path;         }

    public IExcelDataReader getExcelReader()
    {
        FileStream stream = File.Open(_path, FileMode.Open, FileAccess.Read);
        IExcelDataReader reader = null;
        try
        {
            if (_path.EndsWith(".xls"))
            {
                reader = ExcelReaderFactory.CreateBinaryReader(stream);
            }
            if (_path.EndsWith(".xlsx"))
            {
                reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
            }
            return reader;
        }
        catch (Exception)
        {
            throw;
        }
    }

    public IEnumerable<DataRow> getData()
    {
        var reader = this.getExcelReader();
        var excelSheet = reader.AsDataSet(new ExcelDataSetConfiguration()
        {
            ConfigureDataTable = (_) => new ExcelDataTableConfiguration()
            {
                UseHeaderRow = true
            }
        });

        var sheetsName = from DataTable sheets in excelSheet.Tables select sheets.TableName;
        string sheetName = sheetsName.Take(1).FirstOrDefault().ToString();

        var workSheet = reader.AsDataSet().Tables[sheetName];

        if (workSheet.Rows.Count > 0)
        {
            var rows = from DataRow a in workSheet.Rows select a;

            reader.Close();
            return rows;
        }
        else
        {
            reader.Close();
            return null;
        }

    }
}

获得价值

var Data = excelData.getData();

        if (Data != null && Data.Any())
        {
            foreach (var row in Data)
            {
                var  objData = new DataInfo()
                {
                    FirstName = row[0].ToString(),
                    SurName = row[1].ToString()
                };

                ObjDataList.Add(objData);
            }
            Data= null;
            return ObjDataList;
        }

// ObjDataList =制作新列表