Skip to content

Instantly share code, notes, and snippets.

@DejanMilicic
Created January 22, 2021 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DejanMilicic/64b4be7fea04b70ab3063960764442cf to your computer and use it in GitHub Desktop.
Save DejanMilicic/64b4be7fea04b70ab3063960764442cf to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.IO;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Wordprocessing;
public static class Office
{
public static IEnumerable<string> GetExcelText(Stream stream)
{
using var doc = SpreadsheetDocument.Open(stream, false);
var sst = doc.WorkbookPart.GetPartsOfType<SharedStringTablePart>().First()
.SharedStringTable.ChildElements.Select(x => x.InnerText)
.ToArray();
foreach (var sheet in doc.WorkbookPart.Workbook.Descendants<Sheet>())
{
var part = (WorksheetPart)doc.WorkbookPart.GetPartById(sheet.Id);
foreach (var cell in part.Worksheet.Descendants<Cell>())
{
switch (cell.DataType?.Value)
{
case CellValues.Boolean:
yield return cell.InnerText == "0" ? "false" : "true";
break;
case CellValues.SharedString:
yield return sst[int.Parse(cell.InnerText)];
break;
case CellValues.Date:
yield return cell.InnerText;
break;
}
}
}
}
public static IEnumerable<string> GetWordText(Stream stream)
{
using var doc = WordprocessingDocument.Open(stream, false);
foreach (var element in doc.MainDocumentPart.Document.Body)
{
if (element is Paragraph p)
{
yield return p.InnerText;
}
}
var comments = doc.MainDocumentPart?.WordprocessingCommentsPart?.Comments;
if(comments == null)
yield break;
foreach (var element in comments)
{
yield return element.InnerText;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment