Maven整合SSM 框架,POI插件导出EXCEL文件
2019-03-20 22:16
302 查看
版权声明:转载请注明出处 https://blog.csdn.net/qq_28869233/article/details/88701784
正片 片尾 花絮 鸣谢
Maven+SSM+poi导出Excel文件
片头
背景描述
最近被提出需求,要求网站实现对数据库的数据进行查询,并要求可实现导出这些。没办法,有新要求只能乖乖去实现呗。于是上网搜解决办法,经过一系列操作,最终算是实现了需求。
环境
Maven整合SSM框架+poi
正片
1part
maven配置文件pom.xml依赖上
<!--excel解析poi包 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency>
2part
web前端可设置直接打开新窗口,访问网址即可
window.open(url + export/exportExcel?ExcelName=“马品种常量表”)
3part(主要)
controller层
@Controller @RequestMapping("/export") public class ExportController{ @Autowired private ExportService exportService; @RequestMapping(value = "/exportExcel", produces = "application/json; charset=utf-8") @ResponseBody public Map<String, Object> exportExcel(String ExcelName,HttpServletResponse response) throws IOException { //文件名用 String fileName = ""; String fileNameURL = ""; // 创建excel工作薄 HSSFWorkbook workbook = new HSSFWorkbook(); // 创建一个工作表sheet HSSFSheet sheet = workbook.createSheet(); // 创建第一行 HSSFRow row = sheet.createRow(0); HSSFCell cell = null; /** * 1.马品种常量表 */ if(ExcelName.equals("马品种常量表")){ // 创建一个文件(有中文必须URL编码) fileName = ExcelName + ".xls";//创建文件名(改) fileNameURL = URLEncoder.encode(fileName, "UTF-8"); System.out.println(fileName); //获取数据(查出想要导出的数据) List<HorseBreedConstant> lists = exportService.getHorseBreedConstant(); String[] title = { "序号","马品种名" };//设置EXCEL的第一行的标题头(改) // 插入第一行,标题头 for (int i = 0; i < title.length; i++) { // 创建一行的一格 cell = row.createCell(i); // 赋值 cell.setCellValue(title[i]); } if(lists.size() != 0){ // 追加数据行数 int j = 1; HorseBreedConstant list = null; for (int i = 0; i < lists.size(); i++) { // 从集合中得到一个对象 list = lists.get(i); // 创建第2行(数据) HSSFRow nextrow = sheet.createRow(i+1); // 创建第2行第1列并赋值 HSSFCell cessk = nextrow.createCell(0); cessk.setCellValue(list.getHorse_breed_id());//改 // 创建第2行第2列并赋值 cessk = nextrow.createCell(1); cessk.setCellValue(list.getHorse_breed());//改 /* cessk = nextrow.createCell(2); cessk.setCellValue(Integer.parseInt(new java.text.DecimalFormat("0").format(list.get("upload"))));//改 */ j++; } System.out.println(j+"条数据"); } } if (fileName.equals("")) { response.getWriter().write("失败,失败原因:参数为空!"); workbook.close(); return ResponseData.error("失败,失败原因:参数错误!"); } response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename="+fileNameURL+";"+"filename*=utf-8''"+fileNameURL); OutputStream ouputStream; try { ouputStream = response.getOutputStream(); workbook.write(ouputStream); ouputStream.flush(); ouputStream.close(); return ResponseData.success("ok"); } catch (IOException e) { return ResponseData.error("catch"); } finally{ workbook.close(); }
4part
service
@Service @Transactional public class ExportServiceImpl implements ExportService{ @Autowired private ExportMapper exportMapper; @Override public List<HorseBreedConstant> getHorseBreedConstant(){ //返回查询结果 return exportMapper.getHorseBreedConstant(); } }
5part
mapper.xml
<select id="getHorseBreedConstant" resultType="xxx"> SELECT * FROM horse_breed_constant </select>
结果
火狐浏览器测试(文件名中文乱码问题)
导出的excel
片尾
主要部分是在获取lists后,处理lists为想要打印的数据。
//获取数据 List<SemenTransportBackCustom> lists = zinSummaryMapper.getMaleYearStation(semenTransportBackCustom); //循环插入序号 for (int i = 0; i < lists.size(); i++) { //序号 lists.get(i).setId(i+1); }
带查询的话,访问地址进行字符串拼接,带参数。mapper.xml加语句即可,使数据为筛选后的。
花絮
xls和xlsx
fileName = ExcelName + ".xlsx";//创建文件名(改)
xlsx后缀名出错
xls是Excel1997-2003的格式,xlsx是Excel 2007之后版本创建的格式。 两种版本在保存时编码不一样。改成xls就好了
导出文件名乱码
日期转化文字符串输出
// 日期格式转为字符串输出 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
cessk = nextrow.createCell(4); cessk.setCellValue(dateFormat.format(list.getMating_date()));
调整列宽
for (int i = 0; i < title.length; i++) { sheet.setColumnWidth(i, title[i].getBytes().length*1*256+1000);//(中文适用) //sheet.autoSizeColumn(i); 自适应宽度 }
加批注
for (int i = 0; i < title.length; i++) { cell = row.createCell(i);// 创建一行的一格 cell.setCellValue(title[i]);// 赋值 if(i==2){ //1.得到一个POI的工具类 CreationHelper factory = workbook.getCreationHelper(); //2.得到一个换图的对象 Drawing<?> drawing = sheet.createDrawingPatriarch(); //3.对这个单元格加上注解 Comment comment0 = drawing.createCellComment(new HSSFClientAnchor(0,0,0,0,(short)3,3,(short)5,6)); //设置批注框位置 RichTextString str0 = factory.createRichTextString("'-1'表示芯片号为空"); comment0.setString(str0); comment0.setAuthor("administrator"); cell.setCellComment(comment0); } }
鸣谢
相关文章推荐
- SSM框架poi导入导出Excel(MySQL)
- 框架 day50 BOS项目 4 批量导入(ocupload插件,pinyin4J)/POI解析Excel/Combobox下拉框/分区组合条件分页查询(ajax)/分区数据导出(Excel)
- Maven整合SSM框架相关的配置文件
- SSM框架使用POI技术导出Excel表
- SSM框架使用POI导出EXCEL
- SSM框架使用POI技术导出Excel表
- Maven 基于SSM框架的增删改(使用了bootstrap的模态框,分页插件pagehelper, ajax等,附sql文件)
- ssm框架使用poi实现数据导出到excel,并弹框下载
- maven-ssm框架基本整合之配置文件的配置
- 005商城项目:ssm框架的整合成功之后的问题:使用maven的tomcat插件时的debug
- 最基础的-----基于maven进行的SSM框架的整合的全部配置文件.
- SSM框架整合(curd+分页+导出为excel)
- POI框架导出EXCEL的简单列子(跨行跨列)合并单元格
- IDEA-Maven整合SSM框架,超详细教程(Spring+SpringMVC+Mybatis)
- Poi对Excel文件的导入导出
- java学习之--导入导出excel文件 通过poi
- springmvc整合mybatis框架源码 bootstrap html5 mysql oracle maven SSM
- 构建Maven多模块项目+SSM框架整合+Thymeleaf(二)
- 用POI实现excel文件导出 代码示例分享
- java的poi技术下载Excel模板上传Excel读取Excel中内容(SSM框架)