Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created xlsx format from dataset, It is working fine, But i need to protect password when we open xlsx file in destination path.

i have placed my code below, kindly let me know where should i do modification.

kindly help me out, how to code for password protect.

What I have tried:

using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Packaging;  

using (var workbook = SpreadsheetDocument.Create(XLSXCreationPath, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
                {
                    var workbookPart = workbook.AddWorkbookPart();
                    workbook.WorkbookPart.Workbook = new Workbook();
                    workbook.WorkbookPart.Workbook.Sheets = new Sheets();
                    var lines = new List<string>();
                    foreach (System.Data.DataTable table in ds.Tables)
                    {
                        var sheetPart = workbook.WorkbookPart.AddNewPart<WorksheetPart>();
                        var sheetData = new SheetData();
                        sheetPart.Worksheet = new Worksheet(sheetData);
                        Sheets sheets = workbook.WorkbookPart.Workbook.GetFirstChild<Sheets>();
                        string relationshipId = workbook.WorkbookPart.GetIdOfPart(sheetPart);
                        uint sheetId = 1;
                        if (sheets.Elements<Sheet>().Count() > 0)
                        {
                            sheetId =
                                sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
                        }
                        Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = table.TableName };
                        sheets.Append(sheet);
                        Row headerRow = new Row();
                        List<String> columns = new List<string>();
                        foreach (System.Data.DataColumn column in table.Columns)
                        {
                            columns.Add(column.ColumnName);
                            Cell cell = new Cell();
                            cell.DataType = CellValues.String;
                            cell.CellValue = new CellValue(column.ColumnName);
                            headerRow.AppendChild(cell);
                        }
                        sheetData.AppendChild(headerRow);
                        foreach (System.Data.DataRow dsrow in table.Rows)
                        {
                            Row newRow = new Row();
                            foreach (String col in columns)
                            {
                                Cell cell = new Cell();
                                cell.DataType = CellValues.String;
                                cell.CellValue = new CellValue(dsrow[col].ToString()); 
                                newRow.AppendChild(cell);
                            }
                            sheetData.AppendChild(newRow);
                        }
                    }
Posted
Updated 17-Oct-17 10:12am
v2

1 solution

Check the documentation: _Worksheet.Protect method (Microsoft.Office.Interop.Excel)[^].

[edit]
As Richard Deeming points out, your question is for OpenXML, but the suggestion is the same, check the documentation.
[/edit]
 
Share this answer
 
v2
Comments
Richard Deeming 17-Oct-17 13:32pm    
That link's for Interop. It looks like the OP is using the OpenXML SDK.
Richard MacCutchan 17-Oct-17 13:55pm    
Ah, well spotted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900