NPOI 匯出excel 設定指定列唯讀,不能修改

2021-08-29 15:46:13 字數 838 閱讀 4521

最近在做乙個匯入匯出的功能,匯出後,使用者修改部分列的值,然後匯入更新。為了能夠進行更新,必須對某些列進行限制修改。

用的是npoi控制項,hssfworkbook.getsheetat(0).protectsheet("密碼") 進行上鎖。

問題來了,單純用protectsheet並不行,還得設定單元格樣式islocked

iworkbook hssfworkbook = new hssfworkbook(file);

//鎖定樣式

icellstyle cellstylelock = hssfworkbook .createcellstyle();

cellstylelock .islocked = true;//上鎖

//解除鎖定樣式

icellstyle cellstyleunlock = hssfworkbook .createcellstyle();

cellstyleunlock.islocked = false;//解鎖

設定好樣式後,然後再遍歷單元格的時候,賦值樣式

icell cellitem = row.getcell(index);//獲取行內單元格

if (cellitem == null) cellitem = row.createcell(index);//不存在就建立單元格

不允許修改,設定樣式:

cellitem.cellstyle = cellstylelock;//鎖定

允許修改,設定樣式:

cellitem.cellstyle = cellstyleunlock ;//接觸鎖定

NPOI高效匯出Excel

using system.collections.generic using system.data using system.io using system.linq using npoi.hssf.usermodel using npoi.ss.usermodel using npoi.xssf...

NPOI匯出EXCEL樣式

public void export datarequestrequest,dataresponseresponse endregion region 單元格列值屬性樣式 for int jj 0 jj row1.cells.count jj style endregion cell.setcell...

npoi匯入匯出excel

1.匯入 將datatable資料匯入到excel中 要匯入的資料 datatable的列名是否要匯入 要匯入的excel的sheet的名稱 匯入資料行數 包含列名那一行 public int datatabletoexcel datatable data,string sheetname,bool...