java Excel 自用开发模板
下载导出
import com.hpay.admin.api.vo.Message;
import com.hpay.admin.dubbo.IConfigDubboService;
import com.hpay.admin.dubbo.IFileExportLogDubboService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;/*** 文件公共操作* @author Garcia*/@Controller
@RequestMapping("/file")
@Slf4j
public class FileCommonController {private static final String FILE_SEPARATOR = System.getProperties().getProperty("file.separator");private static final String TXT = "txt";private static final String CSV = "csv";private static final String XLS = "xls";private static final String XLSX = "xlsx";@Autowiredprivate IConfigDubboService configService;@Resourceprivate IFileExportLogDubboService fileExportLogDubboService;/*** 下载Excel** @param response* @param excName*/@RequestMapping("downExcel")public void downExcel(HttpServletResponse response, String excName,String fileType,String downFileName) {if (StringUtils.isBlank(excName)) {log.warn("文件名为空");return;}if (StringUtils.isBlank(fileType)) {log.warn("文件类型为空");return;}String path = configService.getProperty("tempExcelPath");String fileName = downFileName + "-" + excName;if (TXT.equals(fileType)||CSV.equals(fileType)){writeTxt(response,excName,path,fileName);}else if(XLS.equals(fileType) || XLSX.equals(fileType)){writeExcel(response,excName,path,fileName);}}private void writeTxt(HttpServletResponse response, String excName,String path,String fileName){//设置文件路径File file = new File(path + FILE_SEPARATOR + excName);if (file.exists()) {response.setContentType("application/binary; charset=UTF-8");response.setCharacterEncoding("UTF-8");try {response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(fileName,StandardCharsets.UTF_8));} catch (UnsupportedEncodingException e) {log.error("其他错误!", e);}byte[] buffer = new byte[1024];FileInputStream fis = null;BufferedInputStream bis = null;try {fis = new FileInputStream(file);bis = new BufferedInputStream(fis);OutputStream os = response.getOutputStream();int i = bis.read(buffer);while (i != -1) {os.write(buffer, 0, i);i = bis.read(buffer);}} catch (Exception e) {log.error("写出文本文件错误!", e);} finally {if (bis != null) {try {bis.close();} catch (IOException e) {log.error("关闭流错误!", e);}}if (fis != null) {try {fis.close();} catch (IOException e) {log.error("关闭流错误!", e);}}delexcel(excName);}}}private void writeExcel(HttpServletResponse response, String excName,String path,String fileName){File file = new File(path + FILE_SEPARATOR + excName);if (file.exists()) {HSSFWorkbook wb = null;try {InputStream is = new FileInputStream(file);wb = new HSSFWorkbook(is);} catch (Exception e) {log.error("读取Excel文件错误!", e);}response.setContentType("application/binary; charset=UTF-8");response.setCharacterEncoding("UTF-8");try {response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(fileName,StandardCharsets.UTF_8));} catch (UnsupportedEncodingException e) {log.error("其他错误!", e);}OutputStream os = null;try {os = response.getOutputStream();if (wb != null) {wb.write(os);}if (os != null) {os.flush();}} catch (IOException e) {log.error("写出Excel文件错误!", e);} finally {if (os != null) {try {os.close();} catch (IOException e) {log.error("关闭流错误!", e);}}delexcel(excName);}}}/*** 删除服务器Excel文件** @param excName* @return*/@RequestMapping("delExcel")@ResponseBodypublic Message delexcel(String excName) {String path = configService.getProperty("tempExcelPath");try{File file = new File(path + FILE_SEPARATOR + excName);long len = file.length();Thread.sleep(3000);if (len!=file.length()){return Message.error("当前文件正在操作,请稍后再删");}file.delete();fileExportLogDubboService.deleteByName(excName);}catch (Exception e){log.error("文件删除异常",e);}return Message.success();}
生成Excel
String []titles = new String[]{"",""};HSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet=generateContentSheet(wb,titles);createRow(wb,sheet,rowList);private void createRow(HSSFWorkbook wb,HSSFSheet sheet, List<List<Object>> warnlist) throws ClassCastException{HSSFRow row =null;HSSFCellStyle style = wb.createCellStyle();style.setWrapText(true);for (List<Object> value : warnlist) {row = sheet.createRow(sheet.getLastRowNum() + 1);for (int i = 0; i < value.size(); i++) {Cell cell = row.createCell(i);cell.setCellStyle(style);if(value.get(i) instanceof String){cell.setCellValue((String)value.get(i));}else if(value.get(i) instanceof Integer){cell.setCellValue((Integer)value.get(i));}else if(value.get(i) instanceof Double){cell.setCellValue((Double)value.get(i));}else if(value.get(i) instanceof Boolean){cell.setCellValue((Boolean)value.get(i));}else if(value.get(i) instanceof Date){cell.setCellValue((Date)value.get(i));}else if(value.get(i) instanceof Calendar){cell.setCellValue((Calendar)value.get(i));}else if(value.get(i) instanceof RichTextString){cell.setCellValue((RichTextString)value.get(i));}else if(value.get(i) instanceof Long){cell.setCellValue((Long)value.get(i));}else if(value.get(i) instanceof BigDecimal){cell.setCellValue(value.get(i).toString());}else if(value.get(i)==null){cell.setCellValue("");}else{log.error("不支持导出类型:{},{}",value.get(i).getClass(),value.get(i));throw new ClassCastException("不支持导出类型:"+value.get(i).getClass()+","+value.get(i));}}}}private HSSFSheet generateContentSheet(HSSFWorkbook workbook,String[] titles){HSSFSheet sheet = null;sheet = workbook.createSheet("审评报告");HSSFRow row = sheet.createRow(0);sheet.autoSizeColumn(0);sheet.setColumnWidth(0,sheet.getColumnWidth(0)*17/10);sheet.autoSizeColumn(1);sheet.setColumnWidth(1,sheet.getColumnWidth(1)*27/10);sheet.autoSizeColumn(2);sheet.setColumnWidth(2,sheet.getColumnWidth(2)*17/10);sheet.autoSizeColumn(3);sheet.setColumnWidth(3,sheet.getColumnWidth(3)*17/10);sheet.autoSizeColumn(4);sheet.setColumnWidth(4,sheet.getColumnWidth(4)*17/10);CellStyle style;Font headerFont = workbook.createFont();
// headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);style = createBorderedStyle(workbook);
// style.setAlignment(CellStyle.ALIGN_CENTER);
// style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
// style.setFillPattern(BorderStyle.SOLID_FOREGROUND);
// style.setFont(headerFont);for (int i = 0; i < titles.length; i++) {HSSFCell cell = row.createCell(i);cell.setCellValue(titles[i]);cell.setCellStyle(style);}return sheet;}/*** 生产单元格样式* @param wb* @return*/private static CellStyle createBorderedStyle(Workbook wb) {CellStyle style = wb.createCellStyle();
// style.setBorderRight(BorderStyle.THIN);
// style.setRightBorderColor(IndexedColors.BLACK.getIndex());
// style.setBorderBottom(BorderStyle.THIN);
// style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
// style.setBorderLeft(BorderStyle.THIN);
// style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
// style.setBorderTop(BorderStyle.THIN);
// style.setTopBorderColor(IndexedColors.BLACK.getIndex());return style;}
相关文章:
![](https://www.ngui.cc/images/no-images.jpg)
java Excel 自用开发模板
下载导出 import com.hpay.admin.api.vo.Message; import com.hpay.admin.dubbo.IConfigDubboService; import com.hpay.admin.dubbo.IFileExportLogDubboService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.apache.poi.hss…...
![](https://img-blog.csdnimg.cn/599cc1edcb874fadaa19f4e14aa719f2.gif#pic_center)
34.CSS魔线图标的悬停效果
效果 源码 index.html <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Icon Fill Hover Effects</title> <link rel="stylesheet" h…...
![](https://img-blog.csdnimg.cn/cd53f00d4e014079b06480c8def3b5fe.png#pic_center)
Django — 会话
目录 一、Cookie1、介绍2、作用3、工作原理4、结构5、用途6、设置7、获取 二、Session1、介绍2、作用3、工作原理3、类型4、用途5、设置6、获取7、清空信息 三、Cookie 和 Session 的区别1、存储位置2、安全性3、数据大小4、跨页面共享5、生命周期6、实现机制7、适用场景 四、P…...
![](https://img-blog.csdnimg.cn/f37b10a9683940d19109a3ddf7fc5ca6.png)
SpringBoot集成easypoi实现execl导出
<!--easypoi依赖,excel导入导出--><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-spring-boot-starter</artifactId><version>4.4.0</version></dependency>通过Exce注解设置标头名字和单…...
![](https://img-blog.csdnimg.cn/3e44818d59a9449d90a4d8c672346366.png)
第9章 【MySQL】InnoDB的表空间
表空间 是一个抽象的概念,对于系统表空间来说,对应着文件系统中一个或多个实际文件;对于每个独立表空间来说,对应着文件系统中一个名为 表名.ibd 的实际文件。大家可以把表空间想象成被切分为许许多多个 页 的池子,当我…...
![](https://www.ngui.cc/images/no-images.jpg)
工作、生活常用免费api接口大全
手机号码归属地:提供三大运营商的手机号码归属地查询。全国快递物流查询:1.提供包括申通、顺丰、圆通、韵达、中通、汇通等600快递公司在内的快递物流单号查询。2.与官网实时同步更新。3.自动识别快递公司。IP归属地-IPv4区县级:根据IP地址查…...
![](https://img-blog.csdnimg.cn/42b1a6c227f84a6bb0b80f4a426f149b.png)
寻找单身狗
在一个数组中仅出现一次,其他数均出现两次,这个出现一次的数就被称为“单身狗“。 一.一个单身狗 我们知道异或运算操作符 ^ ,它的特点是对应二进制位相同为 0,相异为 1。 由此我们容易知道两个相同的数,进行异或运算得到的结果…...
![](https://img-blog.csdnimg.cn/4d80aaf84aff4c81aaa0b77f7ae9b292.png)
【pytest】 allure 生成报告
1. 下载地址 官方文档; Allure Framework 参考文档: 最全的PytestAllure使用教程,建议收藏 - 知乎 https://github.com/allure-framework 1.2安装Python依赖 windows:pip install allure-pytest 2. 脚本 用例 import pytest class …...
![](https://www.ngui.cc/images/no-images.jpg)
动态链接库搜索顺序
动态链接库搜索顺序 同一动态链接库 (DLL) 的多个版本通常存在于操作系统 (OS) 内的不同文件系统位置。 可以通过指定完整路径来控制从中加载任何给定 DLL 的特定位置。 但是,如果不使用该方法,则系统会在加载时搜索 DLL,如本主题中所述。 DL…...
![](https://www.ngui.cc/images/no-images.jpg)
【CAN、LIN通信的区分】
CAN和LIN是两种不同的通信协议,用于不同的应用场景。CAN(Controller Area Network)是一种高速、可靠、多节点的串行通信协议,主要用于汽车电子领域的高速数据传输和控制;而LIN(Local Interconnect Network&…...
![](https://img-blog.csdnimg.cn/888dd1842770411990c2f9c9a062cd1b.png)
Redis环境配置
【Redis解压即可】链接:https://pan.baidu.com/s/1y4xVLF8-8PI8qrczbxde9w?pwd0122 提取码:0122 【Redis桌面工具】 链接:https://pan.baidu.com/s/1IlsUy9sMfh95dQPeeM_1Qg?pwd0122 提取码:0122 Redis安装步骤 1.先打开Redis…...
![](https://www.ngui.cc/images/no-images.jpg)
UG NX二次开发(C++)-采用std::vector对体对象的质心进行排序
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 1、前言2、体对象质心结构体的构造3、采用NXOpen获取part中的所有体对象4、通过遍历体对象集合来实现std::vector<MyBody>的赋值5、对结构体排序6、调用的完整源代码7、生成dll并测试一、pan…...
![](https://www.ngui.cc/images/no-images.jpg)
一点思考|关于「引领性研究」的一点感悟
前言:调研过这么多方向之后,对研究方向的产生与发展具备了一些自己的感悟,尤其是在AI安全领域。私认为,所谓有价值、有意义的研究,就是指在现实社会中能够产生波澜、为国家和社会产生一定效益的研究。 举例来说&#x…...
![](https://img-blog.csdnimg.cn/49003d19702f43c9a233b53cf361faf0.png)
什么是HTTP/2?它与HTTP/1.1相比有什么改进?
聚沙成塔每天进步一点点 ⭐ 专栏简介⭐ HTTP/2 简介⭐ 主要的改进和特点⭐ 总结⭐ 写在最后 ⭐ 专栏简介 前端入门之旅:探索Web开发的奇妙世界 欢迎来到前端入门之旅!感兴趣的可以订阅本专栏哦!这个专栏是为那些对Web开发感兴趣、刚刚踏入前端…...
![](https://www.ngui.cc/images/no-images.jpg)
IDEA
快捷键 好用的快捷键,可以使写代码变得更加便捷~ IntelliJ IDEA具有许多有用的快捷键,这些快捷键可以帮助开发人员更快速、高效地编写和管理代码。以下是一些常用的IntelliJ IDEA快捷键,这些快捷键在Java开发中特别有用: 基本编辑…...
![](https://img-blog.csdnimg.cn/img_convert/9544c5640f40e7501e0c6fa997973ae8.png)
NSS [HXPCTF 2021]includer‘s revenge
NSS [HXPCTF 2021]includer’s revenge 题目描述:Just sitting here and waiting for PHP 8.1 (lolphp). 题目源码:(index.php) <?php ($_GET[action] ?? read ) read ? readfile($_GET[file] ?? index.php) : inclu…...
![](https://img-blog.csdnimg.cn/23b4c8d00a284e9daa6c78ecaeeec175.png#pic_center)
《动手学深度学习 Pytorch版》 7.1 深度卷积神经网络(AlexNet)
7.1.1 学习表征 深度卷积神经网络的突破出现在2012年。突破可归因于以下两个关键因素: 缺少的成分:数据 数据集紧缺的情况在 2010 年前后兴起的大数据浪潮中得到改善。ImageNet 挑战赛中,ImageNet数据集由斯坦福大学教授李飞飞小组的研究人…...
![](https://img-blog.csdnimg.cn/f16d773a8dbf41c69abed5e1deefd80d.png)
C++ - 双指针_盛水最多的容器
盛水最多的容器 11. 盛最多水的容器 - 力扣(LeetCode) 给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。 找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的…...
![](https://img-blog.csdnimg.cn/9ede300cbf154687b7d5ad701dc44dd9.png#pic_center)
分类预测 | Matlab实现NGO-CNN-SVM北方苍鹰算法优化卷积支持向量机分类预测
分类预测 | Matlab实现NGO-CNN-SVM北方苍鹰算法优化卷积支持向量机分类预测 目录 分类预测 | Matlab实现NGO-CNN-SVM北方苍鹰算法优化卷积支持向量机分类预测分类效果基本描述程序设计参考资料 分类效果 基本描述 1.Matlab实现NGO-CNN-SVM北方苍鹰算法优化卷积支持向量机分类预…...
![](https://img-blog.csdnimg.cn/9bc0fe1352604b2db3a7208030a285d3.jpeg#pic_center)
分享一个java+springboot+vue校园电动车租赁系统(源码、调试、开题、lw)
💕💕作者:计算机源码社 💕💕个人简介:本人七年开发经验,擅长Java、Python、PHP、.NET、微信小程序、爬虫、大数据等,大家有这一块的问题可以一起交流! 💕&…...
![](https://img-blog.csdnimg.cn/img_convert/75c8b6dedd706964cdd5efe24d160263.png)
高性能计算环境下的深度学习异构集群建设与优化实践
★深度学习;模式识别;图像处理;人工智能建模;人工智能;深度学习算法;强化学习;神经网络;卷积神经网络;人工神经网络;VIBE算法;控制系统仿真&#…...
![](https://www.ngui.cc/images/no-images.jpg)
Laravel框架 - Facade门面
1 、官方文档给出的定义 “Facades 为应用的 服务容器 提供了一个「静态」 接口。Laravel 自带了很多 Facades,可以访问绝大部分功能。Laravel Facades 实际是服务容器中底层类的 「静态代理」 ,相对于传统静态方法,在使用时能够提供更加灵活…...
![](https://img-blog.csdnimg.cn/91610e0f00e8465e8e840182ca88252a.png)
算法通关村第16关【青铜】| 滑动窗口思想
1. 滑动窗口的基本思想 一句话概括就是两个快慢指针维护的一个会移动的区间 固定大小窗口:求哪个窗口元素最大、最小、平均值、和最大、和最小 可变大小窗口:求一个序列里最大、最小窗口是什么 2. 两个入门题 (1)子数组最大平…...
![](https://img-blog.csdnimg.cn/dd8cc0758ab847b4960974436312ac24.png)
CentOS安装openjdk和elasticsearch
CentOS安装openjdk 文章目录 CentOS安装openjdk一、yum1.1search1.2安装openjdk 二、elasticsearch的启动和关闭2.1启动2.2关闭2.3添加服务 一、yum 1.1search yum search java | grep jdk1.2安装openjdk [roottest ~]# yum install java-1.8.0-openjdk -y 查看openjdk版本 …...
![](https://img-blog.csdnimg.cn/e27eb0b6c6c04eebae49a13abf1a6975.png#pic_left)
【新版】系统架构设计师 - 案例分析 - 信息安全
个人总结,仅供参考,欢迎加好友一起讨论 文章目录 架构 - 案例分析 - 信息安全安全架构安全模型分类BLP模型Biba模型Chinese Wall模型 信息安全整体架构设计WPDRRC模型各模型安全防范功能 网络安全体系架构设计开放系统互联安全体系结构安全服务与安全机制…...
![](https://www.ngui.cc/images/no-images.jpg)
数据库设计(火车订票系统)
为一个火车订票系统设计一个数据库是一个好的方法来训练你的数据库技巧。 其中有一些需要考虑到的复杂度。 过一些需求,并且创建表格。 为这个虚构的火车订票系统提出了10个需求。 我们将把其中每个添加到entity relational diagram(实体关系图&…...
![](https://www.ngui.cc/images/no-images.jpg)
qemu+docker在服务器上搭建linux内核调试环境
基于docker和qemu的操作系统实验环境 参考以上文章实现。 其中 docker run -it --name linux_qemu qemu /bin/bash #从qemu镜像启动一个容器linux_qemu,进入shell 要改为 docker run -it --name linux_qemu 3292900173/qemu /bin/bash另外,在vscode运行过程中,ssh远…...
![](https://img-blog.csdnimg.cn/1c4ff11cfcb6425eaacc904e74c494f2.png)
Stable Diffusion 参数介绍及用法
大模型 CheckPoint 介绍 作用:定调了作图风格,可以理解为指挥者 安装路径:models/Stable-diffusion 推荐: AnythingV5Ink_v32Ink.safetensors cuteyukimixAdorable_midchapter2.safetensors manmaruMix_v10.safetensors counterf…...
![](https://www.ngui.cc/images/no-images.jpg)
打印大对象日志导致GC问题的解决
内容: rpc调用外部服务时,需要将req和resp的信息打印出来,以便于排查问题。但是有的rpc服务的resp信息过于庞大,比如resp中有List<>信息,list很大很大时会导致log.info打印信息时,产生GC,…...
![](https://www.ngui.cc/images/no-images.jpg)
【Docker】学习笔记
1. docker基本操作 镜像搜索 // 直接搜索镜像资源 docker search mysql // 搜索过滤 docker search --filter "is-officialtrue" mysql // 官方发布镜像拉取镜像 docker pull mysql查看本地镜像 docker images删除本地镜像 docker rmi mysql // 强制删除镜像 d…...
![](/images/no-images.jpg)
magento做的网站/互联网营销师培训课程
$ vi ~/.bashrc 59按a或i进入编辑模式PS1${debian_chroot:(debian_chroot)}\w\$默认为PS1${debian_chroot:(debian_chroot)}uh:\w\$ 注:u为username,h为hostname按Esc键退出编辑模式:wq (保...JS提取域中的主机我们使用dsquery导出的计算机结果基本上是…...
![](/images/no-images.jpg)
wordpress登陆后跳转到首页/百度网站排名查询工具
概要 题目来源链接:https://leetcode-cn.com/problems/decompress-run-length-encoded-list/ 难度:简单 类型:数组 题目 给你一个以行程长度编码压缩的整数列表 nums 。 考虑每对相邻的两个元素 [freq, val] [nums[2*i], nums[2*i1]] …...
![](https://img-blog.csdnimg.cn/img_convert/deacb5dbaf94906406ef77caae96d4fb.png)
扬州企业做网站/上海专业的seo推广咨询电话
一 面试问题 1.1 TCP为什么是安全可靠的传输协议 👨💻面试官:TCP为什么是安全的 🙋我:因为TCP在 数据传输之前会有三次握手来进行连接在数据传输时候,有确认、滑动窗口、超时重传、拥塞控制之类机制数…...
![](http://s3.51cto.com/wyfs02/M01/6B/D7/wKioL1U4ciiCzdO2AAGKfYc4is8009.jpg)
企业所得税政策最新2023税率/刷神马seo排名首页排名
前段时间处理了一个关于Exchange 数据库副本无法激活的故障,故障截图如下: 解决方法来看,我们尝试重新建立副本,但仍然无法成功创建,创建后提示失败搁置,那么我们接下来进行一下索引重建,关于重…...
![](/images/no-images.jpg)
为什么打开网站是建设中/重庆seo霸屏
Java break语句1 Java break语句某些时候需要在某种条件出现时强行终止循环,而不是等到循环条件为 false 时才退出循环。此时,可以使用 break 来完成这个功能。break 用于完全结束一个循环,跳出循环体。不管是哪种循环,一旦在循环…...
![](/images/no-images.jpg)
深圳网站维护/百度云网盘资源搜索
鼠标 是否存在 GetSystemMetrics(SM_MOUSEPRESENT) true/false 键个数 GetSystemMetrics(SM_CMOUSEBUTTONS) 左右手习惯 GetSystemMetrics(SM_SWAPBUTTON) 其他参数 SystemParametersInfo 鼠标消息中的lParam 低字节x坐标, 高字节y坐标 wParam鼠标键机shift,ctrl键的状态 MK_…...