当前位置: 首页 > 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;&…...

eNSP-Cloud(实现本地电脑与eNSP内设备之间通信)

说明&#xff1a; 想象一下&#xff0c;你正在用eNSP搭建一个虚拟的网络世界&#xff0c;里面有虚拟的路由器、交换机、电脑&#xff08;PC&#xff09;等等。这些设备都在你的电脑里面“运行”&#xff0c;它们之间可以互相通信&#xff0c;就像一个封闭的小王国。 但是&#…...

MPNet:旋转机械轻量化故障诊断模型详解python代码复现

目录 一、问题背景与挑战 二、MPNet核心架构 2.1 多分支特征融合模块(MBFM) 2.2 残差注意力金字塔模块(RAPM) 2.2.1 空间金字塔注意力(SPA) 2.2.2 金字塔残差块(PRBlock) 2.3 分类器设计 三、关键技术突破 3.1 多尺度特征融合 3.2 轻量化设计策略 3.3 抗噪声…...

OpenLayers 可视化之热力图

注&#xff1a;当前使用的是 ol 5.3.0 版本&#xff0c;天地图使用的key请到天地图官网申请&#xff0c;并替换为自己的key 热力图&#xff08;Heatmap&#xff09;又叫热点图&#xff0c;是一种通过特殊高亮显示事物密度分布、变化趋势的数据可视化技术。采用颜色的深浅来显示…...

《从零掌握MIPI CSI-2: 协议精解与FPGA摄像头开发实战》-- CSI-2 协议详细解析 (一)

CSI-2 协议详细解析 (一&#xff09; 1. CSI-2层定义&#xff08;CSI-2 Layer Definitions&#xff09; 分层结构 &#xff1a;CSI-2协议分为6层&#xff1a; 物理层&#xff08;PHY Layer&#xff09; &#xff1a; 定义电气特性、时钟机制和传输介质&#xff08;导线&#…...

Linux相关概念和易错知识点(42)(TCP的连接管理、可靠性、面临复杂网络的处理)

目录 1.TCP的连接管理机制&#xff08;1&#xff09;三次握手①握手过程②对握手过程的理解 &#xff08;2&#xff09;四次挥手&#xff08;3&#xff09;握手和挥手的触发&#xff08;4&#xff09;状态切换①挥手过程中状态的切换②握手过程中状态的切换 2.TCP的可靠性&…...

JVM垃圾回收机制全解析

Java虚拟机&#xff08;JVM&#xff09;中的垃圾收集器&#xff08;Garbage Collector&#xff0c;简称GC&#xff09;是用于自动管理内存的机制。它负责识别和清除不再被程序使用的对象&#xff0c;从而释放内存空间&#xff0c;避免内存泄漏和内存溢出等问题。垃圾收集器在Ja…...

高危文件识别的常用算法:原理、应用与企业场景

高危文件识别的常用算法&#xff1a;原理、应用与企业场景 高危文件识别旨在检测可能导致安全威胁的文件&#xff0c;如包含恶意代码、敏感数据或欺诈内容的文档&#xff0c;在企业协同办公环境中&#xff08;如Teams、Google Workspace&#xff09;尤为重要。结合大模型技术&…...

学习STC51单片机32(芯片为STC89C52RCRC)OLED显示屏2

每日一言 今天的每一份坚持&#xff0c;都是在为未来积攒底气。 案例&#xff1a;OLED显示一个A 这边观察到一个点&#xff0c;怎么雪花了就是都是乱七八糟的占满了屏幕。。 解释 &#xff1a; 如果代码里信号切换太快&#xff08;比如 SDA 刚变&#xff0c;SCL 立刻变&#…...

Mobile ALOHA全身模仿学习

一、题目 Mobile ALOHA&#xff1a;通过低成本全身远程操作学习双手移动操作 传统模仿学习&#xff08;Imitation Learning&#xff09;缺点&#xff1a;聚焦与桌面操作&#xff0c;缺乏通用任务所需的移动性和灵活性 本论文优点&#xff1a;&#xff08;1&#xff09;在ALOHA…...

处理vxe-table 表尾数据是单独一个接口,表格tableData数据更新后,需要点击两下,表尾才是正确的

修改bug思路&#xff1a; 分别把 tabledata 和 表尾相关数据 console.log() 发现 更新数据先后顺序不对 settimeout延迟查询表格接口 ——测试可行 升级↑&#xff1a;async await 等接口返回后再开始下一个接口查询 ________________________________________________________…...