![](/img/trans.png)
[英]How to store text field value in to a variable for further use of it in java swing?
[英]Is there any way through which i can store array data in variable using java and use that variable further
我目前正在导入 Excel 工作表数据,将该数据按行存储在一个数组中。我的目标是将每个单元格数据存储在一个唯一定义的变量中,并进一步使用相同的变量进行一次迭代,同时将下一行的数据保存在相同的变量和重用。任何人都可以帮助我解决这个问题(请用一个例子来解释只是为了理解)这就是代码的方式
public static void main(String[] args)throws NullPointerException {
try {
File fXmlFile = new File("bin/testngscript/v1.xml");
//get the factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//Using factory get an instance of document builder
DocumentBuilder db = dbf.newDocumentBuilder();
//parse using builder to get DOM representation of the XML file
Document doc = db.parse(fXmlFile);
//read the xml node element
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("Excelpath");
String excelpath1 =null;
for (int index = 0; index < nodeList.getLength(); index++) {
System.out.println(nodeList.item(index).getTextContent());
excelpath1= nodeList.item(index).getTextContent();
System.out.println(excelpath1);
}
excelpath2= excelpath1;
System.out.println(excelpath2);
}
catch(ParserConfigurationException pce) {
pce.printStackTrace();
}catch(SAXException se) {
se.printStackTrace();
}catch(IOException ioe) {
ioe.printStackTrace();
}
int rownum = 0;
String Filepath = excelpath2;
FileInputStream file = new FileInputStream(Filepath);
@SuppressWarnings("resource")
HSSFWorkbook hw = new HSSFWorkbook(file);
HSSFSheet sheet = hw.getSheetAt(0);
rownum= sheet.getLastRowNum();
Object[][] product_data=new Object[rownum][14];
try {
System.out.println("excelpath is"+excelpath2);
for(int i=1;i<rownum;i++){
for(int j=0;j<14;){
product_data[i][j]="productname";
product_data[i][j++]="url";
product_data[i][j++]="themename";
product_data[i][j++]="papertype";
product_data[i][j++]="paperfinish";
product_data[i][j++]="packaging";
product_data[i][j++]="noofpages";
product_data[i][j++]="expectedprice";
product_data[i][j++]="priceinkart";
product_data[i][j++]="shippingcharges";
product_data[i][j++]="taxexpected";
product_data[i][j++]="taxactual";
product_data[i][j++]="total";
product_data[i][j++]="totalexpected";
System.out.println(product_data[i][j++]);
j=0;
}
}
}
catch(Exception e){
e.printStackTrace();
}
这可能是一个间接的答案,使用 java 反射从 xls 获取数据更容易做到。 以 testng 或 selenium 为例,它们以相同的方式做几件事
示例可以使用 Excel 文件中声明的数据填充 Java 对象的库http://persistentdesigns.com/wp/2010/02/excel-read-writable-reports-through-generics-reflection-and-apache-poi/
在 github 中仍然是很好的例子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.