[英]Converting excel to JSON in JAVA using poi and jackson
我正在尝试将Excel转换为Json,它应该保存在一些本地目录中。 我能够保存Json文件,但是所有人都得到了Null值。 它正在逐行读取Excel,并且它本身正在将其更改为JSON格式,并再次保存以转到下一行,并用当前行数据替换之前的Json文件。 它在while循环中,因此它正在迭代并替换文件。 如果我从一会儿移走转换器代码,则只会传来一行数据,而来的是带有列名的空值(json数据),而不是34行数据(完整数据)。
这是代码。 请建议我如何才能实现整个要转换的Excel数据,并将文件保存在本地目录中。
public static void uploadXLS(MultipartFile file, Document doc)
throws IOException {
Products products = new Products();
List<Products> productsList = new ArrayList<Products>();
logger.info("uploadExcel method");
HSSFWorkbook wb = null;
try {
wb= new HSSFWorkbook(file.getInputStream());
System.out.println("workbook: "+wb);
HSSFSheet sheet = wb.getSheetAt(0);
System.out.println("worksheet: "+sheet);
HSSFRow row;
Iterator<Row> iterator = sheet.iterator();
while (iterator.hasNext()) {
products = new Products();
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
Cell cell = cellIterator.next();
Iterator cells = nextRow.cellIterator();
cell=(HSSFCell) cells.next();
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING)
{
System.out.print(cell.getStringCellValue()+" ");
}
else if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
{
System.out.print(cell.getNumericCellValue()+" ");
}
else if(HSSFDateUtil.isCellDateFormatted(cell)){
Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
} else
{
//U Can Handel Boolean, Formula, Errors
}
products.setId(new DataFormatter().formatCellValue(nextRow.getCell(0)));
products.setProductId(new DataFormatter().formatCellValue(nextRow.getCell(1)));
products.setPopularity((new DataFormatter().formatCellValue(nextRow.getCell(12))));
products.setRelevance((new DataFormatter().formatCellValue(nextRow.getCell(13))));
products.setShortlisted((new DataFormatter().formatCellValue(nextRow.getCell(14))));
products.setLikes((new DataFormatter().formatCellValue(nextRow.getCell(15))));
products.setCreateDt((new DataFormatter().formatCellValue(nextRow.getCell(16))));
products.setPageId((new DataFormatter().formatCellValue(nextRow.getCell(17))));
products.setStyleName(nextRow.getCell(18).getStringCellValue());
products.setStyleId(nextRow.getCell(19).getStringCellValue());
products.setPriceRange(nextRow.getCell(20).getStringCellValue());
products.setPriceId(nextRow.getCell(21).getStringCellValue());
// products.setDefaultPrice(nextRow.getCell(22).getStringCellValue());
products.setDefaultMaterial(nextRow.getCell(23).getStringCellValue());
products.setDefaultFinish((new DataFormatter().formatCellValue(nextRow.getCell(24))));
productsList.add(products);
System.out.println(productsList.add(products));
// JSON CONVERTER
ObjectMapper mapper = new ObjectMapper();
System.out.println("productsList: "+products);
DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date();
String location = dateFormat.format(date);
System.out.println("productsList final: "+products);
// Convert object to JSON string
String jsonInString = mapper.writeValueAsString(products);
System.out.println("JsonInString " +jsonInString);
// Convert object to JSON string and pretty print
jsonInString = mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(products);
mapper.writeValue(new File("D:\\"+location+"products.json"), productsList);
}
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
}
}
附加屏幕截图,在这里您可以看到excel中有34行。 它运行良好并显示了值,但是最后一个Json生成的文件具有空值。 所有34行数据都具有空值:(
非常感谢Advance :)。 希望任何人都能使我摆脱这个问题。
首先,您每次必须重新实例化 Product对象,否则列表的末尾只有一个对象。
.....
Iterator<Row> iterator = sheet.iterator();
while (iterator.hasNext()) {
products = new Products(); // re-instantiation.
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
.....
产品包含单个产品信息。 如果可能,然后将其重命名为Product。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.