【发布时间】:2016-03-03 18:53:39
【问题描述】:
我正在尝试将 excel 转换为 Json,它应该保存在某个本地目录中。我能够保存 Json 文件,但所有都得到 Null 值。它正在逐行读取 Excel,它本身正在将其更改为 JSON 格式并再次保存,它将转到下一行并用当前行数据替换以前的 Json 文件。它在 while 循环中,因此它正在迭代和替换文件。如果我将转换器代码从 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 行数据都具有空值 :(
非常感谢提前:)。希望任何人都可以让我摆脱这个问题。
【问题讨论】:
标签: java json jackson apache-poi