1条回答 默认 最新
关注使用XSSFSheet的lockSelectLockedCells和lockSelectUnlockedCells方法。
事例代码如下。
import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class Test1 { public static void main(String[] args) throws IOException { String file = "E:\\tmp\\2.xlsx"; FileOutputStream outputStream = new FileOutputStream(file); Workbook wb = new XSSFWorkbook(); CellStyle unlockedCellStyle = wb.createCellStyle(); unlockedCellStyle.setLocked(false); // Sheet sheet = (XSSFSheet)wb.createSheet(); XSSFSheet secureSheet = (XSSFSheet) wb.createSheet(); Row row = secureSheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("TEST"); cell.setCellStyle(unlockedCellStyle); secureSheet.protectSheet(""); secureSheet.lockSelectLockedCells(true); secureSheet.lockSelectUnlockedCells(true); wb.write(outputStream); outputStream.close(); } }解决评论 打赏 举报无用 1
