Quantcast
Channel: Open XML Format SDK forum
Viewing all articles
Browse latest Browse all 1288

Create hyperlink and populating it in excel sheet using Open XML sdk

$
0
0

I am trying to populate hyperlink in excel sheet using Open Office xml sdk.

Description of Excel sheet:

The excel sheet contains 3 tables –

  • Table 1
  • Table 2
  • Table 3

Table 1 and Table 3 already contains some data along with hyperlinks. Now I want to populate hyperlink and other data in table 2 of excel sheet.

I am using following code, but getting following error while opening the excel sheet:

We found a problem with some content in 'Sheet.xlsx'. Do you want us to try to recover as much as we can?If you trust the source of this workbook, click Yes.

After click on Yes, getting another error:

Excel was able to open the file by repairing or removing the unreadable content.

And after closing this error message, sheet data gets empty

I am using following code:

//Method to sheet data and hyperlink        
private bool PopulateHyperlink(SpreadsheetDocument myWorkbook)
        {
            #region Variables
            bool success = false;            
            Cell toolcelloffering = null;
            Cell toolcellvendor = null;
            Cell toolcellname = null;
            Cell toolcellfunction = null;
            Sheet sheet = null;
            WorksheetPart worksheetPart = null;
            string columnname = string.Empty;
            string toolUrl = string.Empty;
            int i = 1;
            Row row;
            DocumentFormat.OpenXml.UInt32Value sheetId;
            SheetData sheetData;
            var toolOffering = string.Empty;
            List<Tool> toolList = null;
            Hyperlinks hyperlinks1 = new Hyperlinks();
            //Get these values from web.config
            int ToolsColumn=2;
            int ToolsRow=17;
            int ToolsColumnValue=2;
            #endregion
            try
            {
                sheet = myWorkbook.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>().First(s => s.Name.Value.Equals("SheetName", StringComparison.OrdinalIgnoreCase));
                worksheetPart = (WorksheetPart)myWorkbook.WorkbookPart.GetPartById(sheet.Id.Value);
                sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();                
                if (Project.Tools != null)
                {
                    //removing duplicate records from Tools list
                    toolList = Project.Tools.GroupBy(p => p.Name).Select(g => g.First()).ToList();
                    //Loop through the Tools of the project
                    foreach (Tool tool in toolList)
                    {                           
                        toolUrl = tool.URL;                           
                        columnname = GetColumnName(ToolsColumn);
                        toolcellname = GetCellInWorksheet(columnname, Convert.ToUInt32(17), worksheetPart);
                        toolcellname.CellValue = new CellValue(tool.Name.Replace("amp;", string.Empty));
                        toolcellname.DataType = new EnumValue<CellValues>(CellValues.String);
                        toolcellname.StyleIndex = 130;
                        if (!string.IsNullOrEmpty(toolUrl))
                        {
                            toolcellname = CreateHyperLink(myWorkbook, worksheetPart, toolUrl, columnname, toolcellname, i, hyperlinks1, ToolsRow, tool.Name);
                            columnname = string.Empty;
                        }
                        ToolsColumn++;
                        ToolsColumn = ToolsColumnValue;
                        //Increment the row to add the next tool information.
                        ToolsRow++;
                        i++;
                    }
                    PageMargins pageMargins = worksheetPart.Worksheet.Descendants<PageMargins>().First();
                    worksheetPart.Worksheet.InsertBefore<Hyperlinks>(hyperlinks1, pageMargins);
                    worksheetPart.Worksheet.Save();
                }
                success = true;
            }
            catch (Exception ex)
            {
                throw new Exception("Exception while populating the information. " + ex.Message);
            }
            return success;
        }
        //Method to create hyperlink
        private Cell CreateHyperLink(SpreadsheetDocument myWorkbook, WorksheetPart worksheetPart, string url, string columnname, Cell cellName, int i, Hyperlinks hyperlinks, int row, string name)
        {
            Hyperlink hyperlink = new Hyperlink();
            hyperlink.Reference = columnname + row;
            hyperlink.Id = "UNIQUE" + i;
            hyperlinks.Append(hyperlink);
            worksheetPart.AddHyperlinkRelationship(new System.Uri(url, System.UriKind.Absolute), true, hyperlink.Id);
            // Create an element in SheetData
            // Get the SharedStringTablePart. If it does not exist, create a new one.
            SharedStringTablePart shareStringPart;
            if (myWorkbook.WorkbookPart.GetPartsOfType<SharedStringTablePart>().Count() > 0)
            {
                shareStringPart = myWorkbook.WorkbookPart.GetPartsOfType<SharedStringTablePart>().First();
            }
            else
            {
                shareStringPart = myWorkbook.WorkbookPart.AddNewPart<SharedStringTablePart>();
            }
            // Insert the text into the SharedStringTablePart.
            int index = InsertSharedStringItem(name.Replace("amp;", string.Empty), shareStringPart);            
            cellName = GetCellInWorksheet(columnname, Convert.ToUInt32(row), worksheetPart);
            cellName.CellValue = new CellValue(index.ToString());
            cellName.DataType = new EnumValue<CellValues>(CellValues.SharedString);
            cellName.StyleIndex = 134;
            return cellName;
        }
        private Cell GetCellInWorksheet(string columnName, uint rowIndex, WorksheetPart worksheetPart)
        {
            Worksheet worksheet = worksheetPart.Worksheet;
            SheetData sheetData = worksheet.GetFirstChild<SheetData>();
            string cellReference = columnName + rowIndex;
            // If the worksheet does not contain a row with the specified row index, insert one.
            Row row;
            if (sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).Count() != 0)
            {
                row = sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).First();
            }
            else
            {
                row = new Row() { RowIndex = rowIndex };
                sheetData.Append(row);
            }
            // If there is not a cell with the specified column name, insert one.  
            if (row.Elements<Cell>().Where(c => c.CellReference.Value == columnName + rowIndex).Count() > 0)
            {
                return row.Elements<Cell>().Where(c => c.CellReference.Value == cellReference).First();
            }
            else
            {
                // Cells must be in sequential order according to CellReference. Determine where to insert the new cell.
                Cell refCell = null;
                foreach (Cell cell in row.Elements<Cell>())
                {
                    if (string.Compare(cell.CellReference.Value, cellReference, true) > 0)
                    {
                        refCell = cell;
                        break;
                    }
                }
                Cell newCell = new Cell() { CellReference = cellReference };
                row.InsertBefore(newCell, refCell);
                worksheet.Save();
                return newCell;
            }
        }
        private string GetColumnName(int columnNumber)
        {
            StringBuilder retVal = new StringBuilder();
            int x = 0;
            for (int n = (int)(Math.Log(25 * (columnNumber + 1)) / Math.Log(26)) - 1; n >= 0; n--)
            {
                x = (int)((Math.Pow(26, (n + 1)) - 1) / 25 - 1);
                if (columnNumber > x)
                    retVal.Append(System.Convert.ToChar((int)(((columnNumber - x - 1) / Math.Pow(26, n)) % 26 + 65)));
            }
            return retVal.ToString();
        }

Then I made changes in excel sheet.

Removed hyperlink from Table 1 and Table 3 and then again tried to populate data and hyperlink in table 2 of excel sheet. And the code works.

Please suggest how I can populate hyperlinks in table 2 of excel sheet without removing hyperlinks from table 1 and table 3.

I have already referred following suggestion from MSDN forum:

http://social.msdn.microsoft.com/Forums/sqlserver/en-US/4651b349-ab62-404b-842a-1a19926f380c/create-a-hyperlink-to-an-existing-cell-to-another-sheet-using-open-xml-sdk?forum=oxmlsdk

Thank you


Prince Jain




Viewing all articles
Browse latest Browse all 1288

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>