[英]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.