0

I would like to be able to include some raw datafile (txt) inside an xlsx file. I think it should be possible, as images can be included as well. However, I have not found any documentation on how to do this. (It would be even better if I could do this using the Open XML library.)

I found a question + answer on stack overflow which sounds promising, but I did not work for me (or for a spreadsheet).

https://stackoverflow.com/a/3128463/727061

EDIT: Seems I made a mistake which made the Excel file corrupt, after a retry the solution above works!

Community
  • 1
  • 1
Aloys
  • 682
  • 4
  • 11

1 Answers1

0

After researching my file generated using this answer: https://stackoverflow.com/a/3128463/727061 I found out how to add files to a spreadsheet using Open XML.

c#:

// base64 encoded data
string base64Data = "ZG9lIG1paiBtYWFyIHdhdCBleHRyYSB0ZWtzdA==";

// create a spreadsheet document
val spreadsheetDocument = SpreadsheetDocument.Create(filePath, SpreadsheetDocumentType.Workbook);

// initialize spreadsheet here

// add data to spreadsheet document
ExtendedPart ePart = this.spreadsheetDocument.WorkbookPart.AddExtendedPart("http://schemas.microsoft.com/office/2007/relationships/txt", "custom/txt", ".txt", "rId1");
System.IO.Stream data = GetBinaryDataStream(base64Data);
ePart.FeedData(data);
data.Close();

// retreive all extended data parts
val extendedParts = spreadsheetDocument.WorkbookPart.GetPartsOfType<ExtendedPart>().ToList();

// list those parts
Console.Writeline("Extended parts in file");
foreach(ExtendedPart p in extendedParts)
{
    Console.writeline(this.spreadsheetDocument.WorkbookPart.GetIdOfPart(p));
    Console.writeline("-----------------");
    Console.writeline(new StreamReader(elem.GetStream()).ReadToEnd());
}
Community
  • 1
  • 1
Aloys
  • 682
  • 4
  • 11