Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA中Excel到Json的转换器_Java_Json_Language Agnostic_Jackson_Apache Poi - Fatal编程技术网

JAVA中Excel到Json的转换器

JAVA中Excel到Json的转换器,java,json,language-agnostic,jackson,apache-poi,Java,Json,Language Agnostic,Jackson,Apache Poi,我正在尝试将excel数据转换为JSON格式。通过读取行和列并将数据设置为list,然后将该列表转换为JSON格式,我成功地转换了excel数据 但是,我停留在JSON的下一个层次,格式如下 下面是iam使用的代码,但尚未开始使用。如何使嵌套数据(如3列数据)包含在一个数组中,如上图所示 Pojo类: public class Products implements Serializable{ @Id @Column(name="ID") private String

我正在尝试将excel数据转换为JSON格式。通过读取行和列并将数据设置为list,然后将该列表转换为JSON格式,我成功地转换了excel数据

但是,我停留在JSON的下一个层次,格式如下

下面是iam使用的代码,但尚未开始使用。如何使嵌套数据(如3列数据)包含在一个数组中,如上图所示

Pojo类

public class Products implements Serializable{

@Id
@Column(name="ID")
private String id;

@Column(name="PRODUCTID")
private String productId;

@Column(name="NAMEID")
private String nameId;

@Column(name="NAME")
private String name;

@Column(name="P_DESC")
private String desc;

@Column(name="DIMENSION")
private String dimension;

@Column(name="CATEGORY")
private String category;

@Column(name="SUB_CATEGORY")
private String subcategory;

@Column(name="CATEGORYID")
private String categoryId;

@Column(name="SUBCATEGORYID")
private String subcategoryId;

@Column(name="TAGS")
private String tags;

@Column(name="DESIGNER")
private String designer;

@Column(name="CURR")
private String curr;

@Column(name="POPULARITY")
private String popularity;

@Column(name="RELEVANCE")
private String relevance;

@Column(name="SHORTLISTED")
private String shortlisted;

@Column(name="LIKES")
private String likes;

@Column(name="CREATE_String")
private String createDt;

@Column(name="PAGEID")
private String pageId;

@Column(name="STYLENAME")
private String styleName;

@Column(name="STYLEID")
private String styleId;

@Column(name="PRICERANGE")
private String priceRange;

@Column(name="PRICEID")
private String priceId;

@Column(name="DEFAULT_PRICE")
private String defaultPrice;

@Column(name="DEFAULT_MATERIAL")
private String defaultMaterial;

@Column(name="DEFAULT_FINISH")
private String defaultFinish;
/* setters and getters */
}
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.setNameId(new DataFormatter().formatCellValue(nextRow.getCell(2)));
                        products.setName(new DataFormatter().formatCellValue(nextRow.getCell(3)));
                        products.setDesc(new DataFormatter().formatCellValue(nextRow.getCell(4)));
                        products.setDimension(new DataFormatter().formatCellValue(nextRow.getCell(5)));
                        products.setCategory(new DataFormatter().formatCellValue(nextRow.getCell(6)));
                        products.setSubcategory((new DataFormatter().formatCellValue(nextRow.getCell(7))));
                        products.setCategoryId(new DataFormatter().formatCellValue(nextRow.getCell(8)));
                        products.setSubcategoryId((new DataFormatter().formatCellValue(nextRow.getCell(9))));
                        products.setTags((new DataFormatter().formatCellValue(nextRow.getCell(10))));
                        products.setDesigner((new DataFormatter().formatCellValue(nextRow.getCell(11))));
                        products.setCurr((new DataFormatter().formatCellValue(nextRow.getCell(12))));
                        products.setPopularity((new DataFormatter().formatCellValue(nextRow.getCell(13))));
                        products.setRelevance((new DataFormatter().formatCellValue(nextRow.getCell(14))));
                        products.setShortlisted((new DataFormatter().formatCellValue(nextRow.getCell(15))));
                        products.setLikes((new DataFormatter().formatCellValue(nextRow.getCell(16))));
                        products.setCreateDt((new DataFormatter().formatCellValue(nextRow.getCell(17))));
                        products.setPageId((new DataFormatter().formatCellValue(nextRow.getCell(18))));
                        products.setStyleName((new DataFormatter().formatCellValue(nextRow.getCell(19))));
                        products.setStyleId((new DataFormatter().formatCellValue(nextRow.getCell(20))));
                        products.setPriceRange((new DataFormatter().formatCellValue(nextRow.getCell(21))));
                        products.setPriceId((new DataFormatter().formatCellValue(nextRow.getCell(22))));
                        products.setDefaultPrice((new DataFormatter().formatCellValue(nextRow.getCell(23))));
                        products.setDefaultMaterial((new DataFormatter().formatCellValue(nextRow.getCell(24))));
                        products.setDefaultFinish((new DataFormatter().formatCellValue(nextRow.getCell(25))));

                        Map<String, String> inputMap = new HashMap<String, String>();
                        inputMap.put("name", "Java2Novice");
                        inputMap.put("site", "http://java2novice.com");
                        productsList.add(products);
                    System.out.println(productsList);
                

                     
           // 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 and save into file directly
       mapper.writeValue(new File("D:\\"+location+"mygubbi.json"), productsList);
        // Convert object to JSON string and save into file directly
        mapper.writeValue(new File("D:\\products.json"), productsList);
        
        // Convert object to JSON string
        String jsonInString = mapper.writeValueAsString(productsList);
        System.out.println("JsonInString " +jsonInString);

        // Convert object to JSON string and pretty print
        jsonInString = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(products);
        System.out.println("Final Json" +mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(products));
        mapper.writeValue(new File("D:\\productsJson.json"), jsonInString);


        }
        } catch (JsonGenerationException e) {
    e.printStackTrace();
} catch (JsonMappingException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
finally {


}
}
卓越JSONConverter类

