c# controlling word file table - c#

I need to create a new word 2016 file, using VS2017, insert content (that's the easy part), and also to control it like doing the following:
Merge certain cells in same row, or same column
Define Right to Left or LTR
color the text/the background.
and more similar tasks.
I can open a document using
using Microsoft.Office;
using Word = Microsoft.Office.Interop.Word;
I can add text and save the document, yet still I don't see a way to fine control the color/direction and more parameters. After reading the documentation, it seems that this is probably not supported, unless I missed it.
I would appreciate if anyone can guide to a detailed documentation how to edit a word file from C# program.
Anyway, I can bypass it by creating an excel file which is simple using Interop and then insert it.

Here is a working solution for merging cells in a table, using VS2017 c#
var doc = DocX.Create(word_fname);
Table table = doc.AddTable(tableSize, 3);
table.Rows[row_cnt].MergeCells(1, 2); // to merge the 2nd & 3rd cells in the specific row

Related

How to display an Excel array without excel #N/As using C# Excel-DNA

I have a c# function returning a matrix, I read it on Excel (as an excel array). Anyone has an idea how to:
1)Display/expand the full array without having to use ctrl+shift+enter and readjust to remove #N/As.
2) expand only in empty Excel cells i.e do not overwrite existing cells.
You're looking for the new 'Dynamic Arrays' being tested in the insider versions of Excel. See https://techcommunity.microsoft.com/t5/Excel-Blog/Preview-of-Dynamic-Arrays-in-Excel/ba-p/252944 and other Google hits when you search for 'Excel dynamic array'.

multiple autofilters for the same EPPlus worksheet

Im creating an xlsx-file using EPPlus and want to have autofilters for all headers. I set the autofilters like this:
worksheet.Cells["A3:G" + (3 + data.Count).ToString()].AutoFilter = true;
This works smooth for just one table of content. However, I have two separate tables in the same worksheet, and when trying to set autofilters for the second one, the autofilters for the first one disappears.
Any known workarounds for this, or other suggestions?
This is a very good question!
A traditional limitation of AutoFilter is that it can be applied only once on a worksheet.
Starting with Excel 2007 you can create several Tables on a worksheet...........each with filtering capabilities.
See Pieterse's Article

Insert a table into a middle of word processing document (Open XML SDK)

I have a template Word document which i fill details into with openXML SDK 2.0 (using c#).
I also need to insert a table into file, and i found this tutorial on MSDN.
But - the example is appending the table to the end of the document, and I want it to be somewhere in the middle.
I may need to replace this line:
doc.MainDocumentPart.Document.Body.Append(table);
with something else. (The full code is in the link above).
Please help me.. I found nothing yet.
Thanks.
One way to do this may be to use Content Controls as placeholders to insert the table into them from code.
var myContentControl = doc.MainDocumentPart.Document.Body.Descendants<SdtBlock>()
.Where(e => e.Descendants<SdtAlias>().FirstOrDefault().Val == "myTablePlaceholder").FirstOrDefault();
SdtContentBlock sdtContentBlock1 = new SdtContentBlock();
sdtContentBlock1.Append(table); // Your table
myContentControl.Append(sdtContentBlock1);

how to format excel sheet according to property value of an object?

i need to create and download a excel sheet in c# asp.net. I used writing Range. because it is fast. but i need to format the excel sheet. According to a property(usercolor) of the user object, i need to color the row. but when writing to range how can i do that?
i am using this code to write
var startCell = (Range)sheet.Cells[2, 1];
var endCell = new object();
endCell = (Range)sheet.Cells[(usersList.Count + 2), noofcolums];
var writeRange = sheet.get_Range(startCell, endCell);
writeRange.Value2 = data;
data is a TwoDimensionalObject. it is created by user object.
As an additional comment: Do NOT use Excel in a server inevironment. It is slow and Excel might spawn error-windows at any time, causing a hang. This cannot be circumvented in a clean way - even Microsoft agrees and does not support office in a server model.
You might try epplus, a free reading / writing library for excel. It is fast , supports formatting and is way nicer to program than excel interop.
To color any Row in Excel
oRange.get_Range("A1","X1").Interior.Color = System.Drawing.ColorTranslator.ToWin32(Color.Orange);
Hope it helps
I could not find any way to use Object property to map with row color in range writing way. I need to write cell by cell. but it is very slow. So i used creating html file(html table) and convert it to excel document. it is not slow also. Thank you very much for all replies
please refer this Export HTML Table to Excel using ASP.NET

Microsoft Word 2007 VSTO, Create table outside word?

I am using VSTO to fill data into a table in a Microsoft Word 2007 template. The amount of data varies and filling many pages (+50) takes a lot of time.
The code I use to create a table:
Word.Table table = doc.Tables.Add(tablePosition,
numberOfRows,
8,
ref System.Reflection.Missing.Value,
ref System.Reflection.Missing.Value);
I suspect that the time consumption is due to the communication between Visual Studio (C#) and Word each time I insert data into a cell. If this is the case, it might be faster to create the table in C# and afterwards insert it into Word.
The Microsot.Office.Interop.Word.Table is an abstract class - thus I cannot do this
Word.Table table = new Word.Table();
which would have been handy.
Are there other possibilities when just using VSTO?
Try creating the table in HTML Clipboard format, add to clipboard, then paste.
Try creating the table in HTML and inserting it.
Try creating tab-delimited string with newline character for each record. Insert string with selection, convert selection to table using tabs as delimiter.
Create template as XML, transforming data with Xslt into Word XML Document.
Create template as a "Directory Mail Merge", perform mail merge with data.
Depending on your requirements, I recommend using the mail merge technique because the user can edit the template and mail merges are fast, especially if you have 50+ pages.
Although I do similar things with LabVIEW7.1 and Word2000, the problem is similar. I have not found a way to insert blocks of data (table) with one command. There is even a problem when inserting single elements too fast for word, it occasionally hangs than and must be killed in order to solve that. Unfortunately there is no event nor property that signals word's ability to accept the next command and data set - at least I could not find anything.
As this is in a test sequencer I have the time to feed the test results into word with delays long enough to assume word is ready again when the next portion of data is send...

Categories