本文介绍了检测和值的字符串替换变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Excel工作表的第一列包含以下数据什么是$ {V2}的$ {V1}%?,两列(v1和v2)本表包含{类型:INT ,最低:15,最大:58}和{类型:INT,最小值:30,最大:100},这些都是可变v1和v2的范围。我需要从给定范围内的随机值来代替v1和v2在EX pression并用JAVA存储前pression的是另一个s $ P $垫纸。我怎样才能通过JETT的使用做到这一点?

例如:我应该存储?什么是50 25%

这是我做了什么,我能够读取该列在我的java程序,但不能代替值

 进口java.io.FileInputStream中;
进口的java.util.ArrayList;
进口的java.util.List;
进口org.apache.poi.hssf.usermodel.HSSFSheet;
进口org.apache.poi.hssf.usermodel.HSSFWorkbook;
进口org.apache.poi.poifs.filesystem.POIFSFileSystem;
进口org.apache.poi.ss.usermodel.Cell;
进口org.apache.poi.ss.usermodel.Row;公共类ACGS {公共静态无效的主要(字串[] args)抛出异常{
//测试文件位于项目路径
的FileInputStream FILEIN =新的FileInputStream(C://users/user/Desktop/Content.xls);
//读取文件
POIFSFileSystem FS =新POIFSFileSystem(FILEIN);
HSSFWorkbook名=新HSSFWorkbook(FS);
//打开表0这是您的工作表的第一页
HSSFSheet片= filename.getSheetAt(0);//我们将搜索包含字符串你的列名的行0中(这是一个工作表的第一行的列索引
字符串columnWanted =$ {V1};
整数columnNo = NULL;
//输出的所有NOT NULL值列表
清单<电池>细胞=新的ArrayList<电池>();
行FIRSTROW = sheet.getRow(0);(电池单元:FIRSTROW){
如果(cell.getStringCellValue()。包括(columnWanted)){
    columnNo = cell.getColumnIndex();
    的System.out.println(单元格中包含+ cell.getStringCellValue());
    }
}如果(columnNo!= NULL){
 对于(鳞次栉比:表){
小区C = row.getCell(columnNo);
如果(C == NULL || c.getCellType()== Cell.CELL_TYPE_BLANK){
  //在此行中的单元格没有,跳过它
}其他{
  cells.add(C);
      }
}
}其他{
的System.out.println(找不到列+ columnWanted +,在第一行+ fileIn.toString());
     }
   }
}


解决方案

首先,它看起来像您不使用JETT的。你似乎是试图给自己看在S preadsheet,并做一些处理。

下面是你将如何在JETT做到这一点。 JETT不提供其自己的随机数的支持,但连同其Apache的百科全书JEXL前pression支持,和Java本身的随机,你可以发布的预期范围您的随机变量豆JETT,你可以计算和前pression的随机变量。

首先,创建模板US preadsheet,与前pressions填充它( $ {} )的JETT将评估。一个细胞可能包含这样的事情。

Next, create beans to be supplied to JETT. These beans are the named objects that are available to JEXL expressions in your spreadsheet template.

Map<String, Object> beans = new HashMap<String, Object>();
beans.put("v1Min", 15);
beans.put("v1Max", 58);
beans.put("v2Min", 30);
beans.put("v2Max", 100);
beans.put("rnd", new Random());

Next, create your code that invokes the JETT ExcelTransformer.

try
{
   ExcelTransformer transformer = new ExcelTransformer();
   // template file name, destination file name, beans
   transformer.transform("Content.xls", "Populated.xls", beans);
}
catch (IOException e)
{
   System.err.println("IOException caught: " + e.getMessage());
}
catch (InvalidFormatException e)
{
   System.err.println("InvalidFormatException caught: " + e.getMessage());
}

In the resultant spreadsheet, you will see the expressions evaluated. In the cell that contained the expressions above, you will see for example:

(Or you will see different numbers, depending on the random numbers generated.)

这篇关于检测和值的字符串替换变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 14:59
查看更多