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

博客网站开发教程设计电影完整版视频在线

博客网站开发教程,设计电影完整版视频在线,不建议网站,桂林seo排名首先POI没有提供删除列的API,所以就需要用其他的方式实现。 在 java - Apache POI xls column Remove - Stack Overflow 这里找到了实现方式: 先将该列所有值都清空,然后将该列之后的所有列往前移动。 下面的工具类中 deleteColumns(Inpu…

首先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.yayakq.cn/news/550615/

相关文章:

  • vue可以做pc网站吗贵阳网站建设服务公司
  • 无棣网站建设网站icp备案查不到
  • 网站兼容ie7青浦网站优化
  • 中国最新军事新闻最新消息seo如何快速出排名
  • 网站建设要学多久软装设计专业
  • 网站地图怎么做一键导航南阳网站推广招聘
  • 有诗意的设计公司名字seo做得好的企业网站
  • 自己做的网站响应速度慢南山网站优化
  • 摄影工作室网站源码网页网站设计公司排名
  • c 做网站时字体颜色的代码专业柳州网站建设公司
  • 创业网站模板免费下载什么免费推广网站好
  • 上海史特做网站多少钱dw网站建设教程
  • 外贸网站商城建设linux 网站备份
  • 手机网站策划国内永久在线免费建站
  • 安徽省建设工程造价管理总站网站源代码下载
  • 广州应用网站设计山西seo排名厂家
  • 泰安网站建设哪里找wordpress 倒计时广告
  • 搭建手机网站wordpress一键拨号
  • 好的做外贸的网站阿里云服务器上做网站
  • 肇庆企业自助建站系统网站建设企业哪家好
  • 没有网站可以做cpa桥头仿做网站
  • 南京做网站优化的企业排名上海推广seo
  • 百度SEO是谁做的网站万网是做什么的
  • 网站建设中需要注意的问题昆明企业建网站多少钱
  • 网站为什么做微云的采集关键词seo排名怎么样
  • 做系统用哪个网站好一个公司能备案几个网站
  • 室内设计网站推荐知乎国外优秀展厅设计
  • 青海网站建设公司电话如何建CMS网站
  • 厦门做外贸网站跨境外贸是做什么的
  • 网站头部特效大淘客网站商品做淘口令