Wiki

Clone wiki

Aspose for OpenXML / Insert Value in Cell Using OpenXML SDK

Insert Value in Cell

Using OpenXML SDK

#!c#

public bool XLInsertNumberIntoCell(string fileName, 
  string sheetName, string addressName, int value)
{
  //  Given a file, a sheet, and a cell, insert a specified value.
  //  For example: InsertNumberIntoCell("C:\Test.xlsx", "Sheet3", "C3", 14)

  const string documentRelationshipType = 
    "http://schemas.openxmlformats.org/officeDocument/2006/ + 
    "relationships/officeDocument";
  const string worksheetSchema = 
    "http://schemas.openxmlformats.org/spreadsheetml/2006/main";

  //  Retrieve the stream containing the requested
  //  worksheet's info:
  PackagePart documentPart = null;
  Uri documentUri = null;
  bool returnValue = false;

  using (Package xlPackage = 
    Package.Open(fileName, FileMode.Open, FileAccess.ReadWrite))
  {
    //  Get the main document part (workbook.xml).
    foreach (System.IO.Packaging.PackageRelationship relationship 
      in xlPackage.GetRelationshipsByType(documentRelationshipType))
    {
      //  There should only be one document part in the package. 
      documentUri = PackUriHelper.ResolvePartUri(new Uri("/", 
        UriKind.Relative), relationship.TargetUri);
      documentPart = xlPackage.GetPart(documentUri);

      //  There should only be one instance, 
      // but get out no matter what.
      break;
    }

    // Code removed here.

    return returnValue;
  }
}

Updated