Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用POI锁定某些Excel单元格/行其他可编辑单元格/行_Java_Apache Poi - Fatal编程技术网

Java 使用POI锁定某些Excel单元格/行其他可编辑单元格/行

Java 使用POI锁定某些Excel单元格/行其他可编辑单元格/行,java,apache-poi,Java,Apache Poi,我必须写一个excel文件,其中一些行和列是锁定的,其余的是可编辑的。我知道这是贝弗尔提出并回答的,但最新的答案是2012年的,给出的解决方案不再有效。任何一个曾经处理过这个问题的人都能给出一个现在有效的解决方案吗 这是作为解决方案给出的代码 String file = "c:\\poitest.xlsx"; FileOutputStream outputStream = new FileOutputStream(file); Workbook wb = new XSSFWorkbook();

我必须写一个excel文件,其中一些行和列是锁定的,其余的是可编辑的。我知道这是贝弗尔提出并回答的,但最新的答案是2012年的,给出的解决方案不再有效。任何一个曾经处理过这个问题的人都能给出一个现在有效的解决方案吗

这是作为解决方案给出的代码

String file = "c:\\poitest.xlsx";
FileOutputStream outputStream = new FileOutputStream(file);
Workbook wb = new XSSFWorkbook();

CellStyle unlockedCellStyle = wb.createCellStyle();
unlockedCellStyle.setLocked(false);

Sheet sheet = wb.createSheet();
sheet.protectSheet("password");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("TEST");
cell.setCellStyle(unlockedCellStyle);

wb.write(outputStream);
outputStream.close();

现在的效果是一个完全锁定的工作表。

您写道,您希望锁定某些单元格,默认值应该解锁,但您的代码实际上解锁了给定的单元格

因此,我接受了您最初的请求,并进行了快速破解,因为我还没有在快速查看中找到一个合适的方法来设置整个列范围:

import java.io.FileOutputStream;

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.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;

public class XSSFLockTest {
    public static void main(String args[]) throws Exception {
        XSSFWorkbook wb = new XSSFWorkbook();

        CellStyle unlockedCellStyle = wb.createCellStyle();
        unlockedCellStyle.setLocked(false);

        CellStyle lockedCellStyle = wb.createCellStyle();
        lockedCellStyle.setLocked(true);

        XSSFSheet sheet = wb.createSheet();
        CTCol col = sheet.getCTWorksheet().getColsArray(0).addNewCol();
        col.setMin(1);
        col.setMax(16384);
        col.setWidth(9.15);
        col.setStyle(unlockedCellStyle.getIndex());

        sheet.protectSheet("password");

        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("TEST");
        cell.setCellStyle(lockedCellStyle);

        FileOutputStream outputStream = new FileOutputStream("bla.xlsx");
        wb.write(outputStream);
        outputStream.close();
    }
}

您编写了这样的代码,您希望锁定某些单元格,默认值应该是解锁的,但是您的代码实际上解锁了给定的单元格

因此,我接受了您最初的请求,并进行了快速破解,因为我还没有在快速查看中找到一个合适的方法来设置整个列范围:

import java.io.FileOutputStream;

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.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;

public class XSSFLockTest {
    public static void main(String args[]) throws Exception {
        XSSFWorkbook wb = new XSSFWorkbook();

        CellStyle unlockedCellStyle = wb.createCellStyle();
        unlockedCellStyle.setLocked(false);

        CellStyle lockedCellStyle = wb.createCellStyle();
        lockedCellStyle.setLocked(true);

        XSSFSheet sheet = wb.createSheet();
        CTCol col = sheet.getCTWorksheet().getColsArray(0).addNewCol();
        col.setMin(1);
        col.setMax(16384);
        col.setWidth(9.15);
        col.setStyle(unlockedCellStyle.getIndex());

        sheet.protectSheet("password");

        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("TEST");
        cell.setCellStyle(lockedCellStyle);

        FileOutputStream outputStream = new FileOutputStream("bla.xlsx");
        wb.write(outputStream);
        outputStream.close();
    }
}

抱歉,无法复制,apache poi 3.15最终版,Excel 2016。精确代码生成包含受保护工作表“Sheet0”的工作簿,但单元格
A1
可编辑。抱歉,无法复制,apache poi 3.15最终版,Excel 2016。精确您的代码生成包含受保护工作表“Sheet0”的工作簿,但单元格
A1
是可编辑的。@katrin krabbes:如果这解决了您的问题,请接受答案…@katrin krabbes:如果这解决了您的问题,请接受答案。。。