当前位置: 首页 > news >正文

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

相关文章:

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…...

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…...

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…...

SpringBoot集成easypoi实现execl导出

<!--easypoi依赖&#xff0c;excel导入导出--><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-spring-boot-starter</artifactId><version>4.4.0</version></dependency>通过Exce注解设置标头名字和单…...

第9章 【MySQL】InnoDB的表空间

表空间 是一个抽象的概念&#xff0c;对于系统表空间来说&#xff0c;对应着文件系统中一个或多个实际文件&#xff1b;对于每个独立表空间来说&#xff0c;对应着文件系统中一个名为 表名.ibd 的实际文件。大家可以把表空间想象成被切分为许许多多个 页 的池子&#xff0c;当我…...

工作、生活常用免费api接口大全

手机号码归属地&#xff1a;提供三大运营商的手机号码归属地查询。全国快递物流查询&#xff1a;1.提供包括申通、顺丰、圆通、韵达、中通、汇通等600快递公司在内的快递物流单号查询。2.与官网实时同步更新。3.自动识别快递公司。IP归属地-IPv4区县级&#xff1a;根据IP地址查…...

寻找单身狗

在一个数组中仅出现一次&#xff0c;其他数均出现两次&#xff0c;这个出现一次的数就被称为“单身狗“。 一.一个单身狗 我们知道异或运算操作符 ^ &#xff0c;它的特点是对应二进制位相同为 0&#xff0c;相异为 1。 由此我们容易知道两个相同的数,进行异或运算得到的结果…...

【pytest】 allure 生成报告

1. 下载地址 官方文档; Allure Framework 参考文档&#xff1a; 最全的PytestAllure使用教程&#xff0c;建议收藏 - 知乎 https://github.com/allure-framework 1.2安装Python依赖 windows&#xff1a;pip install allure-pytest 2. 脚本 用例 import pytest class …...

动态链接库搜索顺序

动态链接库搜索顺序 同一动态链接库 (DLL) 的多个版本通常存在于操作系统 (OS) 内的不同文件系统位置。 可以通过指定完整路径来控制从中加载任何给定 DLL 的特定位置。 但是&#xff0c;如果不使用该方法&#xff0c;则系统会在加载时搜索 DLL&#xff0c;如本主题中所述。 DL…...

【CAN、LIN通信的区分】

CAN和LIN是两种不同的通信协议&#xff0c;用于不同的应用场景。CAN&#xff08;Controller Area Network&#xff09;是一种高速、可靠、多节点的串行通信协议&#xff0c;主要用于汽车电子领域的高速数据传输和控制&#xff1b;而LIN&#xff08;Local Interconnect Network&…...

Redis环境配置

【Redis解压即可】链接&#xff1a;https://pan.baidu.com/s/1y4xVLF8-8PI8qrczbxde9w?pwd0122 提取码&#xff1a;0122 【Redis桌面工具】 链接&#xff1a;https://pan.baidu.com/s/1IlsUy9sMfh95dQPeeM_1Qg?pwd0122 提取码&#xff1a;0122 Redis安装步骤 1.先打开Redis…...

UG NX二次开发(C++)-采用std::vector对体对象的质心进行排序

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 1、前言2、体对象质心结构体的构造3、采用NXOpen获取part中的所有体对象4、通过遍历体对象集合来实现std::vector<MyBody>的赋值5、对结构体排序6、调用的完整源代码7、生成dll并测试一、pan…...

一点思考|关于「引领性研究」的一点感悟

前言&#xff1a;调研过这么多方向之后&#xff0c;对研究方向的产生与发展具备了一些自己的感悟&#xff0c;尤其是在AI安全领域。私认为&#xff0c;所谓有价值、有意义的研究&#xff0c;就是指在现实社会中能够产生波澜、为国家和社会产生一定效益的研究。 举例来说&#x…...

什么是HTTP/2?它与HTTP/1.1相比有什么改进?

聚沙成塔每天进步一点点 ⭐ 专栏简介⭐ HTTP/2 简介⭐ 主要的改进和特点⭐ 总结⭐ 写在最后 ⭐ 专栏简介 前端入门之旅&#xff1a;探索Web开发的奇妙世界 欢迎来到前端入门之旅&#xff01;感兴趣的可以订阅本专栏哦&#xff01;这个专栏是为那些对Web开发感兴趣、刚刚踏入前端…...

