2020.2.16 实现了管理员数据导出为Excel

main
ppdxzz 6 years ago
parent f8533f0fa0
commit 9762f91c3c

@ -10,32 +10,21 @@
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/cn/ppdxzz/controller/AdminController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/cn/ppdxzz/controller/AdminController.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/cn/ppdxzz/dao/AdminDao.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/cn/ppdxzz/dao/AdminDao.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/cn/ppdxzz/service/AdminService.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/cn/ppdxzz/service/AdminService.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/cn/ppdxzz/service/impl/AdminServiceImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/cn/ppdxzz/service/impl/AdminServiceImpl.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/resources/spring-mvc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/spring-mvc.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/webapp/WEB-INF/jsp/admin-edit.jsp" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/webapp/WEB-INF/jsp/admin-edit.jsp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/webapp/WEB-INF/jsp/admin-list.jsp" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/webapp/WEB-INF/jsp/admin-list.jsp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/webapp/WEB-INF/jsp/login.jsp" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/webapp/WEB-INF/jsp/login.jsp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/webapp/WEB-INF/jsp/main.jsp" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/webapp/WEB-INF/jsp/main.jsp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/webapp/WEB-INF/web.xml" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/webapp/WEB-INF/web.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/webapp/js/excel.min.js" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/webapp/admin-edit.html" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/cn/ppdxzz/controller/AdminController.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/cn/ppdxzz/controller/AdminController.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/cn/ppdxzz/dao/AdminDao.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/cn/ppdxzz/dao/AdminDao.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/cn/ppdxzz/service/AdminService.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/cn/ppdxzz/service/AdminService.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/cn/ppdxzz/service/impl/AdminServiceImpl.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/cn/ppdxzz/service/impl/AdminServiceImpl.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/spring-mvc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/spring-mvc.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/hellossm/WEB-INF/classes/cn/ppdxzz/controller/AdminController.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/hellossm/WEB-INF/classes/cn/ppdxzz/controller/AdminController.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/hellossm/WEB-INF/classes/cn/ppdxzz/dao/AdminDao.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/hellossm/WEB-INF/classes/cn/ppdxzz/dao/AdminDao.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/hellossm/WEB-INF/classes/cn/ppdxzz/service/AdminService.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/hellossm/WEB-INF/classes/cn/ppdxzz/service/AdminService.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/hellossm/WEB-INF/classes/cn/ppdxzz/service/impl/AdminServiceImpl.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/hellossm/WEB-INF/classes/cn/ppdxzz/service/impl/AdminServiceImpl.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/hellossm/WEB-INF/classes/spring-mvc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/target/hellossm/WEB-INF/classes/spring-mvc.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/hellossm/WEB-INF/jsp/admin-edit.jsp" beforeDir="false" afterPath="$PROJECT_DIR$/target/hellossm/WEB-INF/jsp/admin-edit.jsp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/hellossm/WEB-INF/jsp/admin-list.jsp" beforeDir="false" afterPath="$PROJECT_DIR$/target/hellossm/WEB-INF/jsp/admin-list.jsp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/hellossm/WEB-INF/jsp/login.jsp" beforeDir="false" afterPath="$PROJECT_DIR$/target/hellossm/WEB-INF/jsp/login.jsp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/hellossm/WEB-INF/jsp/main.jsp" beforeDir="false" afterPath="$PROJECT_DIR$/target/hellossm/WEB-INF/jsp/main.jsp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/hellossm/WEB-INF/web.xml" beforeDir="false" afterPath="$PROJECT_DIR$/target/hellossm/WEB-INF/web.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/hellossm/js/excel.min.js" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/target/hellossm/admin-edit.html" beforeDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -121,6 +110,19 @@
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="WriteExcel" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="cn.ppdxzz.poi.WriteExcel" />
<module name="hellossm" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="cn.ppdxzz.poi.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Tomcat 9.0.24" type="#com.intellij.j2ee.web.tomcat.TomcatRunConfigurationFactory" factoryName="Local" APPLICATION_SERVER_NAME="Tomcat 9.0.24" ALTERNATIVE_JRE_ENABLED="false" nameIsGenerated="true">
<option name="BROWSER_ID" value="98ca6316-2f89-46d9-a9e5-fa9e2b0625b3" />
<option name="UPDATE_ON_FRAME_DEACTIVATION" value="true" />
@ -198,9 +200,11 @@
<list>
<item itemvalue="Application.MD5Util" />
<item itemvalue="Tomcat Server.Tomcat 9.0.24" />
<item itemvalue="Application.WriteExcel" />
</list>
<recent_temporary>
<list>
<item itemvalue="Application.WriteExcel" />
<item itemvalue="Application.MD5Util" />
</list>
</recent_temporary>
@ -254,6 +258,8 @@
<workItem from="1581649259154" duration="10514000" />
<workItem from="1581662911720" duration="23085000" />
<workItem from="1581731575582" duration="47721000" />
<workItem from="1581814839916" duration="4082000" />
<workItem from="1581820499500" duration="20191000" />
</task>
<servers />
</component>
@ -273,58 +279,58 @@
<screen x="0" y="0" width="1366" height="728" />
</state>
<state x="137" y="23" key="#com.intellij.execution.impl.EditConfigurationsDialog/0.0.1366.728@0.0.1366.728" timestamp="1581485477637" />
<state x="488" y="84" key="#com.intellij.ide.util.MemberChooser" timestamp="1581771525224">
<state x="488" y="84" key="#com.intellij.ide.util.MemberChooser" timestamp="1581836246308">
<screen x="0" y="0" width="1366" height="728" />
</state>
<state x="488" y="84" key="#com.intellij.ide.util.MemberChooser/0.0.1366.728@0.0.1366.728" timestamp="1581771525224" />
<state width="1059" height="2" key="GridCell.Tab.0.bottom" timestamp="1581782641454">
<state x="488" y="84" key="#com.intellij.ide.util.MemberChooser/0.0.1366.728@0.0.1366.728" timestamp="1581836246308" />
<state width="1059" height="127" key="GridCell.Tab.0.bottom" timestamp="1581855818900">
<screen x="0" y="0" width="1366" height="728" />
</state>
<state width="1059" height="2" key="GridCell.Tab.0.bottom/0.0.1366.728@0.0.1366.728" timestamp="1581782641454" />
<state width="1059" height="2" key="GridCell.Tab.0.center" timestamp="1581782641454">
<state width="1059" height="127" key="GridCell.Tab.0.bottom/0.0.1366.728@0.0.1366.728" timestamp="1581855818900" />
<state width="1059" height="127" key="GridCell.Tab.0.center" timestamp="1581855818900">
<screen x="0" y="0" width="1366" height="728" />
</state>
<state width="1059" height="2" key="GridCell.Tab.0.center/0.0.1366.728@0.0.1366.728" timestamp="1581782641454" />
<state width="1059" height="2" key="GridCell.Tab.0.left" timestamp="1581782641454">
<state width="1059" height="127" key="GridCell.Tab.0.center/0.0.1366.728@0.0.1366.728" timestamp="1581855818900" />
<state width="1059" height="127" key="GridCell.Tab.0.left" timestamp="1581855818900">
<screen x="0" y="0" width="1366" height="728" />
</state>
<state width="1059" height="2" key="GridCell.Tab.0.left/0.0.1366.728@0.0.1366.728" timestamp="1581782641454" />
<state width="1059" height="2" key="GridCell.Tab.0.right" timestamp="1581782641454">
<state width="1059" height="127" key="GridCell.Tab.0.left/0.0.1366.728@0.0.1366.728" timestamp="1581855818900" />
<state width="1059" height="127" key="GridCell.Tab.0.right" timestamp="1581855818900">
<screen x="0" y="0" width="1366" height="728" />
</state>
<state width="1059" height="2" key="GridCell.Tab.0.right/0.0.1366.728@0.0.1366.728" timestamp="1581782641454" />
<state width="1059" height="2" key="GridCell.Tab.1.bottom" timestamp="1581782641454">
<state width="1059" height="127" key="GridCell.Tab.0.right/0.0.1366.728@0.0.1366.728" timestamp="1581855818900" />
<state width="1059" height="127" key="GridCell.Tab.1.bottom" timestamp="1581855818900">
<screen x="0" y="0" width="1366" height="728" />
</state>
<state width="1059" height="2" key="GridCell.Tab.1.bottom/0.0.1366.728@0.0.1366.728" timestamp="1581782641454" />
<state width="1059" height="2" key="GridCell.Tab.1.center" timestamp="1581782641454">
<state width="1059" height="127" key="GridCell.Tab.1.bottom/0.0.1366.728@0.0.1366.728" timestamp="1581855818900" />
<state width="1059" height="127" key="GridCell.Tab.1.center" timestamp="1581855818900">
<screen x="0" y="0" width="1366" height="728" />
</state>
<state width="1059" height="2" key="GridCell.Tab.1.center/0.0.1366.728@0.0.1366.728" timestamp="1581782641454" />
<state width="1059" height="2" key="GridCell.Tab.1.left" timestamp="1581782641454">
<state width="1059" height="127" key="GridCell.Tab.1.center/0.0.1366.728@0.0.1366.728" timestamp="1581855818900" />
<state width="1059" height="127" key="GridCell.Tab.1.left" timestamp="1581855818900">
<screen x="0" y="0" width="1366" height="728" />
</state>
<state width="1059" height="2" key="GridCell.Tab.1.left/0.0.1366.728@0.0.1366.728" timestamp="1581782641454" />
<state width="1059" height="2" key="GridCell.Tab.1.right" timestamp="1581782641454">
<state width="1059" height="127" key="GridCell.Tab.1.left/0.0.1366.728@0.0.1366.728" timestamp="1581855818900" />
<state width="1059" height="127" key="GridCell.Tab.1.right" timestamp="1581855818900">
<screen x="0" y="0" width="1366" height="728" />
</state>
<state width="1059" height="2" key="GridCell.Tab.1.right/0.0.1366.728@0.0.1366.728" timestamp="1581782641454" />
<state width="1059" height="2" key="GridCell.Tab.2.bottom" timestamp="1581782641454">
<state width="1059" height="127" key="GridCell.Tab.1.right/0.0.1366.728@0.0.1366.728" timestamp="1581855818900" />
<state width="1059" height="127" key="GridCell.Tab.2.bottom" timestamp="1581855818901">
<screen x="0" y="0" width="1366" height="728" />
</state>
<state width="1059" height="2" key="GridCell.Tab.2.bottom/0.0.1366.728@0.0.1366.728" timestamp="1581782641454" />
<state width="1059" height="2" key="GridCell.Tab.2.center" timestamp="1581782641454">
<state width="1059" height="127" key="GridCell.Tab.2.bottom/0.0.1366.728@0.0.1366.728" timestamp="1581855818901" />
<state width="1059" height="127" key="GridCell.Tab.2.center" timestamp="1581855818901">
<screen x="0" y="0" width="1366" height="728" />
</state>
<state width="1059" height="2" key="GridCell.Tab.2.center/0.0.1366.728@0.0.1366.728" timestamp="1581782641454" />
<state width="1059" height="2" key="GridCell.Tab.2.left" timestamp="1581782641454">
<state width="1059" height="127" key="GridCell.Tab.2.center/0.0.1366.728@0.0.1366.728" timestamp="1581855818901" />
<state width="1059" height="127" key="GridCell.Tab.2.left" timestamp="1581855818901">
<screen x="0" y="0" width="1366" height="728" />
</state>
<state width="1059" height="2" key="GridCell.Tab.2.left/0.0.1366.728@0.0.1366.728" timestamp="1581782641454" />
<state width="1059" height="2" key="GridCell.Tab.2.right" timestamp="1581782641454">
<state width="1059" height="127" key="GridCell.Tab.2.left/0.0.1366.728@0.0.1366.728" timestamp="1581855818901" />
<state width="1059" height="127" key="GridCell.Tab.2.right" timestamp="1581855818901">
<screen x="0" y="0" width="1366" height="728" />
</state>
<state width="1059" height="2" key="GridCell.Tab.2.right/0.0.1366.728@0.0.1366.728" timestamp="1581782641454" />
<state width="1059" height="127" key="GridCell.Tab.2.right/0.0.1366.728@0.0.1366.728" timestamp="1581855818901" />
<state width="1059" height="2" key="GridCell.Tab.3.bottom" timestamp="1581775851407">
<screen x="0" y="0" width="1366" height="728" />
</state>

