Excel标题不可编辑的poi

Excel标题不可编辑的poi,excel,apache-poi,Excel,Apache Poi,我想使用poi使excel的标题行不可编辑 我在互联网上找到了各种各样的解决方案,首先做sheet.protectSheet(“密码”),最终使整个工作表不可编辑,然后遍历所有可编辑的单元格,并将它们的cellStyle设置为cellStyle.setLocked(false) 在我的例子中,因为excel只包含标题,其余的行将由用户填充,所以我不能使整个工作表不可编辑,我只希望用户不可编辑标题。如何实现这一点?使用XSSF可以实现以下目标: 将CellStylehavingsetLockedf

我想使用poi使excel的标题行不可编辑

我在互联网上找到了各种各样的解决方案,首先做
sheet.protectSheet(“密码”)
,最终使整个工作表不可编辑,然后遍历所有可编辑的单元格,并将它们的cellStyle设置为
cellStyle.setLocked(false)


在我的例子中,因为excel只包含标题,其余的行将由用户填充,所以我不能使整个工作表不可编辑,我只希望用户不可编辑标题。如何实现这一点?

使用
XSSF
可以实现以下目标:

CellStyle
having
setLocked
false设置为所有列的默认样式。这可以通过设置一个
org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol
元素来实现,该元素的最小列1和最大列16384设置了该样式

然后通过为该行设置
CustomFormat
true,使第1行不再使用该样式。因此,并非所有列都使用默认样式。另外,将
CellStyle
设置为
setLocked
true作为该行的默认样式。这可以通过从该行获取
org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow
元素并在其中设置
CustomFormat
S
(样式)来实现

结果:除第1行外,所有单元格均解锁

例如:

import java.io.*;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

public class CreateExcelSheetProtectOnlyFirstRow {

 public static void main(String[] args) throws Exception {
  Workbook workbook = new XSSFWorkbook();

  //create a CellStyle having setLocked false
  CellStyle cellstyleUnprotect = workbook.createCellStyle();
  cellstyleUnprotect.setLocked(false);
  //create a CellStyle having setLocked true
  CellStyle cellstyleProtect = workbook.createCellStyle();
  cellstyleProtect.setLocked(true);

  Sheet sheet = workbook.createSheet("Sheet1");

  //set the CellStyle having setLocked false as the default style for all columns
  org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol cTCol = 
      ((XSSFSheet)sheet).getCTWorksheet().getColsArray(0).addNewCol();
  cTCol.setMin(1);
  cTCol.setMax(16384);
  cTCol.setWidth(12.7109375);
  cTCol.setStyle(cellstyleUnprotect.getIndex());

  Row row = sheet.createRow(0);

  //set CustomFormat true for that row
  //so it does not using the default style for all columns
  //and set the CellStyle having setLocked true as the default style for that row
  org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow cTRow = 
      ((XSSFRow)row).getCTRow();
  cTRow.setCustomFormat(true);
  cTRow.setS(cellstyleProtect.getIndex());

  for (int c = 0; c < 3; c++) {
   row.createCell(c).setCellValue("Header " + (c+1));
  }

  sheet.protectSheet("password");   // protect sheet

  workbook.write(new FileOutputStream("CreateExcelSheetProtectOnlyFirstRow.xlsx"));
  workbook.close();
 }
}
import java.io.*;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.xssf.usermodel.*;
公共类CreateExcelSheetProtectOnlyFirstRow{
公共静态void main(字符串[]args)引发异常{
工作簿=新的XSSF工作簿();
//创建setLocked为false的单元格样式
CellStyle cellstyleUnprotect=工作簿.createCellStyle();
cellstyleUnprotect.setLocked(false);
//创建setLocked为true的单元格样式
CellStyle cellstyleProtect=workbook.createCellStyle();
cellstyleProtect.setLocked(true);
工作表=工作簿。创建工作表(“工作表1”);
//将setLocked为false的CellStyle设置为所有列的默认样式
org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol-CTCol=
((XSSFSheet)sheet.getCTWorksheet().getColsArray(0.addNewCol());
cTCol.setMin(1);
cTCol.setMax(16384);
cTCol.setWidth(12.7109375);
cTCol.setStyle(cellstyleUnprotect.getIndex());
Row Row=sheet.createRow(0);
//将该行的CustomFormat设置为true
//因此,它不会对所有列使用默认样式
//并将setLocked为true的CellStyle设置为该行的默认样式
org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow CTRow=
((XSSFRow)row.getCTRow();
cTRow.setCustomFormat(true);
setS(cellstyleProtect.getIndex());
对于(int c=0;c<3;c++){
row.createCell(c).setCellValue(“头”+(c+1));
}
sheet.protectSheet(“密码”);//保护工作表
write(新文件输出流(“CreateExcelSheetProtectOnlyFirstRow.xlsx”);
workbook.close();
}
}

“我只希望用户不可编辑标题。”:那么您将如何使用
Excel
s GUI来满足这一要求?因为
apache-poi
不能做
Excel
本身不能做的事情。@Fabien感谢您的编辑:)