【问题标题】:Cannot get a text value from a error formula cell无法从错误公式单元格中获取文本值
【发布时间】:2018-01-30 06:11:06
【问题描述】:

你好,我遇到了一个错误

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

同时获取具有公式的单元格的值。我正在使用 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;
}

【问题讨论】:

  • 错误很明显,不是吗? cell 中的公式计算结果为错误。我建议使用DataFormatter 来获取单元格内容,如Getting the cell contents 所示。否则,您需要在获取内容之前始终检查CellType

标签: java excel-formula apache-poi


【解决方案1】:

在 C# 中使用 ExcelDataReader

string savepath =HttpContext.Current.Server.MapPath("~/Modules/Test/Excel/excel.xls");

var excelData = new ExcelDataReaderCaller(savepath);

公共类 ExcelDataReaderCaller { 字符串_路径; 公共 ExcelDataReaderCaller(字符串路径) { _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 数据 = 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 = 创建新列表

【讨论】:

  • 您能否说明为什么您认为C# 代码可能是有关Java 问题的答案?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-14
  • 1970-01-01
  • 1970-01-01
  • 2021-03-30
  • 2017-09-30
  • 2023-01-12
  • 1970-01-01
相关资源
最近更新 更多