@ -164,6 +164,21 @@
<artifactId>jackson-annotations</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.29</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
<build>

@ -4,6 +4,7 @@ import cn.ppdxzz.domain.Admin;
import cn.ppdxzz.service.AdminService;
import cn.ppdxzz.utils.MD5Util;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
@ -13,10 +14,12 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;
@ -222,18 +225,19 @@ public class AdminController {
writer.write("true");
}
@ResponseBody
@RequestMapping(value = "/exportAdminInfo")
public List<Admin> export() throws Exception {
List<Admin> admins = adminService.exportAdminInfo();
return admins;
}
/**
*
* @param response
* @throws Exception
*/
@RequestMapping("/exportAdminInfo")
public void exportAdminInfo(HttpServletResponse response) throws Exception {
InputStream is = adminService.getInputStream();
response.setContentType("application/vnd.ms-excel");
response.setHeader("contentDisposition", "attachment;filename=adminsInfo.xls");
ServletOutputStream outputStream = response.getOutputStream();
IOUtils.copy(is,outputStream);
}
}

@ -0,0 +1,189 @@
package cn.ppdxzz.poi;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* Description:Excel
*
* @Date: 2020/2/16 15:17
* @Author: PeiChen
*/
public class WriteExcel {
//导出表的列名
private String[] rowName;
//每行作为一个Object对象
private List<Object[]> dataList = new ArrayList<Object[]>();
//构造方法,传入要导出的数据:第一个参数传入一个列名数组第二个参数传入一个list集合Object对象数组
public WriteExcel(String[] rowName,List<Object[]> dataList){
this.dataList = dataList;
this.rowName = rowName;
}
/*
*
* */
public InputStream export() throws Exception{
HSSFWorkbook workbook = new HSSFWorkbook(); // 创建工作簿对象
HSSFSheet sheet = workbook.createSheet("sheet1"); // 创建工作表
// 设置sheet表样式
HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//获取列头样式对象
HSSFCellStyle style = this.getColumnStyle(workbook); //单元格样式对象
// 定义所需列数
int columnNum = rowName.length;
HSSFRow rowRowName = sheet.createRow(0); // 在索引2的位置创建行(最顶端的行开始的第二行)
// 将列头设置到sheet的单元格中
for(int n=0;n<columnNum;n++){
HSSFCell cellRowName = rowRowName.createCell(n); //创建列头对应个数的单元格
cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); //设置列头单元格的数据类型
HSSFRichTextString text = new HSSFRichTextString(rowName[n]);
cellRowName.setCellValue(text); //设置列头单元格的值
cellRowName.setCellStyle(columnTopStyle); //设置列头单元格样式
}
//将查询出的数据设置到sheet对应的单元格中
for(int i=0;i<dataList.size();i++){
Object[] obj = dataList.get(i); //遍历每个对象
HSSFRow row = sheet.createRow(i+1); //创建所需的行数
for(int j=0; j<obj.length; j++){
HSSFCell cell = null; //设置单元格的数据类型
cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING);
if(!"".equals(obj[j]) && obj[j] != null){
cell.setCellValue(obj[j].toString()); //设置单元格的值
}
cell.setCellStyle(style); //设置单元格样式
}
}
//让列宽随着导出的列长自动适应
for (int colNum = 0; colNum < columnNum; colNum++) {
int columnWidth = sheet.getColumnWidth(colNum) / 256;
for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
HSSFRow currentRow;
//如若当前行未被使用过,则新建一行
if (sheet.getRow(rowNum) == null) {
currentRow = sheet.createRow(rowNum);
} else {
currentRow = sheet.getRow(rowNum);
}
if (currentRow.getCell(colNum) != null) {
HSSFCell currentCell = currentRow.getCell(colNum);
if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
int length = currentCell.getStringCellValue().getBytes().length;
if (columnWidth < length) {
columnWidth = length;
}
}
}
}
if(colNum == 0){
sheet.setColumnWidth(colNum, (columnWidth-2) * 256);
}else{
sheet.setColumnWidth(colNum, (columnWidth+4) * 256);
}
}
String fileName = "Excel-" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";
String headStr = "attachment; filename=\"" + fileName + "\"";
ByteArrayOutputStream os=new ByteArrayOutputStream();
try {
workbook.write(os);
} catch (IOException e) {
e.printStackTrace();
}
byte[] content=os.toByteArray();
InputStream is=new ByteArrayInputStream(content);
return is;
}
/*
*
*/
public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {
// 设置字体
HSSFFont font = workbook.createFont();
//设置字体大小
font.setFontHeightInPoints((short)11);
//字体加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//设置字体名字
font.setFontName("Courier New");
//设置样式;
HSSFCellStyle style = workbook.createCellStyle();
//设置底边框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//设置底边框颜色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//设置左边框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//设置左边框颜色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//设置右边框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//设置右边框颜色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//设置顶边框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置顶边框颜色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在样式用应用设置的字体;
style.setFont(font);
//设置自动换行;
style.setWrapText(false);
//设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return style;
}
/*
*
*/
public HSSFCellStyle getColumnStyle(HSSFWorkbook workbook) {
// 设置字体
HSSFFont font = workbook.createFont();
//设置字体名字
font.setFontName("Courier New");
//设置样式;
HSSFCellStyle style = workbook.createCellStyle();
//设置底边框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//设置底边框颜色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//设置左边框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//设置左边框颜色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//设置右边框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//设置右边框颜色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//设置顶边框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置顶边框颜色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在样式用应用设置的字体;
style.setFont(font);
//设置自动换行;
style.setWrapText(false);
//设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return style;
}
}

