package com.poi.testpoi.controller;
import com.poi.testpoi.pojo.User;
import com.poi.testpoi.service.UserService;
import com.poi.testpoi.util.ExcelImportUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
@Controller
public class IndexController {
@Autowired
private UserService userService;
@RequestMapping("/index")
public String showUser(Model model) {
List<User> users = userService.selectUsers();
model.addAttribute("user", users);
return "index";
}
@RequestMapping(value = "/export")
@ResponseBody
public void export(HttpServletResponse response) throws IOException {
List<User> users = userService.selectUsers();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("获取excel测试表格");
HSSFRow row = null;
row = sheet.createRow(0);//创建第一个单元格
row.setHeight((short) (26.25 * 20));
row.createCell(0).setCellValue("用户信息列表");//为第一行单元格设值
/*为标题设计空间
* firstRow从第1行开始
* lastRow从第0行结束
*
*从第1个单元格开始
* 从第3个单元格结束
*/
CellRangeAddress rowRegion = new CellRangeAddress(0, 0, 0, 2);
sheet.addMergedRegion(rowRegion);
/*CellRangeAddress columnRegion = new CellRangeAddress(1,4,0,0);
sheet.addMergedRegion(columnRegion);*/
/*
* 动态获取数据库列 sql语句 select COLUMN_NAME from INFORMATION_SCHEMA.Columns where table_name='user' and table_schema='test'
* 第一个table_name 表名字
* 第二个table_name 数据库名称
* */
row = sheet.createRow(1);
row.setHeight((short) (22.50 * 20));//设置行高
row.createCell(0).setCellValue("用户Id");//为第一个单元格设值
row.createCell(1).setCellValue("用户名");//为第二个单元格设值
row.createCell(2).setCellValue("用户密码");//为第三个单元格设值
for (int i = 0; i < users.size(); i++) {
row = sheet.createRow(i + 2);
User user = users.get(i);
row.createCell(0).setCellValue(user.getUid());
row.createCell(1).setCellValue(user.getUsername());
row.createCell(2).setCellValue(user.getPassword());
}
sheet.setDefaultRowHeight((short) (16.5 * 20));
//列宽自适应
for (int i = 0; i <= 13; i++) {
sheet.autoSizeColumn(i);
}
response.setContentType("application/vnd.ms-excel;charset=utf-8");
OutputStream os = response.getOutputStream();
response.setHeader("Content-disposition", "attachment;filename=user.xls");//默认Excel名称
wb.write(os);
os.flush();
os.close();
}
@RequestMapping(value = "/import")
public String exImport(@RequestParam(value = "filename")MultipartFile file, HttpSession session) {
boolean a = false;
String fileName = file.getOriginalFilename();
try {
a = userService.batchImport(fileName, file);
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:index";
}
/**
* 获取样式
*
* @param hssfWorkbook
* @param styleNum
* @return
*/
public HSSFCellStyle getStyle(HSSFWorkbook hssfWorkbook, Integer styleNum) {
HSSFCellStyle style = hssfWorkbook.createCellStyle();
style.setBorderRight(BorderStyle.THIN);//右边框
style.setBorderBottom(BorderStyle.THIN);//下边框
HSSFFont font = hssfWorkbook.createFont();
font.setFontName("微软雅黑");//设置字体为微软雅黑
HSSFPalette palette = hssfWorkbook.getCustomPalette();//拿到palette颜色板,可以根据需要设置颜色
switch (styleNum) {
case (0): {//HorizontalAlignment
style.setAlignment(HorizontalAlignment.CENTER_SELECTION);//跨列居中
font.setBold(true);//粗体
font.setFontHeightInPoints((short) 14);//字体大小
style.setFont(font);
palette.setColorAtIndex(HSSFColor.BLUE.index, (byte) 184, (byte) 204, (byte) 228);//替换颜色板中的颜色
style.setFillForegroundColor(HSSFColor.BLUE.index);
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
}
break;
case (1): {
font.setBold(true);//粗体
font.setFontHeightInPoints((short) 11);//字体大小
style.setFont(font);
}
break;
case (2): {
font.setFontHeightInPoints((short) 10);
style.setFont(font);
}
break;
case (3): {
style.setFont(font);
palette.setColorAtIndex(HSSFColor.GREEN.index, (byte) 0, (byte) 32, (byte) 96);//替换颜色板中的颜色
style.setFillForegroundColor(HSSFColor.GREEN.index);
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
}
break;
}
return style;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
springboot+mybatis+poi 实现excel导入导出
共142个文件
xml:89个
java:9个
class:9个
需积分: 38 73 下载量 123 浏览量
2019-09-04
18:24:00
上传
评论 5
收藏 1.21MB ZIP 举报
温馨提示
springboot+mybatis+poi 实现excel导入导出操作
资源推荐
资源详情
资源评论
收起资源包目录
springboot+mybatis+poi 实现excel导入导出 (142个子文件)
IndexController.class 7KB
UserServiceImpl.class 5KB
MyException.class 2KB
TestpoiApplicationTests.class 2KB
User.class 1KB
TestpoiApplication.class 830B
ExcelImportUtils.class 810B
UserService.class 435B
UserMapper.class 432B
mvnw.cmd 5KB
bootstrap.css 143KB
bootstrap.min.css 118KB
bootstrap.min.css 118KB
bootstrap-theme.css 26KB
bootstrap-theme.min.css 23KB
bootstrap-table.min.css 6KB
glyphicons-halflings-regular.eot 20KB
.gitignore 268B
testpoi.iml 10KB
maven-wrapper.jar 46KB
IndexController.java 5KB
UserServiceImpl.java 3KB
MyException.java 3KB
TestpoiApplicationTests.java 1KB
ExcelImportUtils.java 758B
User.java 532B
TestpoiApplication.java 406B
UserService.java 302B
UserMapper.java 274B
jquery-1.8.2.js 261KB
jquery-1.8.2.min.js 92KB
jquery.min.js 82KB
bootstrap.js 68KB
bootstrap-table.min.js 58KB
bootstrap.min.js 36KB
bootstrap-table-zh-CN.min.js 951B
npm.js 484B
index.jsp 1KB
common.jsp 801B
bootstrap.min.css.map 529KB
bootstrap.css.map 380KB
bootstrap-theme.css.map 47KB
bootstrap-theme.min.css.map 25KB
mvnw 6KB
application.properties 413B
application.properties 395B
maven-wrapper.properties 110B
testpoi.rar 591KB
user.sql 2KB
glyphicons-halflings-regular.svg 106KB
glyphicons-halflings-regular.ttf 44KB
glyphicons-halflings-regular.woff 23KB
glyphicons-halflings-regular.woff2 18KB
workspace.xml 64KB
uiDesigner.xml 9KB
pom.xml 4KB
UserMapper.xml 1KB
UserMapper.xml 1KB
Maven__org_springframework_boot_spring_boot_test_autoconfigure_2_1_3_RELEASE.xml 793B
compiler.xml 792B
Maven__org_springframework_boot_spring_boot_starter_logging_2_1_3_RELEASE.xml 772B
Maven__org_springframework_boot_spring_boot_starter_tomcat_2_1_3_RELEASE.xml 765B
Maven__org_springframework_boot_spring_boot_autoconfigure_2_1_3_RELEASE.xml 758B
Maven__org_mybatis_spring_boot_mybatis_spring_boot_autoconfigure_1_3_2.xml 754B
Maven__com_fasterxml_jackson_module_jackson_module_parameter_names_2_9_8.xml 753B
Maven__org_springframework_boot_spring_boot_starter_json_2_1_3_RELEASE.xml 751B
Maven__org_springframework_boot_spring_boot_starter_jdbc_2_1_3_RELEASE.xml 751B
Maven__org_springframework_boot_spring_boot_starter_test_2_1_3_RELEASE.xml 751B
Maven__org_springframework_boot_spring_boot_starter_web_2_1_3_RELEASE.xml 744B
Maven__com_vaadin_external_google_android_json_0_0_20131108_vaadin1.xml 724B
Maven__org_springframework_boot_spring_boot_devtools_2_1_3_RELEASE.xml 723B
Maven__org_springframework_boot_spring_boot_starter_2_1_3_RELEASE.xml 716B
Maven__org_mybatis_spring_boot_mybatis_spring_boot_starter_1_3_2.xml 712B
Maven__com_fasterxml_jackson_datatype_jackson_datatype_jsr310_2_9_8.xml 712B
Maven__org_hibernate_validator_hibernate_validator_6_0_14_Final.xml 705B
Maven__com_fasterxml_jackson_datatype_jackson_datatype_jdk8_2_9_8.xml 698B
Maven__org_springframework_boot_spring_boot_test_2_1_3_RELEASE.xml 695B
Maven__org_apache_tomcat_embed_tomcat_embed_websocket_9_0_16.xml 684B
Maven__org_springframework_spring_expression_5_1_5_RELEASE.xml 682B
Maven__com_fasterxml_jackson_core_jackson_annotations_2_9_0.xml 668B
Maven__org_apache_tomcat_embed_tomcat_embed_jasper_9_0_16.xml 663B
Maven__org_springframework_spring_context_5_1_5_RELEASE.xml 661B
Maven__org_springframework_boot_spring_boot_2_1_3_RELEASE.xml 660B
Maven__org_springframework_spring_webmvc_5_1_5_RELEASE.xml 654B
Maven__org_apache_tomcat_embed_tomcat_embed_core_9_0_16.xml 649B
Maven__org_springframework_spring_beans_5_1_5_RELEASE.xml 647B
Maven__com_fasterxml_jackson_core_jackson_databind_2_9_8.xml 647B
Maven__org_springframework_spring_test_5_1_5_RELEASE.xml 640B
Maven__org_springframework_spring_core_5_1_5_RELEASE.xml 640B
Maven__org_springframework_spring_jdbc_5_1_5_RELEASE.xml 640B
Maven__javax_validation_validation_api_2_0_1_Final.xml 635B
Maven__org_apache_tomcat_embed_tomcat_embed_el_9_0_16.xml 635B
Maven__javax_annotation_javax_annotation_api_1_3_2.xml 635B
Maven__org_springframework_spring_jcl_5_1_5_RELEASE.xml 633B
Maven__org_springframework_spring_aop_5_1_5_RELEASE.xml 633B
Maven__org_springframework_spring_web_5_1_5_RELEASE.xml 633B
Maven__org_apache_logging_log4j_log4j_to_slf4j_2_11_2.xml 632B
Maven__org_jboss_logging_jboss_logging_3_3_2_Final.xml 632B
Maven__commons_fileupload_commons_fileupload_1_3_1.xml 629B
Maven__org_apache_commons_commons_collections4_4_1.xml 629B
共 142 条
- 1
- 2
资源评论
zwz___
- 粉丝: 5
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功