将从xlsx文件中加载内容的结果打印到地图中使用POI。

huangapple go评论70阅读模式
英文:

Printing out results in a map loading contents from a xlsx file using POI

问题


import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

public class Main {

    static final Map<String, String> PRODUCT_CODES_BY_IMAGES = new HashMap<>();

    public static void main(String[] args) throws IOException, InvalidFormatException {
        File file = new File("./data/stock_met_imageurls.xlsx");
        FileInputStream fis = new FileInputStream(file);
        Workbook workbook = new XSSFWorkbook(fis);
        Sheet datatypeSheet = workbook.getSheetAt(0);
        Iterator<Row> iterator = datatypeSheet.iterator();
        while (iterator.hasNext()) {
            Row currentRow = iterator.next();
            Iterator<Cell> cellIterator = currentRow.iterator();

            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();

                if (cell != null && cell.getCellTypeEnum().equals(CellType.STRING)) {
                    String image_url = null;
                    String product_id = null;

                    switch (cell.getColumnIndex()) {
                        case 0:
                            image_url = cell.getStringCellValue();
                            break;
                        case 2:
                            product_id = cell.getStringCellValue();
                            break;
                    }

                    if (image_url != null && product_id != null) {
                        PRODUCT_CODES_BY_IMAGES.put(product_id, image_url);
                    }

                }
            }
        }

        PRODUCT_CODES_BY_IMAGES.forEach((k, v) -> {
            System.out.println("key = " + k);
            System.out.println("val = " + v);
        });

        System.out.println("Size = " + PRODUCT_CODES_BY_IMAGES.size());
    }
}

如果我在下面的情况下这样做:

System.out.println("val = " + cell.getStringCellValue());

它会正确地打印出来,但是出于某种原因映射是空的?

非常感谢您的帮助。

英文:

so I need to print out the product id and image url from a excel file as I need to modify another file with the contents I retrieve, however for some reason the mappings is empty, but if I print the values out directly it shows them correctly.


import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

public class Main {

    static final Map&lt;String, String&gt; PRODUCT_CODES_BY_IMAGES = new HashMap&lt;&gt;();

    public static void main(String[] args) throws IOException, InvalidFormatException {
        File file = new File(&quot;./data/stock_met_imageurls.xlsx&quot;);
        FileInputStream fis = new FileInputStream(file);
        Workbook workbook = new XSSFWorkbook(fis);
        Sheet datatypeSheet = workbook.getSheetAt(0);
        Iterator&lt;Row&gt; iterator = datatypeSheet.iterator();
        while (iterator.hasNext()) {
            Row currentRow = iterator.next();
            Iterator&lt;Cell&gt; cellIterator = currentRow.iterator();

            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();

                if(cell != null &amp;&amp; cell.getCellTypeEnum().equals(CellType.STRING)) {
                    String image_url = null;
                    String product_id = null;

                    switch(cell.getColumnIndex()) {
                        case 0:
                            image_url = cell.getStringCellValue();
                            break;
                        case 2:
                            product_id = cell.getStringCellValue();
                            break;
                    }

                    if(image_url != null &amp;&amp; product_id != null) {
                        PRODUCT_CODES_BY_IMAGES.put(product_id, image_url);
                    }

                }


            }
        }

        PRODUCT_CODES_BY_IMAGES.forEach((k, v) -&gt; {
            System.out.println(&quot;key = &quot; + k);
            System.out.println(&quot;val = &quot; + v);
        });

        System.out.println(&quot;Size = &quot; + PRODUCT_CODES_BY_IMAGES.size());
    }
}

if I did under the cases

System.out.println(&quot;val = &quot; + cell.getStringCellValue());

it prints it out correctly but for some reason the mappings is empty?

Any help would be greatly appreciated.

答案1

得分: 1

因为你的地图被声明为静态的不可变的。查看 https://www.baeldung.com/java-final

英文:

Because your map is declared as static final. Check out https://www.baeldung.com/java-final

答案2

得分: 0

已修正,在上述映射中我将元素添加到了错误的括号上方。

英文:

Fixed, I added the elements in the mappings above the wrong bracket.

huangapple
  • 本文由 发表于 2020年8月29日 19:59:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63646707.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定