SpreadsheetDocument


Nuget - DocumentFormat.OpenXml
The SDK is built on the System.IO.Packaging API and provides strongly types classes to manipulate documents that adhere to the Office Open XML file format.
The SDK supports .NET 3.5 and .NET Core 2.0.

link - ericwhite.com/blog/introduction-to-open-xml-series 

DocumentFormat.OpenXml.Packaging.SpreadsheetDocument 
DocumentFormat.OpenXml.Packaging.WorkbookPart
DocumentFormat.OpenXml.Packaging.WorksheetPart

DocumentFormat.OpenXml.Packaging.Worksheet
DocumentFormat.OpenXml.Packaging.Row
DocumentFormat.OpenXml.Packaging.Cell
DocumentFormat.OpenXml.Packaging.SheetData

DocumentFormat.OpenXml.Office2010.CustomUI
DocumentFormat.OpenXml.Office2010.Excel
DocumentFormat.OpenXml.Office2013.Excel

Create a new Workbook with one worksheet

using DocumentFormat.OpenXml; 
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

static void CreateSpreadsheetWorkbook(string filepath)
{
    SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);

    WorkbookPart workbookPart = spreadsheetDocument.AddWorkbookPart();
    workbookPart.Workbook = new Workbook();

    WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
    worksheetPart.Worksheet = new Worksheet(new SheetData());

    Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());

    Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" };
    sheets.Append(sheet);

    workbookPart.Workbook.Save();

    spreadsheetDocument.Dispose();
}

Opens an Existing Workbook and Inserts a new worksheet

using DocumentFormat.OpenXml.Packaging; 
using DocumentFormat.OpenXml.Spreadsheet;
using System.Linq;

static void InsertWorksheet(string docName)
{
    using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true))
    {
        WorkbookPart workbookPart = spreadSheet.WorkbookPart ?? spreadSheet.AddWorkbookPart();

        WorksheetPart newWorksheetPart = workbookPart.AddNewPart<WorksheetPart>();
        newWorksheetPart.Worksheet = new Worksheet(new SheetData());

        Sheets sheets = workbookPart.Workbook.GetFirstChild<Sheets>() ?? workbookPart.Workbook.AppendChild(new Sheets());
        string relationshipId = workbookPart.GetIdOfPart(newWorksheetPart);

        uint sheetId = 1;
        if (sheets.Elements<Sheet>().Count() > 0)
        {
            sheetId = (sheets.Elements<Sheet>().Select(s => s.SheetId?.Value).Max() + 1) ?? (uint)sheets.Elements<Sheet>().Count() + 1;
        }

        string sheetName = "Sheet" + sheetId;

        Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
        sheets.Append(sheet);
    }
}

Console App that checks the content of a file

using DocumentFormat.OpenXml.Packaging; 
using DocumentFormat.OpenXml.Spreadsheet;
using System;

class MyConsole
{
   static void Main(string[] args)
   {
   ReadExcelFile(args[0]);
   }

   static void ReadExcelFile(string filename)
   {
      try
      {
         using (SpreadsheetDocument xSD = SpreadsheetDocument.Open(filename, false))
         {
            WorkbookPart xWBP = xSD.WorkbookPart ?? xSD.AddWorkbookPart();
            string text;

            foreach (Worksheet xWSP in xWBP.WorksheetParts)
            {
               Worksheet xwsh = xWSP.Worksheet;

               foreach(SheetData xSheetData in xwsh.Elements<SheetData>())
               {
                  foreach(Row xRow in xSheetData.Elements())
                  {
                     foreach(Cell xCell in xRow.Elements<Cell>())
                     {

                        if (xCell.CellFormula != null)
                        {
                           text = xCell.CellFormula.InnerText;

                           if (text.contains("AVERAGE") == true)
                           {
                             Console.WriteLine(@"""" + filename + @"""&&""" + text + @"""");
                           }
                           else
                           {
                           }
                        }

                      }
                   }
                }

             }
         }
         return;
      }
      catch (Exception ex)
      {
         Console.WriteLine(@"""" + filename + @"""&&""" + @"""ERROR""");
         return;
      }
   }
}


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext