C
C#17mo ago
MeHow

❔ DocumentFormat.OpenXML - insert row into an Excel Table

Hi, I have a problem with inserting a row into the Table in Excel document. I had no problems with inserting cell values, but when I want to add a collection of objects as rows it does not update the table. Here's the code:
void InsertDeskIntoATable(Table table, Desk desk)
{
var row = new TableRow();

var props = desk.GetType().GetProperties();

foreach (var prop in props)
{
var value = prop.GetValue(desk, null) ?? 0;
var cell = new TableCell(new Paragraph(new Run(new Text(value.ToString()))));
row.Append(cell);
}

table.Append(row);
table.Save();
}

public class Desk
{
public int Width { get; set; }
public int Height { get; set; }
}
void InsertDeskIntoATable(Table table, Desk desk)
{
var row = new TableRow();

var props = desk.GetType().GetProperties();

foreach (var prop in props)
{
var value = prop.GetValue(desk, null) ?? 0;
var cell = new TableCell(new Paragraph(new Run(new Text(value.ToString()))));
row.Append(cell);
}

table.Append(row);
table.Save();
}

public class Desk
{
public int Width { get; set; }
public int Height { get; set; }
}
I'm using reflections cause I want to make the function generic in the future, it works fine but the table in my file does not update. The reference to the table is correct and the other data saves properly. Just not the table 🙂
3 Replies
canton7
canton717mo ago
OpenXML and Excel is just painful. There's so much boilerplate and weird special cases you need to handle There are wrappers around OpenXML for Excel which make life so much easier. ClosedXML is one I've used, but IIRC the OpenXML repo lists others. I strongly recommend using one (OpenXML for Word is sensible enough to tackle on your own)
canton7
canton717mo ago
GitHub
GitHub - dotnet/Open-XML-SDK: Open XML SDK by Microsoft
Open XML SDK by Microsoft. Contribute to dotnet/Open-XML-SDK development by creating an account on GitHub.
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ Get value out of deserialized json object.I need to get value out of meta, from the "object". As for now I also use this: Dictionary<string, ❔ I'm confused how "x = false" can cause a NullReferenceExceptiondeclaration of _isUpdatingPeakValue is inside a public sealed class: ```C private volatile bool _is❔ Azure Speech Service wont shut upI am using azure in a WPF app, where I have two buttons, Read, and Stop The problem is, that when I❔ The remote certificate is invalid because of error in the certificate chain: partial chainWhen I debug it from VS, the request responds successfully, but when the service is deployed to anot❔ ✅ How does .NET MAUI Blazor relate to Blazor Server or WebAssembly?Or am I conflating those ideas? I see the .NET MAUI Blazor project template `index.html` has the li✅ Output not correctIm doing a Client/server program the program will give out in output if the user is a minor or not t❔ How to create an event handler for a variableI am currently developing a HTML editor that has tabs and a page that shows the output. The variable❔ Looking to turn this GUI integer calculator into a Fraction calculator using the provided classFinal project for school, need to create a GUI fraction calculator. I have a GUI integer calculator ❔ How do I publish a WebAssembly app to a singlefile executableHow do I publish a web assembly app to a selfhosted single executable file? Using these settings juJSON C#Can someone help with this code: using System; using System.Collections.Generic; using System.Compon