IDEA

快捷键 好用的快捷键&#xff0c;可以使写代码变得更加便捷~ IntelliJ IDEA具有许多有用的快捷键&#xff0c;这些快捷键可以帮助开发人员更快速、高效地编写和管理代码。以下是一些常用的IntelliJ IDEA快捷键&#xff0c;这些快捷键在Java开发中特别有用&#xff1a; 基本编辑…...

NSS [HXPCTF 2021]includer‘s revenge

NSS [HXPCTF 2021]includer’s revenge 题目描述&#xff1a;Just sitting here and waiting for PHP 8.1 (lolphp). 题目源码&#xff1a;&#xff08;index.php&#xff09; <?php ($_GET[action] ?? read ) read ? readfile($_GET[file] ?? index.php) : inclu…...

《动手学深度学习 Pytorch版》 7.1 深度卷积神经网络(AlexNet)

7.1.1 学习表征 深度卷积神经网络的突破出现在2012年。突破可归因于以下两个关键因素&#xff1a; 缺少的成分&#xff1a;数据 数据集紧缺的情况在 2010 年前后兴起的大数据浪潮中得到改善。ImageNet 挑战赛中&#xff0c;ImageNet数据集由斯坦福大学教授李飞飞小组的研究人…...

C++ - 双指针_盛水最多的容器

盛水最多的容器 11. 盛最多水的容器 - 力扣&#xff08;LeetCode&#xff09; 给定一个长度为 n 的整数数组 height 。有 n 条垂线&#xff0c;第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。 找出其中的两条线&#xff0c;使得它们与 x 轴共同构成的容器可以容纳最多的…...

分类预测 | Matlab实现NGO-CNN-SVM北方苍鹰算法优化卷积支持向量机分类预测

分类预测 | Matlab实现NGO-CNN-SVM北方苍鹰算法优化卷积支持向量机分类预测 目录 分类预测 | Matlab实现NGO-CNN-SVM北方苍鹰算法优化卷积支持向量机分类预测分类效果基本描述程序设计参考资料 分类效果 基本描述 1.Matlab实现NGO-CNN-SVM北方苍鹰算法优化卷积支持向量机分类预…...

分享一个java+springboot+vue校园电动车租赁系统(源码、调试、开题、lw)

&#x1f495;&#x1f495;作者&#xff1a;计算机源码社 &#x1f495;&#x1f495;个人简介&#xff1a;本人七年开发经验&#xff0c;擅长Java、Python、PHP、.NET、微信小程序、爬虫、大数据等&#xff0c;大家有这一块的问题可以一起交流&#xff01; &#x1f495;&…...

高性能计算环境下的深度学习异构集群建设与优化实践

★深度学习&#xff1b;模式识别&#xff1b;图像处理&#xff1b;人工智能建模&#xff1b;人工智能&#xff1b;深度学习算法&#xff1b;强化学习&#xff1b;神经网络&#xff1b;卷积神经网络&#xff1b;人工神经网络&#xff1b;VIBE算法&#xff1b;控制系统仿真&#…...

Laravel框架 - Facade门面

1 、官方文档给出的定义 “Facades 为应用的 服务容器 提供了一个「静态」 接口。Laravel 自带了很多 Facades&#xff0c;可以访问绝大部分功能。Laravel Facades 实际是服务容器中底层类的 「静态代理」 &#xff0c;相对于传统静态方法&#xff0c;在使用时能够提供更加灵活…...

算法通关村第16关【青铜】| 滑动窗口思想

1. 滑动窗口的基本思想 一句话概括就是两个快慢指针维护的一个会移动的区间 固定大小窗口&#xff1a;求哪个窗口元素最大、最小、平均值、和最大、和最小 可变大小窗口&#xff1a;求一个序列里最大、最小窗口是什么 2. 两个入门题 &#xff08;1&#xff09;子数组最大平…...

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版本 …...

【新版】系统架构设计师 - 案例分析 - 信息安全

个人总结&#xff0c;仅供参考&#xff0c;欢迎加好友一起讨论 文章目录 架构 - 案例分析 - 信息安全安全架构安全模型分类BLP模型Biba模型Chinese Wall模型 信息安全整体架构设计WPDRRC模型各模型安全防范功能 网络安全体系架构设计开放系统互联安全体系结构安全服务与安全机制…...

