Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a window application with a data table. Data come from sql server.
In this data table i have some column with text data and 4 column with image path. Images store in a folder. I want to export the data table to ms word with image. In a proper format.
Can anyone help me how to do this.

What I have tried:

DataTable dt=new DataTable();
Spire.DataExport.RTF.RTFExport rtfExport = new Spire.DataExport.RTF.RTFExport();
rtfExport.DataSource = Spire.DataExport.Common.ExportSource.DataTable;
rtfExport.DataTable = dt;
rtfExport.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView;
RTFStyle rtfStyle = new RTFStyle();
rtfStyle.FontColor = System.Drawing.Color.Blue;
rtfStyle.BackgroundColor = System.Drawing.Color.LightGreen;
rtfExport.RTFOptions.DataStyle = rtfStyle;
rtfExport.FileName = @"E\ToWord.doc";
rtfExport.SaveToFile();


The above code export the data to ms word but not in proper format and also image not export.
help me how to do this .
Thank you
Posted
Updated 28-Apr-17 1:14am
v2

 
Share this answer
 
Comments
[no name] 24-Apr-17 9:10am    
Thank you for reply
This is the link where i get my proper code http://www.c-sharpcorner.com/UploadFile/muralidharan.d/how-to-create-word-document-using-C-Sharp/

