如何解决使用Java和Apache POI用地图中的结果填充xlsx文件空单元格
我基本上有2个xlsx文件,一个带有图像URL和一个产品ID,一个带有空图像URL和一个产品ID。
我已经将需要解析的内容正确地加载到另一个文件中,带有映射的参数包含所有所需的信息。
所以我的问题是,如何将映射结果写入新文件?我基本上需要将图片网址放在第一列的索引中,该索引包含所有产品的图片网址。
这是一些示例代码,
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
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。