32
u/PolishedCheese Jun 29 '21
I have seen those horrors. That API was created in the before times, and has 0 consideration for maintainability.
54
u/Willinton06 Jun 28 '21
Interop needs to be rewritten in Core so we can actually do addins on core, they also need to add C# macros to seal de deal
26
u/vigbiorn Jun 29 '21
The documentation could also be massively upgraded.
I feel OP with the dreaded hack working in Excel Interop. Before I knew what I was looking for, it was impossible to work anything out and I needed to bash together a thing using marshaling (apparently a lot of people don't really know how to use it and are passing on those broken hacks). Once I stumbled into how it's meant to work, I could manage it but just sitting down from 0? It's useless, if not worse.
4
u/Zaphod118 Jun 29 '21
Do you have any tips or resources? I’ve got a work application that builds excel files, and I’ve been using all the marshaling “tricks” you find online. If there’s a better way I’d love to learn it! Because the way it is now is awful
3
u/vigbiorn Jun 29 '21
YMMV for everything below. I'm still no where near an expert. The code below works for my use-case, but it can have issues.
If I remember correctly, the biggest issue for me was using Cells[]. That is where the marshaling needed to come from, I think, because you're calling for the individual data to be read in from Excel. Using UsedRange and (for me) converting it to an object[,] was easiest. Though, if you're dealing with massive excel files, you may want to find a way to limit it to chunks.
I'd like to share resources, but I didn't save the site I used to get my code working over a year ago now, and looking again all I'm finding are options needing marshaling.
If it helps, here is are snippets I use (not used to Reddit's code formatting, so it might be janky):
Excel.Application excel = new Excel.Application();
Excel.Workbooks workbooks = excel.Workbooks;
Excel.Workbook exlFile = workbooks.Open(ExcelFilePath, ReadOnly: true);
Excel.Sheets sheets = exlFile.Worksheets;
//x is the index to your curr sheet of interest
Excel.Worksheet sheet = (Excel.Worksheet)sheets[x];
//I think this reads in the entire sheet, so might cause problems
//for large sheets
Excel.Range usedRange = sheet.UsedRange;
object[,] log = (usedRange.Value as object[,]);
// . . .var aValue = log[x,y];
// . . .
exlFile.Close();
workbooks.Close();
excel.Quit();I've been running the code for awhile and it is much faster. I think Cells is meant for individual one-time cell access, and provided just as a "if you need it", hence why the marshaling is needed to clean up afterwards. Switching from individual Cell[] calls to the UsedRange.Value saved me minutes to read kind of small excel files and I've not had the issue of missing a bit of memory not properly marshaled and having a floating Excel instance in limbo taking up memory.
6
u/burgundius Jun 29 '21
You can actually make add-ins on core and .NET 5, by implementing the IDTExtensibility2 interface from AddInDesignerObjects COM dll, registering the class for COM and writing a few registry entries.
You won't get the benefit of easy debugging, you'll have to attach it yourself.
5
u/Willinton06 Jun 29 '21
I want the whole interoperability to be reworked on core with native support, I’m asking for way too much
43
u/Cmgeodude Jun 29 '21
The programmer gets a gold star for the comment.
No judgment here.
16
u/tulliandar Jun 29 '21
The comment is the worst thing about it. They could’ve tried to be helpful instead of ornery
40
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jun 28 '21 edited Jun 29 '21
Image Transcription: Photograph
[The image is a photograph of code on a computer screen, as follows:]
capRange.Delete();
//Until you've felt my pain I don't want to hear a damn thing about the next 4 lines
capRange.MoveStart(WdUnits.wdCharacter, 1);
capRange.MoveStart(WdUnits.wdCharacter, -1);
capRange.MoveEnd(WdUnits.wdCharacter, 1);
capRange.MoveEnd(WdUnits.wdCharacter, -1);I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
18
8
25
u/devdevgoat Jun 28 '21
Is this why when I select a word in office products it fucking includes the space before/after it?!?!
17
12
Jun 29 '21
I think you can disable this via Options > Advanced > When Selecting, automatically select entire word.
5
8
3
u/danfish_77 Jun 29 '21
A lot of it is more that the Word object model is unintuitive and perhaps surprisingly complicated. Like, I feel like it should be pretty easy to tell if a table is going to break across a page, but nope! Gotta build the whole document to find out
3
u/gigawattwarlock Jun 29 '21
I have felt this pain.
Word wouldn’t make the dynamic range selection without forcefully setting the positioning, but wouldn’t honor 0.
Excel had a similar annoyance but I can’t remember what it was exactly.
1
u/ferrango Well it's broken now, and nobody is here to fix it Jun 29 '21
Try using the Libre Office interop library. I still have nightmares.
1
u/RavynousHunter Jun 29 '21
Yeah...much as I love C# and .NET in general, working with Office interop is one of the biggest gateways to spaghetti code-induced madness. I was doing something for my internship, I don't remember what, and I took one look at screwing with Excel spreadsheets in code and nope'd right outta there.
1
1
141
u/UltraPoci Jun 28 '21
Can anyone explain plis