导出excel
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package com.lovenav.controller;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
@@ -10,15 +12,20 @@ import com.lovenav.entity.UrlList;
|
||||
import com.lovenav.service.UrlCateListService;
|
||||
import com.lovenav.service.UrlListService;
|
||||
import com.lovenav.vo.CateAndUrl;
|
||||
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 org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
@RestController
|
||||
@RequestMapping("/UrlAndCate")
|
||||
public class UrlAndCateController {
|
||||
@@ -43,6 +50,7 @@ public class UrlAndCateController {
|
||||
try {
|
||||
JsonNode rootNode = objectMapper.readTree(data2);
|
||||
disposeBookmarkFunction1(rootNode,"top",email);
|
||||
countCateContainUrlNumber(email);
|
||||
HashMap<String, Object> result = new HashMap<>();
|
||||
result.put("code", 200);
|
||||
result.put("msg", "查询成功");
|
||||
@@ -80,9 +88,9 @@ public class UrlAndCateController {
|
||||
|
||||
//处理成JSON
|
||||
@RequestMapping("/disposeBookmarkToJson")
|
||||
public String disposeBookmarkToJson(Integer userId)
|
||||
public String disposeBookmarkToJson(String userId)
|
||||
{
|
||||
List<UrlCateList> urlCateLists = urlCateListService.selectUrListByUserId(userId);
|
||||
List<UrlCateList> urlCateLists = urlCateListService.selectUrListByUserId(Integer.valueOf(userId));
|
||||
List<UrlList> urlLists = urlListService.selectUrList();
|
||||
|
||||
List<CateAndUrl> cateAndUrlList = new ArrayList<>();
|
||||
@@ -185,4 +193,90 @@ public class UrlAndCateController {
|
||||
String jsonString = JSONObject.toJSONString(result);
|
||||
return jsonString;
|
||||
}
|
||||
|
||||
|
||||
public String countCateContainUrlNumber(String email){
|
||||
HashMap<String, Object> result = new HashMap<>();
|
||||
result.put("code", 200);
|
||||
result.put("msg", "查询成功");
|
||||
result.put("data",urlCateListService.countCateContainUrlNumber(email));
|
||||
String jsonString = JSONObject.toJSONString(result);
|
||||
return jsonString;
|
||||
}
|
||||
//处理成JSON
|
||||
// @RequestMapping("/disposeJsonToExcel")
|
||||
// public String disposeJsonToExcel(String userId)
|
||||
// {
|
||||
//
|
||||
// HashMap<String, Object> result = new HashMap<>();
|
||||
// result.put("code", 200);
|
||||
// result.put("msg", "查询成功");
|
||||
// result.put("data",urlCateListService.getUrl(userId));
|
||||
// String jsonString = JSONObject.toJSONString(result);
|
||||
// return jsonString;
|
||||
// }
|
||||
|
||||
/**
|
||||
* json 转 excel
|
||||
* @param jsonArray
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public HSSFWorkbook jsonToExcel(JSONArray jsonArray) throws IOException {
|
||||
Set<String> keys = new HashSet<>();
|
||||
// 创建HSSFWorkbook对象
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
// 创建HSSFSheet对象
|
||||
HSSFSheet sheet = wb.createSheet("sheet0");
|
||||
|
||||
|
||||
List<JSONObject> jsonObjects = jsonArray.toList(JSONObject.class);
|
||||
|
||||
// 创建HSSFRow对象
|
||||
HSSFRow row ;
|
||||
for (int i=0;i<jsonObjects.size();i++){
|
||||
String temp = jsonObjects.get(i).keySet().toString();
|
||||
String target = temp.substring(1,temp.length()-1);
|
||||
keys.add(target);
|
||||
}
|
||||
|
||||
for( int i =0 ;i<jsonObjects.size(); i++)
|
||||
{
|
||||
row = sheet.createRow(i);
|
||||
HSSFCell cell = row.createCell(0);
|
||||
String temp = jsonObjects.get(i).keySet().toString();
|
||||
String target = temp.substring(1,temp.length()-1);
|
||||
String target1 = temp.substring(2,temp.length()-2);
|
||||
cell.setCellValue(target1);
|
||||
cell = row.createCell(1);
|
||||
cell.setCellValue(jsonObjects.get(i).get(target).toString().substring(2,temp.length()-2));
|
||||
}
|
||||
|
||||
return wb;
|
||||
}
|
||||
@RequestMapping ("/dataDownload/excel")
|
||||
@ResponseBody
|
||||
public String exportExcel(String userId, HttpServletResponse response) {
|
||||
try {
|
||||
//此处为你的json数组
|
||||
HSSFWorkbook sheets = jsonToExcel(urlCateListService.getUrl(userId));
|
||||
// 配置文件下载
|
||||
response.setHeader("content-type", "application/octet-stream");
|
||||
response.setContentType("application/octet-stream");
|
||||
// 下载文件能正常显示中文
|
||||
response.setHeader("Content-Disposition", "attachment;filename=data.xls");
|
||||
OutputStream os = response.getOutputStream();
|
||||
sheets.write(os);
|
||||
sheets.close();
|
||||
os.close();
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
HashMap<String, Object> result = new HashMap<>();
|
||||
result.put("code", 500);
|
||||
result.put("msg", "下载失败");
|
||||
|
||||
return JSON.toJSONString(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user