转换Excel中JSON [英] Converting Excel to JSON

查看:127
本文介绍了转换Excel中JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读使用Apache-POI的Excel文档。
其中有像记录在Excel文档:

I have read an excel document using apache-poi. The excel document which have records like:

A1 A2 A3 A4

A1 A2 B3 B4

我想将它们转换成JSON数组像

i want to convert them into JSON arrays like

{ A1 : {A2 : {A3 : {A4 : some_value } } , {B3 : {B4 : some_value } } } }

其实它很容易转换成XML。
请告诉我如何解决这个问题。
只有暗示就足够了。

Actually its easy to convert to XML. Please tell me how to solve this. Only hints would suffice.

推荐答案

您可以参考以下code:

You can refer the following code:

FileInputStream inp = new FileInputStream( file );
Workbook workbook = WorkbookFactory.create( inp );

// Get the first Sheet.
Sheet sheet = workbook.getSheetAt( 0 );

    // Start constructing JSON.
    JSONObject json = new JSONObject();

    // Iterate through the rows.
    JSONArray rows = new JSONArray();
    for ( Iterator<Row> rowsIT = sheet.rowIterator(); rowsIT.hasNext(); )
    {
        Row row = rowsIT.next();
        JSONObject jRow = new JSONObject();

        // Iterate through the cells.
        JSONArray cells = new JSONArray();
        for ( Iterator<Cell> cellsIT = row.cellIterator(); cellsIT.hasNext(); )
        {
            Cell cell = cellsIT.next();
            cells.put( cell.getStringCellValue() );
        }
        jRow.put( "cell", cells );
        rows.put( jRow );
    }

    // Create the JSON.
    json.put( "rows", rows );

// Get the JSON text.
return json.toString();

这篇关于转换Excel中JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