Java读取Excel转换成JSON字符串进而转换成Java对象

Jar包

Excel数据

Java代码

package ExcelToJSON;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.File;

public class Excel {
    public static void main(String[] args) {
        Sheet sheet;
        Workbook book;
        Cell cell1, cell2, cell3, cell4, cell5,cell6,cell7,cell8;
        JSONArray array = new JSONArray();
        try {
            //为要读取的excel文件名
            book = Workbook.getWorkbook(new File("F://a.xls"));

            //获得第一个工作表对象(ecxel中sheet的编号从0开始,0,1,2,3,....)
            sheet = book.getSheet(0);

            for (int i = 1; i < sheet.getRows(); i++) {
                //获取每一行的单元格
                cell1 = sheet.getCell(0, i);//(列,行)
                cell2 = sheet.getCell(1, i);
                cell3 = sheet.getCell(2, i);
                cell4 = sheet.getCell(3, i);
                cell5 = sheet.getCell(4, i);
                cell5 = sheet.getCell(5, i);
                cell5 = sheet.getCell(6, i);
                cell5 = sheet.getCell(7, i);
                if ("".equals(cell1.getContents())) {//如果读取的数据为空
                    break;
                }
                JSONObject object = new JSONObject();
                object.put("题干",cell1.getContents());
                object.put("类型",cell2.getContents());
                object.put("A选项",cell3.getContents());
                object.put("B选项",cell4.getContents());
                object.put("C选项",cell5.getContents());
                object.put("D选项",cell5.getContents());
                object.put("答案",cell5.getContents());
                object.put("解析",cell5.getContents());
                array.add(object);
            }
            System.out.println(array.toString());
            book.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
[{"题干":"1题题干","类型":"选择","A选项":"低电压配电柜","B选项":"自产","C选项":"低电压配电柜","D选项":"低电压配电柜","答案":"低电压配电柜","解析":"低电压配电柜"},{"题干":"2题题干","类型":"选择","A选项":"低电压配电柜","B选项":"自产","C选项":"低电压配电柜","D选项":"低电压配电柜","答案":"低电压配电柜","解析":"低电压配电柜"},{"题干":"3题题干","类型":"选择","A选项":"低电压配电柜","B选项":"外购","C选项":"低电压配电柜","D选项":"低电压配电柜","答案":"低电压配电柜","解析":"低电压配电柜"}]
时间: 2024-08-26 10:47:48

Java读取Excel转换成JSON字符串进而转换成Java对象的相关文章

java将其他数据格式转换成json字符串格式

package com.wangbo.util; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.math.BigDecimal; import java.math.BigInteger; import java.util.HashMap; import java.util.List; import

JSON对象转换成JSON字符串

1.问题背景 有一个json对象,需要将其转换成json字符串 JSON.stringify(obj) 2.实现源码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtm

将传入结构体 pMtInfo 中包含的数据内容转换成 JSON 字符串返回

upu_struct.h封装了有关  pMtInfo结构体的内容,用到的部分如下图所示: 利用jansson库实现将传入结构体 pMtInfo 中包含的数据内容转换成 JSON 字符串返回 代码如下: #include <stdio.h> #include <string.h> #include "jansson.h" #include "upu_struct.h" #include "upu_proto_parse.h"

Newtonsoft.Json 把对象转换成json字符串

var resultJson = new { records = rowCount, page = pageindex, //总页数=(总页数+页大小-1)/页大小 total = (rowCount + pagesize - 1) / pagesize,//总页数 rows = data.ToList<Web.Entity.Db.Table1Bean>(), }; //JsonResult jr = Json(resultJson, "application/json",

json字符串转换成对象,对象转换成json字符串

方法一: 程序集:  System.Web.Extensions; 命名空间:System.Web.Script.Serialization; 最重要的类:JavaScriptSerializer //实例化 JavaScriptSerializer js = new JavaScriptSerializer(); js.Serialize();//将对象转换成json字符串:    序列号 js.Deserialize();//将json字符串转换成对象:  反序列化 方法二: 程序集:New

Android 将实体类类转换成json字符串

/**      * 将实体类转换成json字符串对象            注意此方法需要第三方gson  jar包     * @param obj  对象     * @return  map     */    private String toJson(Object obj,int method) {        // TODO Auto-generated method stub        if (method==1) { //字段是首字母小写,其余单词首字母大写      

JQuery将form表单值转换成json字符串函数

由于后台接口限定,必须要将表单内容转换成json字符串提交,因此写了一个将form表单值转成json字符串的函数.     前提:页面引入了JQuery 下面直接上代码 一.代码 /** 表单序列化成json字符串的方法 */ function form2JsonString(formId) { var paramArray = $('#' + formId).serializeArray(); /*请求参数转json对象*/ var jsonObj={}; $(paramArray).each

JavaScript对象与JSON字符串互相转换详解

JSON是目前互联网数据传输所采用的的主要格式,很多编程语言都有针对JSON的解析器和序列化器,在web前台领域,js对象与JSON字符串的互相转换需求也较为常见. 此文主要讲解js对象和JSON字符串互相转换的方法和一些细节 首先如果需要在开发中使用JSON传输及相应转换需要在html中引入json.js或者json2.js,下载地址:https://github.com/douglascrockford/JSON-js 1.js对象转JSON字符串(序列化) var jsonText = J

json数据处理:读取文件中的json字符串,转为python字典

方法1: 读取文件中的json字符串, 再用json.loads转为python字典 import json str_file = './960x540/config.json' with open(str_file, 'r') as f: print("Load str file from {}".format(str_file)) str1 = f.read() r = json.loads(str1) print(type(r)) print(r) print(r['under_