导出excel

This commit is contained in:
cyk 2023-12-22 19:49:24 +08:00
parent b30162391d
commit cc5276705d
8 changed files with 270 additions and 13 deletions

11
pom.xml
View File

@ -135,6 +135,17 @@
<version>3.7.1</version>
</dependency>
<!-- 下载excel-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
</dependencies>
<dependencyManagement>

View File

@ -0,0 +1,30 @@
package com.lovenav.controller;
import com.alibaba.fastjson2.JSONObject;
import com.lovenav.dao.AuthDao;
import com.lovenav.entity.Auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
@RestController
@RequestMapping("/Auth")
public class AuthController {
@Autowired
AuthDao authDao;
@RequestMapping("/retAllAuth")
public String retAllAuth(){
List<Auth> auths = authDao.selectAllAuth();
HashMap<String, Object> result = new HashMap<>();
result.put("code", 200);
result.put("msg", "查询结果");
result.put("data",auths);
String jsonString = JSONObject.toJSONString(result);
return jsonString;
}
}

View File

@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@ -42,6 +43,7 @@ public class SearchController {
redisService.addSearchHistoryByUserId(userId, searchKey);
redisService.incrementScoreByUserId(searchKey);
redisService.incrementScore(searchKey);
//返回网站数据
@ -67,15 +69,41 @@ public class SearchController {
@RequestMapping("/getSearchHistoryByUserId")
public String getSearchHistoryByUserId(String userId)
{
String res = "123";
return res;
List<String> stringList = redisService.getSearchHistoryByUserId(userId);
HashMap<String, Object> result = new HashMap<>();
result.put("code", 200);
result.put("msg", "查询成功");
result.put("data",stringList);
String jsonString = JSONObject.toJSONString(result);
return jsonString;
}
/**
* 删除个人历史数据
*/
@RequestMapping("/delSearchHistoryByUserId")
public String delSearchHistoryByUserId(String userId, String searchKey){
String res = "123";
return res;
Long f = redisService.delSearchHistoryByUserId(userId,searchKey);
HashMap<String, Object> result = new HashMap<>();
result.put("code", 200);
result.put("msg", "删除结果");
result.put("data",f);
String jsonString = JSONObject.toJSONString(result);
return jsonString;
}
/**
* 根据searchKey搜索其相关最热的前十名 (如果searchKey为null空则返回redis存储的前十最热词条)
*/
@RequestMapping("/getHotList")
public String getHotList(String searchKey){
List<String> stringList = redisService.getHotList(searchKey);
HashMap<String, Object> result = new HashMap<>();
result.put("code", 200);
result.put("msg", "查询结果");
result.put("data",stringList);
String jsonString = JSONObject.toJSONString(result);
return jsonString;
}
}

View File

@ -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);
}
}
}

View File

@ -1,9 +1,13 @@
package com.lovenav.dao;
import com.lovenav.entity.Auth;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
@Mapper
public interface AuthDao {
int deleteByPrimaryKey(Integer id);
@ -16,4 +20,6 @@ public interface AuthDao {
int updateByPrimaryKeySelective(Auth record);
int updateByPrimaryKey(Auth record);
List<Auth> selectAllAuth();
}

View File

@ -1,5 +1,6 @@
package com.lovenav.service;
import com.alibaba.fastjson2.JSONArray;
import com.lovenav.entity.UrlCateList;
import java.util.List;
@ -9,4 +10,8 @@ public interface UrlCateListService {
public int selectAndInsertUrlCate(String email , String cateName , String parent);
public List<UrlCateList> selectUrListByUserId(Integer userId);
public String countCateContainUrlNumber(String userId);
public JSONArray getUrl(String userId);
}

View File

@ -1,16 +1,19 @@
package com.lovenav.service.serviceImpl;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.lovenav.dao.UrlCateListDao;
import com.lovenav.dao.UrlListDao;
import com.lovenav.dao.UserDao;
import com.lovenav.entity.UrlCateList;
import com.lovenav.entity.UrlList;
import com.lovenav.entity.User;
import com.lovenav.service.UrlCateListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.*;
@Transactional
@Service("urlCateListService")
@ -20,7 +23,8 @@ public class UrlCateListServiceImpl implements UrlCateListService {
@Autowired
UrlCateListDao urlCateListDao;
@Autowired
UrlListDao urlListDao;
@Autowired
UserDao userDao;
public int selectAndInsertUrlCate(String email , String cateName , String parent )
@ -53,4 +57,75 @@ public class UrlCateListServiceImpl implements UrlCateListService {
public List<UrlCateList> selectUrListByUserId(Integer userId){
return urlCateListDao.selectUrListByUserId(userId);
}
public String countCateContainUrlNumber(String email){
User user = userDao.selectByEmail(email);
int userId = user.getId();
List<UrlCateList> urlCateLists =urlCateListDao.selectUrListByUserId(userId);
List<UrlList> urlLists = urlListDao.selectUrList();
HashMap<String,Integer> CateNumber = new HashMap<>();
for(UrlCateList urlCateList :urlCateLists)
{
CateNumber.put(urlCateList.getName(),0);
}
for(UrlList urlList : urlLists){
Long id = Long.valueOf(urlList.getCateId());
while(id!= 0)
{
for(UrlCateList urlCateList :urlCateLists)
{
if(id == Long.valueOf(urlCateList.getId())){
int cateNum = CateNumber.get(urlCateList.getName())+1;
CateNumber.put(urlCateList.getName(),cateNum);
id=Long.valueOf(urlCateList.getRootCateId());
break;
}
}
}
}
Iterator iterator = CateNumber.entrySet().iterator();
while(iterator.hasNext()) {
Map.Entry<String, Integer> entry=(Map.Entry<String, Integer>) iterator.next();
for(int i = 0; i<urlCateLists.size();i++)
{
UrlCateList urlCateList = urlCateLists.get(i);
if(urlCateList.getName().equals(entry.getKey()))
{
urlCateList.setUrlNumber(Long.valueOf(entry.getValue()));
urlCateListDao.updateByPrimaryKeySelective(urlCateList);
break;
}
}
}
return CateNumber.toString();
}
public JSONArray getUrl(String userId)
{
List<UrlCateList> urlCateLists =urlCateListDao.selectUrListByUserId(Integer.valueOf(userId));
List<UrlList> urlLists = urlListDao.selectUrList();
JSONArray jsonArray = new JSONArray();
for(UrlCateList urlCateList : urlCateLists)
{
int parentId = urlCateList.getId();
for(UrlList urlList : urlLists)
{
if(urlList.getCateId() == parentId)
{
JSONObject jsonObject = new JSONObject();
jsonObject.put(urlList.getName(),urlList.getUrl());
jsonArray.add(jsonObject);
break;
}
}
}
return jsonArray;
}
}

View File

@ -19,6 +19,14 @@
from ln_auth
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectAllAuth" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from ln_auth
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from ln_auth
where id = #{id,jdbcType=INTEGER}