A community member has associated this post with a similar question:
DocumentFormat.OpenXml.Spreadsheet while copying from existing excel apply validation if existing excel has hyperlinks download file not opened

Only moderators can edit this content.

DocumentFormat.OpenXml.Spreadsheet while copying from existing excel apply validation if existing excel has hyperlinks and datavalidation download file not opened

Hari Krishna 1 Reputation point
2022-08-08T18:10:31.32+00:00

229265-yn7wu.png

string MyFile = @"C:\Users\Desktop\temp\sample.xlsm";
using (SpreadsheetDocument myDoc = SpreadsheetDocument.Open(MyFile, true))
{
WorkbookPart wbPart = myDoc.WorkbookPart;
Hyperlinks hyperlinks1 = new Hyperlinks();
//var sheetData = new DocumentFormat.OpenXml.Spreadsheet.SheetData();
WorksheetPart worksheetPart = GetWorksheetPartByName(myDoc, "Test");

            DataValidations dataValidations = new DataValidations();  
            DataValidation dataValidation = new DataValidation()  
            {  
                Type = DataValidationValues.List,  
                AllowBlank = true,  
                SequenceOfReferences = new ListValue<StringValue>() { InnerText = "A1:A1048576" }  
            };  
            Formula1 formula = new Formula1();  
            formula.Text = "\"FirstChoice,SecondChoice,ThirdChoice\"";  

            dataValidation.Append(formula);  
            dataValidations.Append(dataValidation);  

            //insert the dataValidations to the correct place  
            worksheetPart.Worksheet.InsertBefore(dataValidations, worksheetPart.Worksheet.Descendants<PageMargins>().FirstOrDefault());  
        }  
        Console.WriteLine("File Saved to " + MyFile);  
Microsoft 365 and Office | Excel | For business | Windows
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes