JAVA将JSON写入excel

该代码段展示了如何将JSON数据解析成Java对象,进一步转化为列表,并创建Excel文件进行数据导出。主要涉及JSONObject、JSONArray、HSSFWorkbook等库的操作,实现了从JSON到Excel的转换过程。
摘要由CSDN通过智能技术生成
   JSONObject object= JSON.parseObject(result);
        JSONObject departmentSharingRequestPackageDTO=object.getJSONObject("data");
        JSONArray jsonArray=departmentSharingRequestPackageDTO.getJSONArray("list");
        List<JSONObject> list = JSONObject.parseArray(jsonArray.toJSONString(), JSONObject.class);
        Set keys = null;
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("sheet0");
        int roleNo = 0;
        int rowNo = 0;
        for(JSONObject jsonObject:list){
            HSSFRow row = sheet.createRow(roleNo++);
            if (keys == null) {
                keys = jsonObject.keySet();
                for (Object s : keys) {
                    HSSFCell cell = row.createCell(rowNo++);
                    cell.setCellValue(s.toString());
                }
                rowNo = 0;
                row = sheet.createRow(roleNo++);
            }
            for (Object s : keys) {
                HSSFCell cell = row.createCell(rowNo++);
                cell.setCellValue(jsonObject.getString((String) s));
            }
            rowNo = 0;
        }
        FileOutputStream output = new FileOutputStream("/data/file/target.xls");
        wb.write(output);
        wb.close();
        output.flush();
        output.close();
        FileInputStream fileInputStream = new FileInputStream("/data/file/target.xls");
        String encodedFileName = URLEncoder.encode(departmentSearchResultParam.getInterfaceId(), "utf-8");
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;fileName=" + encodedFileName);
        ServletOutputStream outputStream = response.getOutputStream();
        byte[] buffer = new byte[1024];
        while (fileInputStream.read(buffer) != -1) {
            outputStream.write(buffer);
        }
        log.info("下载完成");
        outputStream.flush();
        outputStream.close();
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用Apache POI库来读取Excel文件,然后将数据转换为JSON格式。以下是一个示例代码: ```java import org.apache.poi.ss.usermodel.*; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; public class ExcelToJsonConverter { public static void main(String[] args) { try { // 读取Excel文件 FileInputStream file = new FileInputStream(new File("example.xlsx")); Workbook workbook = WorkbookFactory.create(file); Sheet sheet = workbook.getSheetAt(0); // 创建JSON对象 JSONObject jsonObject = new JSONObject(); JSONArray jsonArray = new JSONArray(); // 读取每一行数据 for (int i = 1; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); JSONObject obj = new JSONObject(); // 读取每一列数据 for (int j = 0; j < row.getLastCellNum(); j++) { Cell cell = row.getCell(j); String columnName = sheet.getRow(0).getCell(j).getStringCellValue(); obj.put(columnName, cell); } jsonArray.add(obj); } jsonObject.put("data", jsonArray); // 将JSON对象写入文件 FileWriter fileWriter = new FileWriter("output.json"); fileWriter.write(jsonObject.toJSONString()); fileWriter.flush(); fileWriter.close(); System.out.println("转换完成"); } catch (Exception e) { e.printStackTrace(); } } } ``` 其中,`example.xlsx`是要读取的Excel文件名,`output.json`是要输出的JSON文件名。这段代码会将Excel文件的第一行作为JSON对象的属性名,每一行数据作为JSON对象的属性值,然后输出到JSON文件中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值