Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在selenium中的单个工作簿中创建多个工作表并提取每个工作表的数据_Java_Apache Poi - Fatal编程技术网

Java 如何在selenium中的单个工作簿中创建多个工作表并提取每个工作表的数据

Java 如何在selenium中的单个工作簿中创建多个工作表并提取每个工作表的数据,java,apache-poi,Java,Apache Poi,我想在一个工作簿中创建多个工作表。如何在selenium中编写代码 public static void main(String[] args) { List<String[]> value=null; Set<String> uniquename = new HashSet<String>(); List<String[]> data1 = new ArrayList<String[]&g

我想在一个工作簿中创建多个工作表。如何在selenium中编写代码

public static void main(String[] args) {

        List<String[]> value=null;
        Set<String> uniquename = new HashSet<String>();
        List<String[]> data1 = new  ArrayList<String[]>();
        data1.add( new String[] {"Apple iPhone 8 Plus (Space Grey, 64 GB)","56000"});
        data1.add(new String[] {"Apple iPhone 6 (Grey, 128 GB)","47000"});
        data1.add(new String[] {"Apple iPhone XS (Space Grey, 512 GB)","28000"});
        data1.add(new String[] {"Apple iPhone XS (Space Grey, 512 GB)","29000"});
        data1.add(new String[] {"Apple iPhone 7 Plus (Gold, 128 GB)","19000"});
        data1.add(new String[] {"Apple iPhone 7 Plus (Gold, 128 GB)","18000"});

        Map<String,List<String[]> > hashmap = new LinkedHashMap<String,List<String[]> >();

        for(String[] data:data1) {
public static  void write(String name, String pname, String pprice) {

         XSSFSheet sheet = workbook1.createSheet(name);
         Row row = sheet.createRow(1);
         Cell cell1 = row.createCell(0);
         Cell cell2 = row.createCell(1);

         cell1.setCellValue(pname);
         cell2.setCellValue(pprice);

    }


    public static void save(XSSFWorkbook workbook)
    {
        try
        {

            FileOutputStream out = new FileOutputStream("iphonedata_demo.xlsx");
            workbook.write(out);

            out.close();
            System.out.println("iphonedata_demo.xlsx written successfully on disk.");
        }catch (Exception e) {

        } 
    }


}
publicstaticvoidmain(字符串[]args){
列表值=空;
Set uniquename=new HashSet();
List data1=new ArrayList();
data1.add(新字符串[]{“Apple iPhone 8 Plus(灰色,64 GB)”,“56000”});
添加(新字符串[]{“苹果iPhone 6(灰色,128 GB)”,“47000”});
添加(新字符串[]{“Apple iPhone XS(空间灰色,512GB)”,“28000”});
添加(新字符串[]{“Apple iPhone XS(空间灰色,512GB)”,“29000”});
data1.add(新字符串[]{“Apple iPhone 7 Plus(黄金,128 GB)”,“19000”});
添加(新字符串[]{“苹果iPhone7Plus(黄金,128GB)”,“18000”});
Map hashmap=newlinkedhashmap();
for(字符串[]数据:data1){
公共静态无效写入(字符串名称、字符串pname、字符串pprice){
XSSFSheet sheet=workbook1.createSheet(名称);
Row Row=sheet.createRow(1);
Cell cell1=row.createCell(0);
Cell cell2=row.createCell(1);
cell1.setCellValue(pname);
cell2.设置CellValue(pprice);
}
公共静态无效保存(XSSF工作簿工作簿)
{
尝试
{
FileOutputStream out=新的FileOutputStream(“iphonedata_demo.xlsx”);
练习册。写(出);
out.close();
System.out.println(“iphonedata_demo.xlsx已成功写入磁盘”);
}捕获(例外e){
} 
}
}

您必须为数组列表中的每个唯一名称创建一张工作表…
以下代码将您提供的条目重新排列到
地图中
,以便将唯一的电话名作为键,将列表中的所有价格(唯一与否,您的决定)作为值。查看代码注释:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Main {

    public static void main(String[] args) {
        List<String[]> data1 = new ArrayList<String[]>();
        data1.add(new String[] { "Apple iPhone 8 Plus (Space Grey, 64 GB)", "56000" });
        data1.add(new String[] { "Apple iPhone 6 (Grey, 128 GB)", "47000" });
        data1.add(new String[] { "Apple iPhone XS (Space Grey, 512 GB)", "28000" });
        data1.add(new String[] { "Apple iPhone XS (Space Grey, 512 GB)", "29000" });
        data1.add(new String[] { "Apple iPhone 7 Plus (Gold, 128 GB)", "19000" });
        data1.add(new String[] { "Apple iPhone 7 Plus (Gold, 128 GB)", "18000" });

        Map<String, List<String>> pricesPerPhoneName = new HashMap<String, List<String>>();

        /*
         * rearrange the given data in order to have unique names as keys and then use
         * those for sheet creation
         */
        data1.forEach(arr -> {
            String phoneName = arr[0];
            String price = arr[1];
            // check if the key is present in the map
            if (pricesPerPhoneName.containsKey(phoneName)) {
                /*
                 * add another price to the list of prices for the current key, check if the
                 * price is already contained and do nothing if yes...
                 */
                List<String> prices = pricesPerPhoneName.get(phoneName);
                if (!prices.contains(price)) {
                    prices.add(price);
                }
            } else {
                // put key and a list with the (one and only so far) price
                List<String> prices = new ArrayList<String>();
                prices.add(price);
                pricesPerPhoneName.put(phoneName, prices);
            }
        });

        /*
         * Now, you have all the prices stored as values of each key, which is a unique
         * phone name. The remaining code handles the creation of elements of the workbook
         */

        XSSFWorkbook workbook = new XSSFWorkbook();
        // provide a full path to the workbook
        Path workbookPath = Paths.get("C:\\iphones.xlsx");  // CHANGE TO YOUR PATH

        // creat a sheet for each phone name and write the data into it
        pricesPerPhoneName.forEach((name, prices) -> {
            /*
             * ########################################################################
             * ### please note that names for sheets in excel have a limited length ###
             * ### that's why the common prefix gets replaced by nothing here       ###
             * ########################################################################
             */
            XSSFSheet sheet = workbook.createSheet(name.replace("Apple iPhone", ""));

            // write one row for each price
            for (int i = 0; i < prices.size(); i++) {
                XSSFRow row = sheet.createRow(i);
                // write the phone name in column A and the price in column B
                XSSFCell phoneNameCell = row.createCell(0);
                XSSFCell phonePriceCell = row.createCell(1);
                phoneNameCell.setCellValue(name);
                phonePriceCell.setCellValue(prices.get(i));
            }

            // make the columns fit their content
            sheet.autoSizeColumn(0);
            sheet.autoSizeColumn(1);
        });

        // write the workbook via FileOutputStream
        try (FileOutputStream fos = new FileOutputStream(workbookPath.toAbsolutePath().toString())) {
            // write the workbook using the FileOutputStream
            workbook.write(fos);
            // force the FileOutputStream to write everything until it is empty
            fos.flush();
            // close the FileOutputStream
            fos.close();
            // close the workbook.
            workbook.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
import java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.nio.file.Path;
导入java.nio.file.path;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入org.apache.poi.xssf.usermodel.XSSFCell;
导入org.apache.poi.xssf.usermodel.XSSFRow;
导入org.apache.poi.xssf.usermodel.xssfheet;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
公共班机{
公共静态void main(字符串[]args){
List data1=new ArrayList();
data1.add(新字符串[]{“Apple iPhone 8 Plus(灰色,64 GB)”,“56000”});
添加(新字符串[]{“苹果iPhone 6(灰色,128 GB)”,“47000”});
添加(新字符串[]{“Apple iPhone XS(空间灰色,512GB)”,“28000”});
添加(新字符串[]{“Apple iPhone XS(空间灰色,512GB)”,“29000”});
data1.add(新字符串[]{“Apple iPhone 7 Plus(黄金,128 GB)”,“19000”});
添加(新字符串[]{“苹果iPhone7Plus(黄金,128GB)”,“18000”});
Map pricesPerPhoneName=新HashMap();
/*
*重新排列给定的数据,使其具有唯一的名称作为键,然后使用
*用于创建图纸的
*/
数据1.forEach(arr->{
字符串phoneName=arr[0];
字符串价格=arr[1];
//检查地图上是否有钥匙
if(pricesPerPhoneName.containsKey(phoneName)){
/*
*将另一个价格添加到当前键的价格列表中,检查
*价格已包含,如果是,则不执行任何操作。。。
*/
标价=pricesPerPhoneName.get(phoneName);
如果(!prices.contains(price)){
价格。添加(价格);
}
}否则{
//把钥匙和一张有(目前唯一)价格的清单放进去
标价=新的ArrayList();
价格。添加(价格);
pricesPerPhoneName.put(电话名、价格);
}
});
/*
*现在,您已经将所有价格存储为每个键的值,这是唯一的
*电话名。剩余的代码处理工作簿元素的创建
*/
XSSFWorkbook工作簿=新XSSFWorkbook();
//提供工作簿的完整路径
Path workbookPath=Path.get(“C:\\iphones.xlsx”);//更改到您的路径
//为每个电话号码创建一张表格,并将数据写入其中
pricesPerPhoneName.forEach((名称、价格)->{
/*
* ########################################################################
*#####请注意,excel中工作表的名称长度有限###
*##########这就是为什么公共前缀在这里被零替换的原因###
* ########################################################################
*/
XSSFSheet sheet=workbook.createSheet(name.replace(“苹果iPhone”);
//每种价格写一行
对于(int i=0;i