ExcelWriter.Style CellLocked - 为什么它不起作用?
我正在尝试使用 #officewriter #excelwriter 创建一个简单的示例,但在使用样式时遇到问题.不知道为什么.
I'm trying to create a simple example with #officewriter #excelwriter and having trouble using the styles. Not sure why.
ExcelApplication XLAPP = new ExcelApplication();
Workbook WB = XLAPP.Create();
Worksheet WKST = WB.Worksheets[0];
DataSet ds = <...some valid dataset with 2 columns....>
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 样式不起作用.为什么?
Here's the problem: the color style works in the output but the CellLocked style does not. Why?
感谢您的帮助.沮丧 - 我以为这是一个简单的例子!
Thanks for any help. Frustrated - I thought this was a simple example!
ExcelWriter 旨在模仿 Excel,因此通常您需要在 Excel 中执行以激活某些属性的步骤与您需要在 ExcelWriter 中执行的步骤相同.
ExcelWriter is designed to mimic Excel, so usually the steps that you need to take in Excel to active certain properties are the same steps that you need to take in ExcelWriter.
在 Excel 中,工作表中的所有单元格都设置了 locked 样式属性,但该属性在工作表受到保护之前不会生效.(右键单击单元格并转到设置单元格格式 > 保护 - Locked 属性已选中,但在转到查看 > 保护之前,单元格不会被锁定表).
In Excel, all the cells in a worksheet have the locked style property set, but that property will not take effect until the worksheet is protected. (Right click on a cell and go to Format Cells > Protection - the Locked property is checked, but the cells are not locked until you go to Review > Protect Sheet).
同样,在 ExcelWriter 中,Worksheet.Cells 中的所有单元格都有Style.CellLocked 默认设置为 true,但该属性在 Worksheet.Protect() 之前不会生效叫.保护工作表后,应锁定单元格.
Similarly, in ExcelWriter, all the cells in Worksheet.Cells have the Style.CellLocked set to true by default, but that property will not take effect until Worksheet.Protect() is called. Once you protect the sheet, the cells should be locked.
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> 在您想要保持解锁状态的任何单元格上.当工作表受到保护时,这些单元格将保持可编辑状态.
Since all the cells default to Locked, if you want to lock only an area of cells and not the entire worksheet, you will need to set Style.CellLocked to false on any cells that you want to leave unlocked. When the worksheet is protected, these cells will remain editable.
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");
有关保护工作表的更多信息,我们的文档中有一个指南:保护您的工作表.
For more about protecting worksheets, we have a guide in our documentation: Protecting Your Worksheet.
注意:我为 OfficeWriter 的制造商 SoftArtisans 工作.