数据库设计(火车订票系统)

为一个火车订票系统设计一个数据库是一个好的方法来训练你的数据库技巧。 其中有一些需要考虑到的复杂度。 过一些需求&#xff0c;并且创建表格。 为这个虚构的火车订票系统提出了10个需求。 我们将把其中每个添加到entity relational diagram&#xff08;实体关系图&…...

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另外&#xff0c;在vscode运行过程中,ssh远…...

Stable Diffusion 参数介绍及用法

大模型 CheckPoint 介绍 作用&#xff1a;定调了作图风格&#xff0c;可以理解为指挥者 安装路径&#xff1a;models/Stable-diffusion 推荐&#xff1a; AnythingV5Ink_v32Ink.safetensors cuteyukimixAdorable_midchapter2.safetensors manmaruMix_v10.safetensors counterf…...

打印大对象日志导致GC问题的解决

内容&#xff1a; rpc调用外部服务时&#xff0c;需要将req和resp的信息打印出来&#xff0c;以便于排查问题。但是有的rpc服务的resp信息过于庞大&#xff0c;比如resp中有List<>信息&#xff0c;list很大很大时会导致log.info打印信息时&#xff0c;产生GC&#xff0c…...

【Docker】学习笔记

1. docker基本操作 镜像搜索 // 直接搜索镜像资源 docker search mysql // 搜索过滤 docker search --filter "is-officialtrue" mysql // 官方发布镜像拉取镜像 docker pull mysql查看本地镜像 docker images删除本地镜像 docker rmi mysql // 强制删除镜像 d…...

magento做的网站/互联网营销师培训课程

$ vi ~/.bashrc 59按a或i进入编辑模式PS1${debian_chroot:(debian_chroot)}\w\$默认为PS1${debian_chroot:(debian_chroot)}uh:\w\$ 注&#xff1a;u为username&#xff0c;h为hostname按Esc键退出编辑模式:wq (保...JS提取域中的主机我们使用dsquery导出的计算机结果基本上是…...

wordpress登陆后跳转到首页/百度网站排名查询工具

概要 题目来源链接&#xff1a;https://leetcode-cn.com/problems/decompress-run-length-encoded-list/ 难度&#xff1a;简单 类型&#xff1a;数组 题目 给你一个以行程长度编码压缩的整数列表 nums 。 考虑每对相邻的两个元素 [freq, val] [nums[2*i], nums[2*i1]] …...

扬州企业做网站/上海专业的seo推广咨询电话

一 面试问题 1.1 TCP为什么是安全可靠的传输协议 &#x1f468;‍&#x1f4bb;面试官&#xff1a;TCP为什么是安全的 &#x1f64b;我&#xff1a;因为TCP在 数据传输之前会有三次握手来进行连接在数据传输时候&#xff0c;有确认、滑动窗口、超时重传、拥塞控制之类机制数…...

企业所得税政策最新2023税率/刷神马seo排名首页排名

前段时间处理了一个关于Exchange 数据库副本无法激活的故障&#xff0c;故障截图如下&#xff1a; 解决方法来看&#xff0c;我们尝试重新建立副本&#xff0c;但仍然无法成功创建&#xff0c;创建后提示失败搁置&#xff0c;那么我们接下来进行一下索引重建&#xff0c;关于重…...

为什么打开网站是建设中/重庆seo霸屏

Java break语句1 Java break语句某些时候需要在某种条件出现时强行终止循环&#xff0c;而不是等到循环条件为 false 时才退出循环。此时&#xff0c;可以使用 break 来完成这个功能。break 用于完全结束一个循环&#xff0c;跳出循环体。不管是哪种循环&#xff0c;一旦在循环…...

深圳网站维护/百度云网盘资源搜索

鼠标 是否存在 GetSystemMetrics(SM_MOUSEPRESENT) true/false 键个数 GetSystemMetrics(SM_CMOUSEBUTTONS) 左右手习惯 GetSystemMetrics(SM_SWAPBUTTON) 其他参数 SystemParametersInfo 鼠标消息中的lParam 低字节x坐标, 高字节y坐标 wParam鼠标键机shift,ctrl键的状态 MK_…...