Wiki

Clone wiki

Aspose for OpenXML / Reading Worksheet Cell Value Using OpenXML SDK

Reading Worksheet Cell Value

Using OpenXML SDK

#!c#

public string XLGetCellValue(string fileName, 
  string sheetName, string addressName)
{
  //  Return the value of the specified cell.
  const string documentRelationshipType = 
    "http://schemas.openxmlformats.org/officeDocument/" +
    "2006/relationships/officeDocument";
  const string worksheetSchema = 
    "http://schemas.openxmlformats.org/spreadsheetml/2006/main";
  const string sharedStringsRelationshipType = 
    "http://schemas.openxmlformats.org/officeDocument/" + 
    "2006/relationships/sharedStrings";
  const string sharedStringSchema = 
    "http://schemas.openxmlformats.org/spreadsheetml/2006/main";

  string cellValue = null;

  //  Retrieve the stream containing the requested
  //  worksheet's info:
  using (Package xlPackage = Package.Open(
    fileName, FileMode.Open, FileAccess.Read))
  {
    PackagePart documentPart = null;
    Uri documentUri = null;

    //  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;
    }

    if (documentPart != null)
    {
      // Load the contents of the workbook.
      XmlDocument doc = new XmlDocument();
      doc.Load(documentPart.GetStream());

      //  Create a namespace manager, so you can search.
      //  Add a prefix (d) for the default namespace.
      NameTable nt = new NameTable();
      XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
      nsManager.AddNamespace("d", worksheetSchema);
      nsManager.AddNamespace("s", sharedStringSchema);

      // Next code block goes here.
  return cellValue;
}

Using Aspose SDK

#!c#

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aspose.Cells;

namespace Worksheets
{
    class Program
    {
        static string projectFiles = null;

        static void Main(string[] args)
        {
            projectFiles = Path.GetFullPath("../../Files/");
            addWorkSheet();
            addRows();
            deleteRows();
            insertCols();
            deleteCols();
            accessCell();

        }


        static void accessCell()
        {
            Workbook workbook = new Workbook(projectFiles + "TestBook.xlsx");
            //Accessing the first worksheet in the Excel file
            Worksheet worksheet = workbook.Worksheets[0];

            //Accessing a cell using its name
            Cell cell = worksheet.Cells["A1"];

            //Accessing a cell using the indices of its row and column
            Cell celli = worksheet.Cells[0, 0];
        }

        static void insertCols()
        {
            Workbook workbook = new Workbook(projectFiles + "TestBook.xlsx");

            //Accessing the first worksheet in the Excel file
            Worksheet worksheet = workbook.Worksheets[0];

            //Inserting 10 Cols into the worksheet starting from 3rd col
            worksheet.Cells.InsertColumns(2, 10);

            //Saving the modified Excel file
            workbook.Save(projectFiles + "output.xls");
        }

        static void deleteCols()
        {
            Workbook workbook = new Workbook(projectFiles + "TestBook.xlsx");

            //Accessing the first worksheet in the Excel file
            Worksheet worksheet = workbook.Worksheets[0];

            //Delete 10 Cols from the worksheet starting from 3rd col
            worksheet.Cells.DeleteColumns(2, 10, true);

            //Saving the modified Excel file
            workbook.Save(projectFiles + "output.xls");
        }

        static void addRows()
        {
            Workbook workbook = new Workbook(projectFiles + "TestBook.xlsx");

            //Accessing the first worksheet in the Excel file
            Worksheet worksheet = workbook.Worksheets[0];

            //Inserting 10 rows into the worksheet starting from 3rd row
            worksheet.Cells.InsertRows(2,10);

            //Saving the modified Excel file
            workbook.Save(projectFiles + "output.xls");
        }

        static void deleteRows()
        {
            Workbook workbook = new Workbook(projectFiles + "TestBook.xlsx");

            //Accessing the first worksheet in the Excel file
            Worksheet worksheet = workbook.Worksheets[0];

            //Delete 10 rows from the worksheet starting from 3rd row
            worksheet.Cells.DeleteRows(2, 10);

            //Saving the modified Excel file
            workbook.Save(projectFiles + "output.xls");
        }

        static void addWorkSheet()
        {
            Workbook workbook = new Workbook(projectFiles + "TestBook.xlsx");

            //Adding a new worksheet to the Workbook object
            int i = workbook.Worksheets.Add();

            //Obtaining the reference of the newly added worksheet by passing its sheet index
            Worksheet worksheet = workbook.Worksheets[i];

            //Setting the name of the newly added worksheet
            worksheet.Name = "My Worksheet";

            //Saving the Excel file
            workbook.Save(projectFiles + "output.xls");

        }
    }
}
Source: https://bitbucket.org/asposemarketplace/aspose-for-openxml/src/ffec3cde9d87/Aspose.Cells/Worksheets/?at=master

Updated