Breaks the DataLinks in an Excel WorkBook through C#
We use Excel Interop Library in .Net to write into Workbooks. We create Charts on the fly, and set its datasource to a DataSet, which would be on another Workbook. But, if you want to send only the Workbook with the final result, then Excel provides you an option to break the DataLinks.
On doing so, It creates the data for populating the charts or any other Excel object thus used.
Below is the code snippet in C# for doing the same.
/// <summary>
/// Breaks the DataLinks in a WorkBook
/// </summary>
/// <param name="wb"></param>
private void BreakDataLinks(Excel._Workbook wb)
{
if (DialogResult.Yes == MessageBox.Show(this,"Do you want to break the links to Data Sources ?", "Links",
MessageBoxButtons.YesNo))
{
Array links = (Array)wb.LinkSources(Excel.XlLink.xlExcelLinks);
if (links != null)
{
for (int i = 1; i <= links.Length; i++)
{
wb.BreakLink((string)links.GetValue(i),
Excel.XlLinkType.xlLinkTypeExcelLinks);
}
}
}
}
Please note:
You also need to supress the Excel Question that pop's up when you are doing the Link Break.
//TO Supress the Update Q
oXL.DisplayAlerts = false;oXL.AskToUpdateLinks =
false;
where "oXL" is the Excel Application Object defined as follows:
//Excel Application ObjectExcel.Application oXL;
oXL =
new Excel.Application();
By [)ia6l0 iii Popularity (1058 Views)