英文:
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<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());
}
}
if I did under the cases
System.out.println("val = " + 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论