@ -2,6 +2,7 @@ package cn.ppdxzz.service;
import cn.ppdxzz.domain.Admin;
import java.io.InputStream;
import java.util.List;
/**
@ -33,10 +34,7 @@ public interface AdminService {
//授权
void put_power(Admin admin) throws Exception;
/**
*
* @return
* @throws Exception
*/
List<Admin> exportAdminInfo() throws Exception;
//返回一个携带所有管理员信息数据的InputStream输入流
InputStream getInputStream() throws Exception;
}

@ -2,12 +2,19 @@ package cn.ppdxzz.service.impl;
import cn.ppdxzz.dao.AdminDao;
import cn.ppdxzz.domain.Admin;
import cn.ppdxzz.poi.WriteExcel;
import cn.ppdxzz.service.AdminService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
@ -117,8 +124,33 @@ public class AdminServiceImpl implements AdminService {
adminDao.put_power(admin);
}
/**
*
* @return
* @throws Exception
*/
@Override
public List<Admin> exportAdminInfo() throws Exception {
return adminDao.exportAdminInfo();
public InputStream getInputStream() throws Exception {
//Excel中的每列列名依次对应数据库的字段
String[] title = new String[]{"ID","用户名","密码","姓名","手机号","权限","描述"};
List<Admin> admins = adminDao.exportAdminInfo();
List<Object[]> dataList = new ArrayList<Object[]>();
for (int i = 0; i < admins.size(); i++) {
Object[] obj = new Object[7];
obj[0] = admins.get(i).getId();
obj[1] = admins.get(i).getUsername();
//可在此处设置密码是否MD5解密成普通可见密码
obj[2] = admins.get(i).getPassword();
obj[3] = admins.get(i).getName();
obj[4] = admins.get(i).getPhone();
obj[5] = admins.get(i).getPower();
obj[6] = admins.get(i).getDescription();
dataList.add(obj);
}
WriteExcel ex = new WriteExcel(title, dataList);
InputStream in;
in = ex.export();
return in;
}
}

