Java之使用poi导出excel文件,并为特定单元格加锁

您所在的位置:网站首页 excel表不可编辑 Java之使用poi导出excel文件,并为特定单元格加锁

Java之使用poi导出excel文件,并为特定单元格加锁

2024-06-30 14:09| 来源: 网络整理| 查看: 265

使用 SXSSFWorkbook 进行Excel导出下载

注意:测试结果没有达到预期,那换个方式试试

参考:1.https://blog.csdn.net/aiza4108/article/details/101129894 2.https://blog.csdn.net/cc_yy_zh/article/details/78772217

我想要的需求 1.导出的excel表头不能被修改 2.表头所在列的行(初始化行数,文章是50w行)是可以被编辑的(除过表头) 3.其余是不可编辑的 4.如下图 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191227155811772.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1lUUkVFX0JK,size_16,color_FFFFFF,t_70 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 为特定列行加锁 1.先锁住整张sheet 2.在对不加锁的列和行进行解锁

加锁核心代码 为当前sheet加锁,加完锁后,整个sheet就会被锁定

// 为当前sheet加锁 sheet.protectSheet(EXCEL_LOCK);

解锁锁核心代码 在这里插入图片描述

// 1.为单元格解锁,并设置单元格格式为文本 CellStyle unlockStyle = sxssfWorkbook.createCellStyle(); unlockStyle.setLocked(false); unlockStyle.setDataFormat(dataFormat.getFormat("@")); // 2.将设置好的unlockStyle 赋予到特定单元格上 SXSSFCell cell = sheetRow.createCell(column); cell.setCellStyle(unlockStyle);

示例代码1

package com.qzlink.util.excel; import com.jfinal.core.Controller; import com.jfinal.plugin.activerecord.Record; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.LinkedList; import java.util.List; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.DataFormat; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.IndexedColors; 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.streaming.SXSSFCell; import org.apache.poi.xssf.streaming.SXSSFRow; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.base.Joiner; public class ExcelUtil { private static Logger log = LoggerFactory.getLogger(ExcelUtil.class); private static final String EXCEL_LOCK = "732a138b-288d-49fd-8039-3f3673c64a66"; public static void exportExcel(Controller controller, String[] headers, String[] columns, String fileName, List dataList) { HttpServletResponse response = controller.getResponse(); response.setHeader("Content-disposition", "attachment; filename=" + fileName); response.setContentType("application/msexcel;charset=UTF-8"); response.setCharacterEncoding("utf-8"); OutputStream out = null; try{ out = response.getOutputStream(); // 创建工作簿 SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(); // 数据格式化 DataFormat dataFormat = sxssfWorkbook.createDataFormat(); // 设置单元格格式为“文本”格式 CellStyle cellStyle = sxssfWorkbook.createCellStyle(); cellStyle.setDataFormat(dataFormat.getFormat("@")); // 设置单元格为锁定模式,并设置单元格格式为文本 // 以下加锁代码可不用,这段代码多此一举 CellStyle lockStyle = sxssfWorkbook.createCellStyle(); lockStyle.setLocked(true); lockStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); lockStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); lockStyle.setDataFormat(dataFormat.getFormat("@")); // 为单元格解锁,并设置单元格格式为文本 // 解锁代码 CellStyle unlockStyle = sxssfWorkbook.createCellStyle(); unlockStyle.setLocked(false); unlockStyle.setDataFormat(dataFormat.getFormat("@")); // 创建工作表 SXSSFSheet sheet = sxssfWorkbook.createSheet("sheet1"); // 判断header是否为空 if (null == headers || headers.length


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3