ExcelWriter.Style CellLocked – 为什么它不起作用?

我正在尝试使用#officewriter #excelwriter创建一个简单的示例,并且无法使用这些样式。 不知道为什么。

ExcelApplication XLAPP = new ExcelApplication(); Workbook WB = XLAPP.Create(); Worksheet WKST = WB.Worksheets[0]; DataSet ds =  DataView dv = ds.Tables[0].DefaultView; DataImportProperties props = WB.CreateDataImportProperties(); SoftArtisans.OfficeWriter.ExcelWriter.Style dataStyle = WB.CreateStyle(); props.UseColumnNames = true; dataStyle.BackgroundColor = Color.SystemColor.Red; dataStyle.CellLocked = true; Area importArea = WKST.ImportData(dv, WKST.Cells[0, 0],props); importArea.ApplyStyle(dataStyle); XLAPP.Save(WB, Page.Response, "Output.xls", false); 

这是问题所在:颜色样式在输出中起作用,但CellLocked样式不起作用。 为什么?

谢谢你的帮助。 沮丧 – 我认为这是一个简单的例子!

ExcelWriter旨在模仿Excel,因此通常需要在Excel中使用以激活某些属性的步骤与ExcelWriter中需要执行的步骤相同。

在Excel中,工作表中的所有单元格都设置了锁定样式属性,但该属性在工作表受到保护之前不会生效。 (右键单击一个单元格,然后转到“设置单元格格式”>“保护” – 选中“ 锁定”属性,但在转到“ 审阅”>“保护表”之前,单元格不会被锁定)。

同样,在ExcelWriter中,默认情况下, Worksheet.Cells中的所有单元格都将Style.CellLocked设置为true ,但在调用Worksheet.Protect()之前该属性不会生效。 保护纸张后,应锁定单元格。

 ExcelApplication xlapp = new ExcelApplication(); Workbook wb = xlapp.Create(ExcelApplication.FileFormat.Xlsx); Worksheet ws = wb.Worksheets["Sheet1"]; //Currently, all the cells have default "locked" style set to true //Protecting the worksheet will activate this property ws.Protect("MyPassword"); xlapp.Save(wb, "ProtectedWorksheet.xlsx"); 

由于所有单元格默认为Locked ,如果您只想锁定单元格区域而不是整个工作表,则需要在要解锁的任何单元格上将Style.CellLocked设置为false 。 当工作表受到保护时,这些单元格将保持可编辑状态。

 ExcelApplication xlapp = new ExcelApplication(); Workbook wb = xlapp.Open("MyWorkbook.xlsx"); Worksheet ws = wb.Worksheets[0]; //Create a style that will leave certain cells unlocked //when the worksheet is protected Style unlockStyle = wb.CreateStyle(); unlockStyle.CellLocked = false; //Select the area that will be left unprotected //Apply the style to that area Area unlockedArea = ws.PopulatedCells; unlockedArea.ApplyStyle(unlockStyle); //Protect the worksheet ws.Protect("MyPassword"); xlapp.Save(wb, "MyNewWorkbook.xlsx"); 

有关保护工作表的更多信息,我们在文档中提供了指南: 保护工作表 。

注意:我为OfficeWriter的制造商SoftArtisans工作。