Click here to Skip to main content
15,441,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to insert values into the cells of the table in my word file in c# using Microsoft interop and i have used foreach in the below code for dummy number that automatically insert as per the cell position i want to repace with it my own values

in this case what should i try please give a suggestion or a solution for this
Thank You!!

What I have tried:

//Add paragraph with Heading 1 style
               Microsoft.Office.Interop.Word.Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
               object styleHeading1 = "Heading 1";
               para1.Range.set_Style(ref styleHeading1);
               para1.Range.Text = "1. Number of users Login ";
               para1.Range.Font.Bold = 2;
               para1.Range.Font.Color = WdColor.wdColorBlack;
               para1.Range.InsertParagraphAfter();
               Table firstTable = document.Tables.Add(para1.Range, 2, 5, ref missing, ref missing);
firstTable.Borders.Enable = 1;
               

                foreach (Row row in firstTable.Rows)
                {
                    foreach (Cell cell in row.Cells)
                    {
                        //Header row
                        if (cell.RowIndex == 1)
                        {
                            cell.Range.Text = "Column " + cell.ColumnIndex.ToString();
                            cell.Range.Font.Bold = 1;
                            //other format properties goes here
                            cell.Range.Font.Name = "verdana";
                            cell.Range.Font.Size = 10;
                            //cell.Range.Font.ColorIndex = WdColorIndex.wdGray25;                            
                            cell.Shading.BackgroundPatternColor = WdColor.wdColorGray25;                      
                            //Center alignment for the Header cells                                           
                            cell.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;        
                            cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                            
                        }
                        //Data row
                        else
                        {
                            cell.Range.Text = (cell.RowIndex - 3 + cell.ColumnIndex).ToString();
                        }
                    }
                }
Posted
Updated 4-Aug-22 1:04am
Comments
0x01AA 4-Aug-22 6:47am     CRLF
Not sure wheter I understand correctly. But you can address a specific Cell by: firstTable.Rows[1].Cells[1].Range.Text = "My Value for that Cell"; Keep in mind, in office interop the index of a collection starts alway at '1'.
Manojkumar-dll 4-Aug-22 6:57am     CRLF
Thank You so much!! please post this as an answer so i can mark it
0x01AA 4-Aug-22 7:06am    
You are welcome. Thanks also.

You just need a set of data (an array or collection) that you want to put in the cells. So replace (cell.RowIndex - 3 + cell.ColumnIndex).ToString() with each item of your data.
 
Share this answer
 
Comments
Manojkumar-dll 4-Aug-22 6:23am     CRLF
Ys it works but for all the cells it's taking the same value for each and very cell means what should i do also for the column names thank you
Richard MacCutchan 4-Aug-22 7:09am    
Think about what you are trying to achieve here. You need a different value for each cell, so start by collecting all the different values in a collection o b ject. You can then use a simple loop that indexes the valiues as you fill each cell.
Manojkumar-dll 4-Aug-22 7:11am    
Sure !!!Thanks a lot sir
One can address a specific cell directly by row/column index. E.g. like
firstTable.Rows[1].Cells[1].Range.Text = "My Value for that Cell";

Note: Keep in mind that for office interop the index of a collection starts at '1' and ends at 'collectionName.Count'


[Edit]
Just played around more with word interop and found a second (maybe slightly more performant) way how to address a cell of a table:
secondTable.Cell(2, 1).Range.Text = "My 2/1";  // Row 2, Column 1
See here for more details: Table.Cell(Int32, Int32) Method (Microsoft.Office.Interop.Word) | Microsoft Docs[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900