使用 Java 和 Apache POI 使用地图的结果填充 xlsx 文件的空单元格
Fill xlsx file empty cell with results from a map using Java and Apache POI
我基本上有 2 个 xlsx 文件,一个有图像 url 和一个产品 ID,一个有空图像 url 和一个产品 ID。
我已经在另一个文件中正确加载了我需要解析的内容,地图的参数包含了所有需要的信息。
所以我的问题是,如何将映射结果写入新文件?我基本上需要将图像 url 放在第一列的索引上,其中包含所有产品的图像 urls。
这是一些示例代码,
private static void writeFile(Map<String, String> products) throws IOException {
File file = new File("./data/stock_zonder_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)) {
if(cell.getColumnIndex() == 2) {
String val = cell.getStringCellValue();
if(products.containsKey(val)) {
String image_url = products.get(val);
//here I need to write to the file the image url on column 0 of the specified $file with the $image_url
}
}
}
}
}
}
根据问题,您的要求是更新现有的 excel 文件。
尝试以下步骤。
//create a row id and initialized
int rowId = 0;
while (iterator.hasNext()) {
---- //another code
rowId++;
if(products.containsKey(val)) {
String image_url = products.get(val);
//assumed as first row is header
Cell cell = datatypeSheet.getRow(rowId).getCell(0); //get the cell from relevant row (rowId) and first column (column index is a 0)
cell.setCellValue(image_url); //set image_url into cell
}
---- //another code
}
我基本上有 2 个 xlsx 文件,一个有图像 url 和一个产品 ID,一个有空图像 url 和一个产品 ID。
我已经在另一个文件中正确加载了我需要解析的内容,地图的参数包含了所有需要的信息。
所以我的问题是,如何将映射结果写入新文件?我基本上需要将图像 url 放在第一列的索引上,其中包含所有产品的图像 urls。
这是一些示例代码,
private static void writeFile(Map<String, String> products) throws IOException {
File file = new File("./data/stock_zonder_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)) {
if(cell.getColumnIndex() == 2) {
String val = cell.getStringCellValue();
if(products.containsKey(val)) {
String image_url = products.get(val);
//here I need to write to the file the image url on column 0 of the specified $file with the $image_url
}
}
}
}
}
}
根据问题,您的要求是更新现有的 excel 文件。
尝试以下步骤。
//create a row id and initialized
int rowId = 0;
while (iterator.hasNext()) {
---- //another code
rowId++;
if(products.containsKey(val)) {
String image_url = products.get(val);
//assumed as first row is header
Cell cell = datatypeSheet.getRow(rowId).getCell(0); //get the cell from relevant row (rowId) and first column (column index is a 0)
cell.setCellValue(image_url); //set image_url into cell
}
---- //another code
}