none
CellValue Insert Prolbem RRS feed

  • Question

  • hi.

    I want Insert cellValue..

    but. Finally, the value disappears.

    Looking at the code below.

    If a dog was a 5 DefinedName be set only have four valuesI think Last Value  disappears.

    Finally, do not seem to set the value.

    Why do these problems....

    byte[] byteArray = File.ReadAllBytes("Book1.xlsx");
          
          memoryStream.Write(byteArray, 0, (int)byteArray.Length);
    
          document = SpreadsheetDocument.Open(memoryStream, true);
    
                List<DefinedName> definedNames = new List<DefinedName>();
    
          foreach (DefinedName define in document.WorkbookPart.Workbook.DefinedNames)
          {
    
            definedNames.Add(define);
    
            Cell cell = GetDefineTargetToCell(define.Text, document.WorkbookPart.Workbook.Sheets);
    
            if(cell != null)
            {
              cell.CellValue = new CellValue(define.Name);
              cell.DataType = new DocumentFormat.OpenXml.EnumValue<CellValues>(CellValues.SharedString);
            }
          }
    
    
        
          document.WorkbookPart.Workbook.Save();
          
          byte[] bytesinMemory = memoryStream.ToArray();
          FileStream outStream = File.OpenWrite("Book1.xlsx");
          outStream.Write(bytesinMemory, 0, bytesinMemory.Length);
          outStream.Close();
    

     

     

     

    Thursday, November 11, 2010 8:21 AM

Answers

  • I found the reason.

    need Sheet Save..

     

     foreach (DefinedName define in document.WorkbookPart.Workbook.DefinedNames)
          {
    
            definedNames.Add(define);
    
            Worksheet workSheet;
            Cell cell = GetDefineTargetToCell(define.Text, document.WorkbookPart.Workbook.Sheets, out workSheet);
    
            if(cell != null)
            {
              cell.CellValue = new CellValue(define.Name);
              cell.DataType = new DocumentFormat.OpenXml.EnumValue<CellValues>(CellValues.SharedString);
            }
           <strong> workSheet.Save();</strong>
          }
    
    • Proposed as answer by Bessie Zhao Friday, November 19, 2010 8:32 AM
    • Marked as answer by Bessie Zhao Friday, December 3, 2010 10:53 AM
    Friday, November 12, 2010 1:40 AM

All replies

  • Hi,

    Let me clarify :

    You have 5 DefinedNames in a workbook
    You loop through tem and create 5 cells but when you open the Workbook you only see 4 cell has been created.

    Am I correct?

    Thanks
    Ankush

     


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. Regards, Ankush Microsoft Online Community Support
    Thursday, November 11, 2010 5:44 PM
  • Hi.. Ankush

    Yes..

    Why Create 4cell...?

    I want to know why.

    Thanks.

    Friday, November 12, 2010 1:27 AM
  • I found the reason.

    need Sheet Save..

     

     foreach (DefinedName define in document.WorkbookPart.Workbook.DefinedNames)
          {
    
            definedNames.Add(define);
    
            Worksheet workSheet;
            Cell cell = GetDefineTargetToCell(define.Text, document.WorkbookPart.Workbook.Sheets, out workSheet);
    
            if(cell != null)
            {
              cell.CellValue = new CellValue(define.Name);
              cell.DataType = new DocumentFormat.OpenXml.EnumValue<CellValues>(CellValues.SharedString);
            }
           <strong> workSheet.Save();</strong>
          }
    
    • Proposed as answer by Bessie Zhao Friday, November 19, 2010 8:32 AM
    • Marked as answer by Bessie Zhao Friday, December 3, 2010 10:53 AM
    Friday, November 12, 2010 1:40 AM