ExternalLinkCollection.Add

Add(string, string[])

Adds an external link.

public int Add(string fileName, string[] sheetNames)
ParameterTypeDescription
fileNameStringThe external file name.
sheetNamesString[]All sheet names of the external file.

Return Value

The position of the external name in this list.

Examples

namespace AsposeCellsExamples
{
    using Aspose.Cells;
    using System;

    public class ExternalLinkCollectionMethodAddWithStringStringArrayDemo
    {
        public static void Run()
        {
            // Create a new workbook
            Workbook workbook = new Workbook();
            
            // Get the external links collection
            ExternalLinkCollection externalLinks = workbook.Worksheets.ExternalLinks;
            
            // Prepare parameters for Add method
            string fileName = "ExternalWorkbook.xlsx";
            string[] sheetNames = new string[] { "Sheet1", "Sheet2" };

            try
            {
                // Call the Add method with (String, String[]) parameters
                int index = externalLinks.Add(fileName, sheetNames);
                
                Console.WriteLine($"External link added at index: {index}");
                Console.WriteLine($"Total external links: {externalLinks.Count}");

                // Display added external link information
                ExternalLink link = externalLinks[index];
                Console.WriteLine($"External link file: {link.DataSource}");
                Console.WriteLine("Referenced sheets:");
                foreach (string sheet in sheetNames)
                {
                    Console.WriteLine($"- {sheet}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error executing Add method: {ex.Message}");
            }
            
            // Save the workbook
            workbook.Save("ExternalLinkCollectionAddDemo.xlsx");
        }
    }
}

See Also


Add(DirectoryType, string, string[])

Add an external link .

public int Add(DirectoryType directoryType, string fileName, string[] sheetNames)
ParameterTypeDescription
directoryTypeDirectoryTypeThe directory type of the file name.
fileNameStringthe file name.
sheetNamesString[]All sheet names of the external file.

Return Value

The position of the external name in this list.

Examples

namespace AsposeCellsExamples
{
    using Aspose.Cells;
    using System;

    public class ExternalLinkCollectionMethodAddWithDirectoryTypeStringStringDemo
    {
        public static void Run()
        {
            // Create a new workbook
            Workbook workbook = new Workbook();
            
            // Get the external links collection
            ExternalLinkCollection externalLinks = workbook.Worksheets.ExternalLinks;
            
            // Prepare parameters for Add method
            DirectoryType directoryType = DirectoryType.Volume; // Changed from Directory to Volume
            string fileName = "ExternalWorkbook.xlsx";
            string[] sheetNames = new string[] { "Sheet1", "Sheet2" };

            try
            {
                // Call the Add method with (DirectoryType, String, String[]) parameters
                int index = externalLinks.Add(directoryType, fileName, sheetNames);
                
                Console.WriteLine($"External link added at index: {index}");
                Console.WriteLine($"Total external links: {externalLinks.Count}");
                
                // Display information about the added external link
                ExternalLink link = externalLinks[index];
                Console.WriteLine($"External link references: {link.DataSource}");
                foreach (string sheet in sheetNames) // Changed from link.SheetNames to sheetNames
                {
                    Console.WriteLine($" - References sheet: {sheet}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error executing Add method: {ex.Message}");
            }
            
            // Save the workbook
            workbook.Save("ExternalLinkCollectionAddDemo.xlsx");
        }
    }
}

See Also