fastjson解析json文件并导出excel表格

一、工程建立及jar导入
导入jar包.png

二、直接贴简单的学习代码

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class ReadFile {
     public void reader(String filePath) {
            try {
                File file = new File(filePath);
                if (file.isFile() && file.exists()) {
                    InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");
                    BufferedReader br = new BufferedReader(read);
                    String s = "";
                    String tmpstr = "";
                    while ((s = br.readLine()) != null) {
                        tmpstr += s;
                    }
                    if (tmpstr != null) {
                        process(tmpstr);
                    } 
                    System.out.println("--- end ---");
                }
            }catch (IOException e) {
                System.out.println("Error reading file content!");
                e.printStackTrace();
            }
            
        }
     
     /**
      * {
            success: true,
            root: [{
                type: 'long',
                propName: 'xmpp.nat.serverport',
                location: '0',
                propValue: '5228',
                desc: '打洞服务器端口 5228(缺省)'
            }, {
                type: 'char',
                propName: 'xmpp.security.default.pi.level',
                location: '0',
                propValue: '-1',
                desc: '缺省主控方继承级别 -1表示继承所有顶级部门权限(缺省) 0只是继承一级部门'
            }]
        }
      * @param txtStr
      */
     
     private static void process(String txtStr) {
            JSONObject json = JSONObject.parseObject(txtStr);
            Object datas = json.get("root");
            List<Property> root_list = JSON.parseArray(datas+"", Property.class);
            //创建工作簿
            HSSFWorkbook workBook = new HSSFWorkbook();
            //创建工作表  工作表的名字叫helloWorld
            HSSFSheet sheet = workBook.createSheet("系统属性");
            for(int i=0; i<root_list.size(); i++) {
                //创建行,第3行
                HSSFRow row0 = sheet.createRow(i);
                //创建单元格,操作第三行第三列
                HSSFCell cell0 = row0.createCell(0);
                HSSFCell cell1 = row0.createCell(1);
                HSSFCell cell2 = row0.createCell(2);
                cell0.setCellValue(root_list.get(i).getPropName());
                cell1.setCellValue(root_list.get(i).getPropValue());
                cell2.setCellValue(root_list.get(i).getDesc());
                
            }
            FileOutputStream fout = null;
            Date date = new Date();
            try {
                fout = new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\property("+date.getTime()+").xls"));
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }
            try {
                workBook.write(fout);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            try {
                fout.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                workBook.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,287评论 25 708
  • (万尚学习会)打卡第57天 姓名:徐娟 部门:人事部 组别:待定 【知~学习】 诵读《活法》第三章 重新认识劳动和...
    徐娟Wellin阅读 448评论 0 0
  • 今天给大家介绍是我很喜欢的一位女作者——荒川弘 荒川弘被大家亲切的称为雪姨,不是那个敲门的雪姨呦。你会问她都有什么...
    皮皮romantic阅读 694评论 2 2
  • 一个人觉得生活很沉重,就去找哲人寻求解脱之法。后来哲人叫他去走一步背一块石头到背篓里,借此来剖析他觉得人生痛苦的原...
    31慢慢阅读 979评论 2 0