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

JAVA删除excel指定列

首先POI没有提供删除列的API,所以就需要用其他的方式实现。

在 java - Apache POI xls column Remove - Stack Overflow 这里找到了实现方式:

先将该列所有值都清空,然后将该列之后的所有列往前移动。

下面的工具类中 

deleteColumns(InputStream excelStream, List<String> delColumnTitleList)方法实现了批量删除列的逻辑。

import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;/*** @Description* @ClassName ExcelUtil* @Date 2022/12/23 11:38*/
public class ExcelUtil {/*** 获取sheet表头* @param sheet* @return*/public static List<String> getTitle(Sheet sheet) {List<String> titleList = new ArrayList<>();if (sheet.getPhysicalNumberOfRows() > 0) {Row headerRow = sheet.getRow(0); // 获取第一行(表头行)for (int i = 0; i < headerRow.getPhysicalNumberOfCells(); i++) {Cell cell = headerRow.getCell(i);if (cell != null) {String headerText = cell.getStringCellValue();titleList.add(headerText);}}}return titleList;}/*** 删除excel指定列* @param excelStream excel流* @param delColumnTitleList 需要删除的列的表头* @return*/@SneakyThrowspublic static ByteArrayOutputStream deleteColumns(InputStream excelStream, List<String> delColumnTitleList) {ByteArrayOutputStream outputStream = new ByteArrayOutputStream();Workbook workbook = new XSSFWorkbook(excelStream);// 获取第一个sheetSheet sheet = workbook.getSheetAt(0);deleteColumns(sheet, delColumnTitleList);workbook.write(outputStream);workbook.close();outputStream.close();return outputStream;}/*** 删除sheet指定的列* @param sheet* @param delColumnTitleList*/public static void deleteColumns(Sheet sheet, List<String> delColumnTitleList) {List<String> titleList = getTitle(sheet);for (String delTitle : delColumnTitleList) {int i = titleList.indexOf(delTitle);if (i >= 0) {deleteColumn(sheet, i);}//由于是循环删除,删除后,列所在位置索引会变化,所以titleList相应也移除删除的列titleList.remove(delTitle);}}/*** 删除指定列* poi没有提供删除指定列的api,所以先将该列清空,然后将后续的列往前移动,这样达到删除列的效果* @param sheet* @param columnToDelete*/public static void deleteColumn(Sheet sheet, int columnToDelete) {int maxColumn = 0;for (int r = 0; r < sheet.getLastRowNum() + 1; r++) {Row row = sheet.getRow(r);// if no row exists here; then nothing to do; next!if (row == null) {continue;}// if the row doesn't have this many columns then we are good; next!int lastColumn = row.getLastCellNum();if (lastColumn > maxColumn) {maxColumn = lastColumn;}if (lastColumn < columnToDelete) {continue;}for (int x = columnToDelete + 1; x < lastColumn + 1; x++) {Cell oldCell = row.getCell(x - 1);if (oldCell != null) {row.removeCell(oldCell);}Cell nextCell = row.getCell(x);if (nextCell != null) {Cell newCell = row.createCell(x - 1, nextCell.getCellType());cloneCell(newCell, nextCell);}}}// Adjust the column widthsfor (int c = 0; c < maxColumn; c++) {sheet.setColumnWidth(c, sheet.getColumnWidth(c + 1));}}/*** 右边列左移*/private static void cloneCell(Cell cNew, Cell cOld) {cNew.setCellComment(cOld.getCellComment());cNew.setCellStyle(cOld.getCellStyle());switch (cNew.getCellTypeEnum()) {case BOOLEAN: {cNew.setCellValue(cOld.getBooleanCellValue());break;}case NUMERIC: {cNew.setCellValue(cOld.getNumericCellValue());break;}case STRING: {cNew.setCellValue(cOld.getStringCellValue());break;}case ERROR: {cNew.setCellValue(cOld.getErrorCellValue());break;}case FORMULA: {cNew.setCellFormula(cOld.getCellFormula());break;}}}}

http://www.lryc.cn/news/209450.html

相关文章:

  • Netty编码器和解码器
  • 大语言模型(LLM)综述(三):大语言模型预训练的进展
  • 如何在Node.js中使用环境变量或命令行参数来设置HTTP爬虫ip?
  • VMware打开共享虚拟机后找不到/mnt/hgfs/文件夹,以及不能拖拽/复制粘贴等操作,ubuntu不能安装VMware tools
  • pytorch 入门 (五)案例三:乳腺癌识别识别-VGG16实现
  • 【QT开发(14)】QT P2P chat 聊天
  • 解决adb root命令时错误 adbd cannot run as root in production builds
  • 操作系统中套接字和设备独立性软件的关系
  • C++ Qt/VTK装配体组成联动连接杆
  • File文件查找
  • 小程序 wxml2canvas开发文档
  • SpringCloud微服务 【实用篇】| 认识微服务
  • Csdn文章编写参考案例
  • Jmeter性能测试:高并发分布式性能测试
  • 2015年亚太杯APMCM数学建模大赛B题城市公共交通服务水平动态评价模型求解全过程文档及程序
  • CCF CSP认证历年题目自练 Day40
  • 闲聊一下写技术博客的一些感想
  • 单片机为什么一直用C语言,不用其他编程语言?
  • 利用HTTP2,新型DDoS攻击峰值破纪录
  • android鼠标滚轮事件监听方法
  • 【C语言|关键字】C语言32个关键字详解(4)——其他(typedef、sizeof)
  • Hafnium简介和构建
  • 2023年香水行业数据分析:国人用香需求升级,高端香水高速增长
  • 这可能是最简单的Page Object库
  • 论文阅读——BERT
  • 竞赛 深度学习人体跌倒检测 -yolo 机器视觉 opencv python
  • Springboot创建多数据源
  • 【Hello Algorithm】滑动窗口内最大值最小值
  • HTML,CSS实现鼠标划过头像,头像突出变大(附源码)
  • “爱知道”,你知道吗?