public class Products implements Serializable{

@Id
@Column(name="ID")
private String id;

@Column(name="PRODUCTID")
private String productId;

@Column(name="NAMEID")
private String nameId;

@Column(name="NAME")
private String name;

@Column(name="P_DESC")
private String desc;

@Column(name="DIMENSION")
private String dimension;

@Column(name="CATEGORY")
private String category;

@Column(name="SUB_CATEGORY")
private String subcategory;

@Column(name="CATEGORYID")
private String categoryId;

@Column(name="SUBCATEGORYID")
private String subcategoryId;

@Column(name="TAGS")
private String tags;

@Column(name="DESIGNER")
private String designer;

@Column(name="CURR")
private String curr;

@Column(name="POPULARITY")
private String popularity;

@Column(name="RELEVANCE")
private String relevance;

@Column(name="SHORTLISTED")
private String shortlisted;

@Column(name="LIKES")
private String likes;

@Column(name="CREATE_String")
private String createDt;

@Column(name="PAGEID")
private String pageId;

@Column(name="STYLENAME")
private String styleName;

@Column(name="STYLEID")
private String styleId;

@Column(name="PRICERANGE")
private String priceRange;

@Column(name="PRICEID")
private String priceId;

@Column(name="DEFAULT_PRICE")
private String defaultPrice;

@Column(name="DEFAULT_MATERIAL")
private String defaultMaterial;

@Column(name="DEFAULT_FINISH")
private String defaultFinish;
/* setters and getters */
}
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.setNameId(new DataFormatter().formatCellValue(nextRow.getCell(2)));
                        products.setName(new DataFormatter().formatCellValue(nextRow.getCell(3)));
                        products.setDesc(new DataFormatter().formatCellValue(nextRow.getCell(4)));
                        products.setDimension(new DataFormatter().formatCellValue(nextRow.getCell(5)));
                        products.setCategory(new DataFormatter().formatCellValue(nextRow.getCell(6)));
                        products.setSubcategory((new DataFormatter().formatCellValue(nextRow.getCell(7))));
                        products.setCategoryId(new DataFormatter().formatCellValue(nextRow.getCell(8)));
                        products.setSubcategoryId((new DataFormatter().formatCellValue(nextRow.getCell(9))));
                        products.setTags((new DataFormatter().formatCellValue(nextRow.getCell(10))));
                        products.setDesigner((new DataFormatter().formatCellValue(nextRow.getCell(11))));
                        products.setCurr((new DataFormatter().formatCellValue(nextRow.getCell(12))));
                        products.setPopularity((new DataFormatter().formatCellValue(nextRow.getCell(13))));
                        products.setRelevance((new DataFormatter().formatCellValue(nextRow.getCell(14))));
                        products.setShortlisted((new DataFormatter().formatCellValue(nextRow.getCell(15))));
                        products.setLikes((new DataFormatter().formatCellValue(nextRow.getCell(16))));
                        products.setCreateDt((new DataFormatter().formatCellValue(nextRow.getCell(17))));
                        products.setPageId((new DataFormatter().formatCellValue(nextRow.getCell(18))));
                        products.setStyleName((new DataFormatter().formatCellValue(nextRow.getCell(19))));
                        products.setStyleId((new DataFormatter().formatCellValue(nextRow.getCell(20))));
                        products.setPriceRange((new DataFormatter().formatCellValue(nextRow.getCell(21))));
                        products.setPriceId((new DataFormatter().formatCellValue(nextRow.getCell(22))));
                        products.setDefaultPrice((new DataFormatter().formatCellValue(nextRow.getCell(23))));
                        products.setDefaultMaterial((new DataFormatter().formatCellValue(nextRow.getCell(24))));
                        products.setDefaultFinish((new DataFormatter().formatCellValue(nextRow.getCell(25))));

                        Map<String, String> inputMap = new HashMap<String, String>();
                        inputMap.put("name", "Java2Novice");
                        inputMap.put("site", "http://java2novice.com");
                        productsList.add(products);
                    System.out.println(productsList);
                

                     
           // 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 and save into file directly
       mapper.writeValue(new File("D:\\"+location+"mygubbi.json"), productsList);
        // Convert object to JSON string and save into file directly
        mapper.writeValue(new File("D:\\products.json"), productsList);
        
        // Convert object to JSON string
        String jsonInString = mapper.writeValueAsString(productsList);
        System.out.println("JsonInString " +jsonInString);

        // Convert object to JSON string and pretty print
        jsonInString = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(products);
        System.out.println("Final Json" +mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(products));
        mapper.writeValue(new File("D:\\productsJson.json"), jsonInString);


        }
        } catch (JsonGenerationException e) {
    e.printStackTrace();
} catch (JsonMappingException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
finally {


}
}
publicstaticvoiduploadxls(多部分文件、文档文档)
抛出IOException{
产品=新产品();
List productsList=new ArrayList();
logger.info(“上传Excel方法”);
HSSF工作簿wb=null;
试一试{
wb=新的HSSFWorkbook(file.getInputStream());
System.out.println(“工作簿:+wb”);
HSSFSheet sheet=wb.getSheetAt(0);
System.out.println(“工作表:+工作表”);
HSSFRow row;
迭代器迭代器=sheet.Iterator();
while(iterator.hasNext()){
产品=新产品();
行nextRow=iterator.next();
迭代器cellIterator=nextRow.cellIterator();
Cell=cellIterator.next();
迭代器单元格=nextRow.cellIterator();
cell=(HSSFCell)cells.next();
if(cell.getCellType()==HSSFCell.cell\u TYPE\u字符串)
{
System.out.print(cell.getStringCellValue()+);
}
else if(cell.getCellType()==HSSFCell.cell\u TYPE\u NUMERIC)
{
System.out.print(cell.getNumericCellValue()+);
}
else if(HSSFDateUtil.isCellDateFormatted(单元格)){
日期日期=HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
}否则
{
//你可以亨德尔布尔,公式,错误
}
products.setId(新的DataFormatter().formatCellValue(nextRow.getCell(0));
products.setProductId(新的DataFormatter().formatCellValue(nextRow.getCell(1));
products.setNameId(新的DataFormatter().formatCellValue(nextRow.getCell(2));
products.setName(新的DataFormatter().formatCellValue(nextRow.getCell(3));
products.setDesc(新的DataFormatter().formatCellValue(nextRow.getCell(4));
products.setDimension(新的DataFormatter().formatCellValue(nextRow.getCell(5));
products.setCategory(新的DataFormatter().formatCellValue(nextRow.getCell(6));
products.setSubcategory((新的DataFormatter().formatCellValue(nextRow.getCell(7)));
products.setCategoryId(新的DataFormatter().formatCellValue(nextRow.getCell(8));
setSubcategoryId((新的DataFormatter().formatCellValue(nextRow.getCell(9)));
setTags((新的DataFormatter().formatCellValue(nextRow.getCell(10)));
setDesigner((新的DataFormatter().formatCellValue(nextRow.getCell(11)));
products.setCurr((新的DataFormatter().formatCellValue(nextRow.getCell(12)));
products.setPopularity((新的DataFormatter().formatCellValue(nextRow.getCell(13)));
products.setRelevance((新的DataFormatter().formatCellValue(nextRow.getCell(14)));
products.setShortlisted((新的DataFormatter().formatCellValue(nextRow.getCell(15)));
products.setLikes((新的DataFormatter().formatCellValue(nextRow.getCell(16)));
products.setCreateDt((新的DataFormatter().formatCellValue(nextRow.getCell(17)));
products.setPageId((新的DataFormatter().formatCellValue(nextRow.getCell(18)));
products.setStyleName((新的DataFormatter().formatCellValue(nextRow.getCell(19)));
products.setStyleId((新的DataFormatter().formatCellValue(nextRow.getCell(20)));
products.setPriceRange((新的DataFormatter().formatCellValue(nextRow.getCell(21)));
products.setPriceId((新的DataFormatter().formatCellValue(nextRow.getCell(22)));
products.setDefaultPrice((新的DataFormatter().formatCellValue(nextRow.getCell(23)));
products.setDefaultMaterial((新的DataFormatter().formatCellValue(nextRow.getCell(24)));
products.setDefaultFinish((新的DataFormatter().formatCellValue(nextRow.getCell(25)));
Map inputMap=newhashmap();
put(“name”,“Java2Novice”);
inputMap.put(“站点”http://java2novice.com");
产品列表。添加(产品);
System.out.println(产品列表);
//JSON转换器
ObjectMapper mapper=新的ObjectMapper();
System.out.println(“产品列表:+产品”);
DateFormat DateFormat=新的SimpleDataFormat(“yyyyMMddHHmmss”);
日期=新日期();
字符串位置=dateFormat.format(日期);
System.out.println(“产品列表最终:+产品”);
//将对象转换为JSON字符串并直接保存到文件中
writeValue(新文件(“D:\\”+location+“mygubbi.json”)、productsList);
//将对象转换为JSON字符串并直接保存到文件中
writeValue(新文件(“D:\\products.json”)、productsList);
//将对象转换为JSON字符串
字符串jsonInString=mapper.writeValueAsString(productsList);
System.out.println(“JsonInString
    public static Map getImgs(HSSFWorkbook wb) {

   Map hm1 = new HashMap<String, ArrayList<Somename>>();
    HSSFSheet sheet = wb.getSheetAt(2);
    Iterator<Row> iterator= sheet.iterator();
    ArrayList<Somename> al = new ArrayList<Somename>();
    while (iterator.hasNext()) {
        Row row = iterator.next();

        String sno = new DataFormatter().formatCellValue(row.getCell(0));


        if (hm1.get(sno) == null) {
            hm1.put(sno, new ArrayList());
        }

        String some= new DataFormatter().formatCellValue(row
                .getCell(1));

        Somename img1 = new Somename(some);
        System.out.println(img1);

        ArrayList r = (ArrayList) hm1.get(sno);
        r.add(img1);

    }
    return hm1;
    }
  ArrayList<Somename> al = (ArrayList<Somename>) hm1.get(id_prod);
                        Somename[] ar = new Images[al.size()];

                        products.setSome(al.toArray(imgfar));