java导出json数据到excel中

可以用前端js导,也可以使用后端的POI导出

前端导出

base64(s) { return window.btoa(unescape(encodeURIComponent(s))) },

exportExcel() { // 前端导出excel
  console.log(this.showList)
  let str = '<tr><td>代理名称</td><td>allianceid</td><td>attachmentFileId</td>'
  for (let i = 0; i < this.showList.length; i++) {
    str += '<tr>'
    for (let item in this.showList[i]) {
      // 增加\t为了不让表格显示科学计数法或者其他格式
      str += `<td>${this.showList[i][item] + '\t'}</td>`
    }
    str += '</tr>'
  }
  let worksheet = 'Sheet1'
  let uri = 'data:application/vnd.ms-excel;base64,'
  // 下载的表格模板数据
  let template = `<html xmlns:o="urn:schemas-microsoft-com:office:office" 
  xmlns:x="urn:schemas-microsoft-com:office:excel" 
  xmlns="http://www.w3.org/TR/REC-html40">
  <head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>
    <x:Name>${worksheet}</x:Name>
    <x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>
    </x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
    </head><body><table>${str}</table></body></html>`
  // 下载模板
  window.location.href = uri + this.base64(template)
},//showList是table数据

后端POI导出excel

  public static ResponseEntity<byte[]> export(List<Position> pos) throws IOException {
        //创建一个 excel 文档
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建 excel 属性配置
        workbook.createInformationProperties();
        //获取并且配置文档属性
        DocumentSummaryInformation information = workbook.getDocumentSummaryInformation();

        information.setCategory("职位表");
        information.setManager("管理员");
        information.setCompany("liy");

        //创建表单
        HSSFSheet sheet = workbook.createSheet();

        HSSFRow row = sheet.createRow(0);
        HSSFCell c0 = row.createCell(0);
        HSSFCell c1 = row.createCell(1);
        HSSFCell c2 = row.createCell(2);
        HSSFCell c3 = row.createCell(3);


       c0.setCellValue("编号");
       c1.setCellValue("职位名称");
       c2.setCellValue("创建日期");
       c3.setCellValue("是否可用");

        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));

        for (int i = 0; i < pos.size(); i++) {
            Position position = pos.get(i);
            HSSFRow r = sheet.createRow(i + 1);
            HSSFCell cl0 = r.createCell(0);
            HSSFCell cl1 = r.createCell(1);
            HSSFCell cl2 = r.createCell(2);
            HSSFCell cl3 = r.createCell(3);

            cl0.setCellValue(position.getId());
            cl1.setCellValue(position.getName());
            cl2.setCellValue(position.getCreatedate());
            cl2.setCellStyle(cellStyle);
            cl3.setCellValue(position.getEnabled()?"是":"否");

        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        workbook.write(baos);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentDispositionFormData("attachment",new String("职位表.xls".getBytes("UTF-8"),"iso-8859-1"));
        return new ResponseEntity<byte[]>(baos.toByteArray(),headers, HttpStatus.CREATED);

    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java代码示例,通过json数据导出excel表,并返回下载url: ```java import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.UUID; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import com.alibaba.fastjson.JSON; public class ExcelExportUtil { public static String exportToExcel(String jsonData) throws IOException { // 解析json数据 List<Map<String, Object>> dataList = JSON.parseObject(jsonData, List.class); // 创建excel工作簿 Workbook workbook = new HSSFWorkbook(); // 创建第一个sheet页 Sheet sheet = workbook.createSheet("Sheet1"); // 创建表头 Row headerRow = sheet.createRow(0); int cellIndex = 0; for (String key : dataList.get(0).keySet()) { Cell cell = headerRow.createCell(cellIndex++); cell.setCellValue(key); } // 填充数据 int rowIndex = 1; for (Map<String, Object> data : dataList) { Row row = sheet.createRow(rowIndex++); cellIndex = 0; for (Object value : data.values()) { Cell cell = row.createCell(cellIndex++); if (value instanceof Number) { cell.setCellValue(((Number) value).doubleValue()); } else { cell.setCellValue(value.toString()); } } } // 生成文件名 String fileName = UUID.randomUUID().toString() + ".xls"; // 保存excel文件 File file = new File(fileName); OutputStream os = new FileOutputStream(file); workbook.write(os); os.close(); // 返回下载url String url = file.toURI().toURL().toString(); return url; } public static void main(String[] args) throws IOException { // 测试数据 List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>(); Map<String, Object> data1 = new LinkedHashMap<String, Object>(); data1.put("name", "张三"); data1.put("age", 20); data1.put("gender", "男"); dataList.add(data1); Map<String, Object> data2 = new LinkedHashMap<String, Object>(); data2.put("name", "李四"); data2.put("age", 25); data2.put("gender", "女"); dataList.add(data2); // 导出excel并返回下载url String url = exportToExcel(JSON.toJSONString(dataList)); System.out.println(url); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值