美文网首页
fastjson解析json文件并导出excel表格

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

作者: 小强不可爱 | 来源:发表于2018-08-22 11:11 被阅读0次
    一、工程建立及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();
                }
            }
    }
    

    相关文章

      网友评论

          本文标题:fastjson解析json文件并导出excel表格

          本文链接:https://www.haomeiwen.com/subject/qhmeiftx.html