none
Shared String is missing in excel RRS feed

  • Question

  • Hi,

    I have generated the excel in DFT task(SSIS) and it has text and numeric fields. After generated the excel, i have to apply the style format for that excel using Openxml. When i apply number format for the numeric field, the datatype say its inlinestring. When i lookup the shared string for the excel cell  nothing found and it returns null.I am using below code to check the datatype and value.Could you please some one advise why it does not have shared string for the excel cell/workbook. Please note that if i open the excel by manually and save it with out making any changes, after that i could see shared string for the excel work book. its confusing me.

    public static string ChangeTextCellToNumber(string fileName,

                string sheetName,

                string addressName)

            {

                string value = null;

     

                // Open the spreadsheet document for read-only access.

                using (SpreadsheetDocument document =

                    SpreadsheetDocument.Open(fileName, true))

                {

                    

                    // Retrieve a reference to the workbook part.

                    WorkbookPart wbPart = document.WorkbookPart;

                    Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().

                      Where(s => s.Name == sheetName).FirstOrDefault();

     

                    // Throw an exception if there is no sheet.

                    if (theSheet == null)

                    {

                        throw new ArgumentException("sheetName");

                    }

     

                    // Retrieve a reference to the worksheet part.

                    WorksheetPart wsPart =

                        (WorksheetPart)(wbPart.GetPartById(theSheet.Id));

     

                    Cell theCell = wsPart.Worksheet.Descendants<Cell>().

                      Where(c => c.CellReference == addressName).FirstOrDefault();

                    if (theCell.InnerText.Length > 0)

                    {

                        value = theCell.InnerText;

     

                        if (theCell.DataType != null)

                        {

                            switch (theCell.DataType.Value)

                            {

                                case CellValues.InlineString:

     

                                   // For shared strings, look up the value in the

                                    // shared strings table.

                                    var stringTable =

                                        wbPart.GetPartsOfType<SharedStringTablePart>()

                                        .FirstOrDefault();

                                    if (stringTable != null)

                                    {

                                        value =

                                            stringTable.SharedStringTable

                                            .ElementAt(int.Parse(value)).InnerText;

                                        // Convert to number

                                        theCell.CellValue = new CellValue(value);

                                        theCell.DataType = new EnumValue<CellValues>(CellValues.Number);

                                       

                                        wsPart.Worksheet.Save();

                                    }

                                    break;

     

                                case CellValues.Boolean:

                                    switch (value)

                                    {

                                        case "0":

                                            value = "FALSE";

                                            break;

                                        default:

                                            value = "TRUE";

                                            break;

                                    }

                                    break;

                            }

                        }

                    }

                }

                return value;

            }

    Saturday, August 15, 2020 4:37 PM