只读或使用open xml sdk锁定特定的单元格或行 [英] Read only or Lock the particular cells or rows using open xml sdk

查看:175
本文介绍了只读或使用open xml sdk锁定特定的单元格或行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用open xml sdk来导出excel。我得到的excel文件,并将该文件复制到另一个地方,然后我插入新的行与数据。

I am using open xml sdk to export excel. I get the excel file and copying that file to another place and then I'm inserting new rows with the data.

我应该把这行作为只读(或我使用open xml sdk进行锁定。

I should make that rows as read only (or I've to lock) using open xml sdk..

如何做?

推荐答案

我已经引用了以下链接 Excel文件密码使用Open XML SDK进行保护

I've referred the following link Excel File Password Protection with Open XML SDK.

这里我给了你完整的代码,从目录打开文件并插入单元格。这里我只插入了一个单元格。您可以根据需要插入。这里我插入了锁定的单元格。找到这个,这对你来说非常有帮助..

Here I gave you full code to open the file from directory and insert cells. Here I've inserted only one cell. You can insert as you want. Here I've inserted locked cells. Find this one, it'll very helpful to you..

注意:这里我只提到了代码只读现有文件并插入行和单元格并锁定手动插入的单元格

谢谢。

// To read the xlsx file..
    Package spreadsheetPackage = Package.Open(destinationFile, FileMode.Open, FileAccess.ReadWrite);

// create a document
                    using (var document = SpreadsheetDocument.Open(spreadsheetPackage))
                    {

                        var workbookPart = document.WorkbookPart;
                        //var workbook = workbookPart.Workbook;

                        var sheet = workbookPart.Workbook.Descendants<Sheet>().FirstOrDefault();
                        Worksheet ws = ((WorksheetPart)(workbookPart.GetPartById(sheet.Id))).Worksheet;
                        SheetData sheetData = ws.GetFirstChild<SheetData>();

                        if (sheet == null || sheetData == null)
                            throw new Exception("No sheet found in the template file");

                        int rowIndex;

                        var worksheetPart = (WorksheetPart)workbookPart.GetPartById(sheet.Id);
                        var rows = worksheetPart.Worksheet.Descendants<Row>();
                        DocumentFormat.OpenXml.Spreadsheet.Cell cell = new Cell();

// Getting row index from the web config..
                        if (!int.TryParse(WebConfigurationManager.AppSettings["BasReportRowIndex"].ToString(), out rowIndex))
                            throw new Exception("Mention template row index in the configuration file");

                        // Create Cell format .. It's necessary to lock the cell
                        CellFormat lockFormat = new CellFormat() { ApplyProtection = true, Protection = new Protection() { Locked = true } };
                        WorkbookStylesPart sp = workbookPart.GetPartsOfType<WorkbookStylesPart>().FirstOrDefault();

                        if (sp == null)
                            sp = worksheetPart.AddNewPart<WorkbookStylesPart>();

                        sp.Stylesheet.CellFormats.AppendChild<CellFormat>(lockFormat);
                        sp.Stylesheet.CellFormats.Count = UInt32Value.FromUInt32((uint)sp.Stylesheet.CellFormats.ChildElements.Count);
                        sp.Stylesheet.Save();

                        foreach (WeekSummary summary in report.Summary)
                        {
                            DocumentFormat.OpenXml.Spreadsheet.Row row = new DocumentFormat.OpenXml.Spreadsheet.Row();

                            // Before insert Row we've to mention the index where the row must be inserted
                            row.RowIndex = (UInt32)rowIndex++;

                            row.AppendChild<Cell>(new Cell() { DataType = CellValues.String, CellValue = new CellValue(summary.name ?? ""), StyleIndex = 0 });

                            // Append the row to sheet data.. 
                            sheetData.AppendChild<Row>(row);
                        }
// Till the line It's only to create and insert row..

                        // Now we've to mention the sheet protection. It's necessary for the whole sheet. Then only cells will be locked
                        SheetProtection sheetProtection = new SheetProtection();
                        sheetProtection.Password = "CC";
                        // these are the "default" Excel settings when you do a normal protect
                        sheetProtection.Sheet = true;
                        sheetProtection.Objects = true;
                        sheetProtection.Scenarios = true;

                        // After the following lines, the cell will be locked...
                        bool bFound = false;
                        OpenXmlElement oxe = worksheetPart.Worksheet.FirstChild;
                        foreach (var child in worksheetPart.Worksheet.ChildElements)
                        {
                            // start with SheetData because it's a required child element
                            if (child is SheetData || child is SheetCalculationProperties)
                            {
                                oxe = child;
                                bFound = true;
                            }
                        }

                        if (bFound)
                            worksheetPart.Worksheet.InsertAfter(sheetProtection, oxe);
                        else
                            worksheetPart.Worksheet.PrependChild(sheetProtection);

                        worksheetPart.Worksheet.Save();
                    }

这篇关于只读或使用open xml sdk锁定特定的单元格或行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