检测和值的字符串替换变量 [英] detecting and replacing variables in a string by values

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

问题描述

我有一个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将评估。一个细胞可能包含这样的事情。


  

什么是$ {rnd.nextInt(v1Max - v1Min + 1)+ v1Min}为$%?{rnd.nextInt(v2Max - v2Min + 1)+ v2Min}


接下来,创建豆要提供给JETT。这些bean是命名对象是提供给JEXL前pressions在您的S preadsheet模板。

 地图<弦乐,对象>豆类=新的HashMap<弦乐,对象>();
beans.put(v1Min,15);
beans.put(v1Max,58);
beans.put(v2Min,30);
beans.put(v2Max,100);
beans.put(RND,新的随机());

接下来,创建code调用该JETT ExcelTransformer

 尝试
{
   ExcelTransformer变压器=新ExcelTransformer();
   //模板文件名,目标文件名,豆
   transformer.transform(Content.xls,Po​​pulated.xls豆);
}
赶上(IOException异常E)
{
   通信System.err.println(IOException的捕获:+ e.getMessage());
}
赶上(InvalidFormatException E)
{
   通信System.err.println(InvalidFormatException捕获:+ e.getMessage());
}

在生成的小号preadsheet,你会看到评估前pressions。在包含上述前pressions小区,你会看到,例如:


  

什么是38 41%?


(或者你会看到不同的号码,这取决于产生的随机数)

I have an excel sheet whose first column contains following data "What is ${v1} % of ${v2}?", two more columns (v1 and v2) in this sheet contains {"type":"int", "minimum":15, "maximum":58} and {"type":"int", "minimum":30, "maximum":100}, these are the ranges of variable v1 and v2. I need to replace v1 and v2 in the expression with a random value from the given range and store the expression in another spread sheet using JAVA. How can I do this by making use of JETT?

For example: I should store "What is 25% of 50?"

This is what I have done,I am able to read the column in my java program but not replace the values

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

public class ACGS {

public static void main(String[] args) throws Exception {  
//test file is located in your project path         
FileInputStream fileIn = new FileInputStream("C://users/user/Desktop/Content.xls");
//read file 
POIFSFileSystem fs = new POIFSFileSystem(fileIn);  
HSSFWorkbook filename = new HSSFWorkbook(fs);
//open sheet 0 which is first sheet of your worksheet
HSSFSheet sheet = filename.getSheetAt(0);

//we will search for column index containing string "Your Column Name" in the row 0 (which is first row of a worksheet
String columnWanted = "${v1}";
Integer columnNo = null;
//output all not null values to the list
List<Cell> cells = new ArrayList<Cell>();
Row firstRow = sheet.getRow(0);

for(Cell cell:firstRow){
if (cell.getStringCellValue().contains(columnWanted)){
    columnNo = cell.getColumnIndex();
    System.out.println("cell contains "+cell.getStringCellValue());
    }
}

if (columnNo != null){
 for (Row row : sheet) {
Cell c = row.getCell(columnNo);
if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) {
  // Nothing in the cell in this row, skip it
} else {
  cells.add(c);
      }
}
}   else{
System.out.println("could not find column " + columnWanted + " in first row of " + fileIn.toString());
     }
   }
}

解决方案

First, it looks like you aren't using JETT at all. You appear to be attempting to read the spreadsheet yourself and do some processing.

Here is how you would do this in JETT. JETT doesn't provide its own random number support, but together with its Apache Commons JEXL expression support, and Java's own Random, you can publish the expected ranges of your random variables as beans to JETT, and you can calculate a random variable with an expression.

First, create your template spreadsheet, populating it with expressions (between ${ and }) that JETT will evaluate. One cell might contain something like this.

What is ${rnd.nextInt(v1Max - v1Min + 1) + v1Min}% of ${rnd.nextInt(v2Max - v2Min + 1) + v2Min}?

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:

What is 41% of 38?

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