actually not proper, need to add images conditionally in table cell but i can't can you help me please
try
            {
                progressBar1.Visible = true;
               
                Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application();
                winword.Visible = false;
                //Create a missing variable for missing value
                object missing = System.Reflection.Missing.Value;
                //Create a new document
                Microsoft.Office.Interop.Word.Document document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);


              

              
                document.Content.SetRange(0, 0);
               
                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 = "Post Add report";
                para1.Range.InsertParagraphAfter();

                string samplePath2 = System.Windows.Forms.Application.StartupPath + Path.DirectorySeparatorChar + "IPIMages" + Path.DirectorySeparatorChar;
                string imagePath = samplePath2 + "logo.jpg";

                Paragraph para = document.Content.Paragraphs.Add(ref missing);
                para.Range.InsertParagraphBefore();
               
                InlineShape map = document.InlineShapes.AddPicture(imagePath, ref missing, ref missing, ref missing);
                map.Height = 176;
                map.Width = 177;
                para.Range.InsertParagraphAfter();
                object oPageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
                para.Range.InsertBreak(ref oPageBreak);

                string[] coulmnname = value.Split(',');
                int coulmcount = dt.Rows.Count;
                progressBar1.Maximum = coulmcount-1;
                #region 
                Table firstTable = document.Tables.Add(para1.Range, coulmcount - 1, 7, ref missing, ref missing);
                firstTable.Borders.Enable = 1;
                int i = 0, r = 0, s = 0; string img = "";
                string samplePath = "", imagepath = "";


                foreach (Row row in firstTable.Rows)
                {
                    r++;
                    int j = 0;
                    foreach (Cell cell in row.Cells)
                    {
                        progressBar1.Value = r;
                        //Header row
                        if (cell.RowIndex == 1)
                        {
                            cell.Range.Text = coulmnname[i].ToString();
                            cell.Range.Font.Bold = 1;
                            cell.Range.Font.Name = "verdana";
                            cell.Range.Font.Size = 10;
                            cell.Shading.BackgroundPatternColor = WdColor.wdColorGray25;
                            cell.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                            cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                            i++;
                        }
                        //Data row
                        else
                        {


                            if (j == 3)
                            {
                                img = dt.Rows[r - 2][coulmnname[j]].ToString();
                                if (img == "No image")
                                {

                                    cell.Range.Text = dt.Rows[r - 2][coulmnname[j]].ToString();
                                    cell.Range.Font.Name = "Century";
                                    cell.Range.Font.Size = 14;
                                    cell.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                                    cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                                    j++;
                                }
                                else
                                {
                                    samplePath = System.Windows.Forms.Application.StartupPath + Path.DirectorySeparatorChar + "PostAd" + Path.DirectorySeparatorChar;
                                    imagepath = samplePath + dt.Rows[r - 2][coulmnname[j]];
                                    Range rngPic2 = document.Tables[1].Cell(r, 4).Range;
                                    rngPic2.InlineShapes.AddPicture(imagepath, ref missing, ref missing, ref missing);
                                    j++;
                                }
                            }
                            else if (j == 4)
                            {
                                img = dt.Rows[r - 2][coulmnname[j]].ToString();
                                if (img == "No image")
                                {
                                    cell.Range.Text = dt.Rows[r - 2][coulmnname[j]].ToString();
                                    cell.Range.Font.Name = "Century";
                                    cell.Range.Font.Size = 14;
                                    cell.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                                    cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                                    j++;
                                }
                                else
                                {
                                    samplePath = System.Windows.Forms.Application.StartupPath + Path.DirectorySeparatorChar + "PostAd" + Path.DirectorySeparatorChar;
                                    imagepath = samplePath + dt.Rows[r - 2][coulmnname[j]];
                                    Range rngPic2 = document.Tables[1].Cell(r, 5).Range;
                                    rngPic2.InlineShapes.AddPicture(imagepath, ref missing, ref missing, ref missing);
                                    j++;
                                }
                            }
                            else if (j == 5)
                            {
                                img = dt.Rows[r - 2][coulmnname[j]].ToString();
                                if (img == "No image")
                                {
                                    cell.Range.Text = dt.Rows[r - 2][coulmnname[j]].ToString();
                                    cell.Range.Font.Name = "Century";
                                    cell.Range.Font.Size = 14;
                                    cell.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                                    cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                                    j++;
                                }
                                else
                                {
                                    samplePath = System.Windows.Forms.Application.StartupPath + Path.DirectorySeparatorChar + "PostAd" + Path.DirectorySeparatorChar;
                                    imagepath = samplePath + dt.Rows[r - 2][coulmnname[j]];
                                    Range rngPic2 = document.Tables[1].Cell(r, 6).Range;
                                    rngPic2.InlineShapes.AddPicture(imagepath, ref missing, ref missing, ref missing);
                                    j++;
                                }

                            }

                            else if (j == 6)
                            {
                                img = dt.Rows[r - 2][coulmnname[j]].ToString();
                                if (img == "No image")
                                {
                                    cell.Range.Text = dt.Rows[r - 2][coulmnname[j]].ToString();
                                    cell.Range.Font.Name = "Century";
                                    cell.Range.Font.Size = 14;
                                    cell.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                                    cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                                    j++;
                                }
                                else
                                {
                                    samplePath = System.Windows.Forms.Application.StartupPath + Path.DirectorySeparatorChar + "PostAd" + Path.DirectorySeparatorChar;
                                    imagepath = samplePath + dt.Rows[r - 2][coulmnname[j]];
                                    Range rngPic2 = document.Tables[1].Cell(r, 7).Range;
                                    rngPic2.InlineShapes.AddPicture(imagepath, ref missing, ref missing, ref missing);
                                    j++;
                                }
                            }
                            else
                            {
                                cell.Range.Text = dt.Rows[r - 2][coulmnname[j]].ToString();
                                cell.Range.Font.Name = "Century";
                                cell.Range.Font.Size = 14;
                                cell.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                                cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                                j++;

                            }

                        }
                    }
                    s++;
                }
                #endregion

                //Save the document
                Random random = new Random();
                int randomNumber = random.Next(0, 100);
                object filename = @"E:\report" + randomNumber + ".docx";
                document.SaveAs2(ref filename);
                document.Close(ref missing, ref missing, ref missing);
                document = null;
                winword.Quit(ref missing, ref missing, ref missing);
                winword = null;
                MessageBox.Show("Document created successfully in E drive in (report" + randomNumber + ".docx)");
                progressBar1.Visible = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Please Delete Existing Report's or move to another drive");
            }
 
Share this answer
 
v2
Comments
Richard Deeming 28-Apr-17 9:08am    
You've tagged the question as ASP.NET, so you need to read the following Microsoft knowledgebase article:
Considerations for server-side Automation of Office[^]
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

Apart from requiring an MS Office license for the server, there are numerous hoops you'll have to jump through to stand any chance of getting this code to work on a real server. And even then, there is no guarantee that it won't randomly stop working at some point in the future.

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