@ -18,17 +18,34 @@
<context:component-scan base-package="cn.ppdxzz">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--开启SpringMVC的注解支持-->
<!-- 处理请求时返回json字符串的中文乱码问题 -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--配置视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!--过滤静态资源-->
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/images/" mapping="/images/**" />
@ -37,7 +54,7 @@
<mvc:resources location="/fonts/" mapping="/fonts/**" />
<mvc:resources location="/layui_exts/" mapping="/layui_exts/**" />
<mvc:resources location="/layer/" mapping="/layer/**" />
<!--开启SpringMVC的注解支持-->
<!--
支持AOP的注解支持AOP底层使用代理技术
JDK动态代理要求必须有接口

@ -21,7 +21,6 @@
<script type="text/javascript" src="${pageContext.request.contextPath}/js/xadmin.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/bootstrap.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.1.1.js"></script>
<script type="text/javascript" charset="UTF-8" src="${pageContext.request.contextPath}/layui_exts/excel.js"></script>
<!--[if lt IE 9]>
<script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
<script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
@ -73,7 +72,7 @@
</div>
<xblock>
<a href="${pageContext.request.contextPath}/adminAdd" class="layui-btn layui-btn-normal"><i class="layui-icon">&#xe654;</i>添加</a>
<button id="fileDownLoad" class="layui-btn layui-btn-warm" lay-filter="toolbarDemo" lay-submit=""><i class="layui-icon">&#xe67c;</i>导出</button>
<a onclick="exportInfo()" class="layui-btn layui-btn-warm" href="javascript:;"><i class="layui-icon">&#xe67c;</i>导出</a>
<span class="x-right" style="line-height:40px">共有数据:${pageInfo.total} 条</span>
</xblock>
<div class="layui-card-body">
@ -184,85 +183,7 @@
</div>
<script>
layui.config({
base: 'layui_exts/',
}).extend({
excel: 'excel',
});
layui.use(['jquery', 'excel', 'form','layer','laydate'], function(){
var form = layui.form,
$ = layui.jquery,
laydate = layui.laydate;
var excel = parent.layui.excel;
//执行一个laydate实例
laydate.render({
elem: '#start' //指定元素
});
form.render();
form.on('submit(toolbarDemo)', function(){
$.ajax({
url: '${pageContext.request.contextPath}/exportAdminInfo',
type: 'post',
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (data) {
// 假如返回的 data 是需要导出的列表数据
console.log(data);
// 1. 如果需要调整顺序,请执行梳理函数
var dt = excel.filterExportData(data, [
'id'
,'username'
,'password'
,'name'
,'phone'
,'power'
,'description'
]);
// 2. 数组头部新增表头
dt.unshift({id: 'ID', username: '用户名', password: '密码', name: '姓名', phone: '手机号', power: '权限', description: '权限描述'});
// 3.自定义导出Excel各字段的宽度
var colConf = excel.makeColConfig({
'A': 40,
'B': 50,
'C': 120,
'D': 80,
'F': 120
}, 60);
var time_start = Date.now();
// 3. 执行导出函数,系统会弹出弹框
excel.exportExcel({
sheet1: dt
}, '管理员导出数据.xlsx', 'xlsx', {
extend: {
'!cols': colConf
}
});
var time_end = Date.now();
var spent_time = (time_end - time_start) / 1000;
layer.msg('导出数据共耗时'+spent_time+'s');
//setTimeout(function () {window.location.href='/findAdmin';},2000);
},
error: function () {
//console.log(data);
setTimeout(function () {window.location.href='${pageContext.request.contextPath}/findAllAdmin';},2000);
}
});
});
});
</script>
<script>
//删除操作
function member_del(obj,id,power,del_power){
layer.confirm('确认要删除吗?',function(index){
if (power == 0 || power < del_power){
@ -282,6 +203,7 @@
});
});
}
//授权操作
function put_power(obj,id,power) {
//验证是否拥有权限
if (power == 0) {
@ -319,8 +241,12 @@
layer.close(index);
});
});
}
//导出Excel操作
function exportInfo() {
layer.confirm('确定导出管理员数据吗?',function (index) {
location.href="${pageContext.request.contextPath}/exportAdminInfo";
});
}
</script>
</body>

@ -1,10 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
你好啊,哈哈哈
</body>
</html>

@ -18,17 +18,34 @@
<context:component-scan base-package="cn.ppdxzz">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--开启SpringMVC的注解支持-->
<!-- 处理请求时返回json字符串的中文乱码问题 -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--配置视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!--过滤静态资源-->
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/images/" mapping="/images/**" />
@ -37,7 +54,7 @@
<mvc:resources location="/fonts/" mapping="/fonts/**" />
<mvc:resources location="/layui_exts/" mapping="/layui_exts/**" />
<mvc:resources location="/layer/" mapping="/layer/**" />
<!--开启SpringMVC的注解支持-->
<!--
支持AOP的注解支持AOP底层使用代理技术
JDK动态代理要求必须有接口

@ -18,17 +18,34 @@
<context:component-scan base-package="cn.ppdxzz">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--开启SpringMVC的注解支持-->
<!-- 处理请求时返回json字符串的中文乱码问题 -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--配置视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!--过滤静态资源-->
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/images/" mapping="/images/**" />
@ -37,7 +54,7 @@
<mvc:resources location="/fonts/" mapping="/fonts/**" />
<mvc:resources location="/layui_exts/" mapping="/layui_exts/**" />
<mvc:resources location="/layer/" mapping="/layer/**" />
<!--开启SpringMVC的注解支持-->
<!--
支持AOP的注解支持AOP底层使用代理技术
JDK动态代理要求必须有接口

@ -17,11 +17,10 @@
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/xadmin.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.1.1.js"></script>
<%--<script src="${pageContext.request.contextPath}/lib/layui/layui.js" charset="utf-8"></script>--%>
<script src="${pageContext.request.contextPath}/lib/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/xadmin.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/bootstrap.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.1.1.js"></script>
<script type="text/javascript" charset="UTF-8" src="${pageContext.request.contextPath}/layui_exts/excel.js"></script>
<!--[if lt IE 9]>
<script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
<script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
@ -73,7 +72,7 @@
</div>
<xblock>
<a href="${pageContext.request.contextPath}/adminAdd" class="layui-btn layui-btn-normal"><i class="layui-icon">&#xe654;</i>添加</a>
<button id="fileDownLoad" class="layui-btn layui-btn-warm" lay-filter="toolbarDemo" lay-submit=""><i class="layui-icon">&#xe67c;</i>导出</button>
<a onclick="exportInfo()" class="layui-btn layui-btn-warm" href="javascript:;"><i class="layui-icon">&#xe67c;</i>导出</a>
<span class="x-right" style="line-height:40px">共有数据:${pageInfo.total} 条</span>
</xblock>
<div class="layui-card-body">
@ -184,85 +183,7 @@
</div>
<script>
layui.config({
base: 'layui_exts/',
}).extend({
excel: 'excel',
});
layui.use(['jquery', 'excel', 'form','layer','laydate'], function(){
var form = layui.form,
$ = layui.jquery,
laydate = layui.laydate;
var excel = parent.layui.excel;
//执行一个laydate实例
laydate.render({
elem: '#start' //指定元素
});
form.render();
form.on('submit(toolbarDemo)', function(){
$.ajax({
url: '${pageContext.request.contextPath}/exportAdminInfo',
type: 'post',
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (data) {
// 假如返回的 data 是需要导出的列表数据
console.log(data);
// 1. 如果需要调整顺序,请执行梳理函数
var dt = excel.filterExportData(data, [
'id'
,'username'
,'password'
,'name'
,'phone'
,'power'
,'description'
]);
// 2. 数组头部新增表头
dt.unshift({id: 'ID', username: '用户名', password: '密码', name: '姓名', phone: '手机号', power: '权限', description: '权限描述'});
// 3.自定义导出Excel各字段的宽度
var colConf = excel.makeColConfig({
'A': 40,
'B': 50,
'C': 120,
'D': 80,
'F': 120
}, 60);
var time_start = Date.now();
// 3. 执行导出函数,系统会弹出弹框
excel.exportExcel({
sheet1: dt
}, '管理员导出数据.xlsx', 'xlsx', {
extend: {
'!cols': colConf
}
});
var time_end = Date.now();
var spent_time = (time_end - time_start) / 1000;
layer.msg('导出数据共耗时'+spent_time+'s');
//setTimeout(function () {window.location.href='/findAdmin';},2000);
},
error: function () {
//console.log(data);
setTimeout(function () {window.location.href='${pageContext.request.contextPath}/findAllAdmin';},2000);
}
});
});
});
</script>
<script>
//删除操作
function member_del(obj,id,power,del_power){
layer.confirm('确认要删除吗?',function(index){
if (power == 0 || power < del_power){
@ -282,6 +203,7 @@
});
});
}
//授权操作
function put_power(obj,id,power) {
//验证是否拥有权限
if (power == 0) {
@ -319,8 +241,12 @@
layer.close(index);
});
});
}
//导出Excel操作
function exportInfo() {
layer.confirm('确定导出管理员数据吗?',function (index) {
location.href="${pageContext.request.contextPath}/exportAdminInfo";
});
}
</script>
</body>

@ -1,10 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
你好啊,哈哈哈
</body>
</html>

@ -5,6 +5,7 @@
3. 2020.2.13用bootstrap框架完成了管理员添加和修改的页面实现并加入jQuery校验字段是否为空最终实现了管理员添加以及修改功能。
4. 2020.2.14把原来的登录页面更换成动态特效的登录页面并使用ajax完成登录的后台校验管理员查询功能完成了模糊查询但是模糊查询带上分页技术后页面跳转出现问题点击分页按钮后又重新查询了全部数据的分页查看了相关资料进行改进目前并未完全实现。
5. 2020.2.15实现了管理员信息模糊查询并分页的效果并增加了超级管理员可给普通管理员授权的功能着手实现数据导出功能发现程序运行时报错经验证后发现是json格式从后台传向前台需要经过解析故导入了相关依赖即可而后又遇到layui的Excel模块不可用开始着手解决。
6. 2020.2.16实现了管理员数据Excel格式的导出并可以指定保存位置。

Loading…
Cancel
Save