Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an application where I load an Office 2007 Excel document into my form. I need to export all the contents of that Excel file into an Office 2007 word document.

The Output should exactly match the Preview that we see when we give a print from Excel. It should accept the portrait and landscape mode. Need to check the Print Area Options.

My Excel document may contain Charts, Pictures, OleObject, Shapes or any other Objects acceptable to an Excel.

Currently I have implemented a method where I need to loop through the excel contents and detect each of the acceptable Objects.

I am having numerous “For” loops like below for chart, picture, oleobject and shapes and for normal texts.

C#
foreach (ChartObject chObj in (ChartObjects)ws.ChartObjects(missing))
 {
 if (chObj != null && chObj.Visible)
     {
       if (firstCol > chObj.TopLeftCell.Column)
           firstCol = chObj.TopLeftCell.Column;
       if (firstRow > chObj.TopLeftCell.Row)
           firstRow = chObj.TopLeftCell.Row;
       if (lastCol < chObj.BottomRightCell.Column)
          lastCol = chObj.BottomRightCell.Column;
       if (lastRow < chObj.BottomRightCell.Row)
          lastRow = chObj.BottomRightCell.Row;
     }
 }


The disadvantage of this is that the Object may vary from Excel to Excel and this forces me to add additional “For” loops for each Object.


Could some one help me to find a best way to export the full contents of Excel to Word without looping thorough objects. All the options of Excels should be available like setting Print Area, Landscape and Portrait Orientations etc..

Thanks
Posted

I think you gave the answer your self, put the full content of the Excel in Word. Thus add the Excel workbook to the Word document instead of all objects in the workbook. You can position the workbook in Word and all Excel objects remain in the same position.

Regards
Piet
 
Share this answer
 
Comments
Ullas_Joseph 13-Feb-12 8:20am    
Could you please explain how to put the full content of Excel to a Word Document.
Do you have any examples for me to refer.

I think you are mentioning about embedding the excel to a word. I am not looking for that.

I need the all objects in the excel to be exported to word;for say which should look exactly the same as when we took a printout of the Excel to a paper.
Hi friend,

The below Code may Help You, as i have created it in asp .net C#...
protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (string.Compare(System.IO.Path.GetExtension(FileUpload1.FileName), ".xls", true) != 0)
        {

            filename = (FileUpload1.FileName);
            file1 = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);

            path = Server.MapPath(@"~/Docs/" + file1 + "");

            if (!System.IO.File.Exists(path))
            {
                FileUpload1.PostedFile.SaveAs(path);
            }

            string strQuery = string.Empty;
            strExcelCon = ConfigurationSettings.AppSettings["ExcelSource"] + path;
            OleDbConnection excelCon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strExcelCon + ";Extended Properties=Excel 12.0");
            string command = "Select * FROM [Sheet1$]";
            excelCon.Open();
            OleDbDataAdapter da = new OleDbDataAdapter(command, excelCon);
            
            ds = new DataSet();
            da.Fill(ds, "Sheet1");
            GridView1.DataSource = ds;
            GridView1.DataBind();
            excelCon.Close();
           
            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                string DateTime = System.DateTime.Now.Day + "/" + System.DateTime.Now.Month + "/" + System.DateTime.Now.Year;
                SqlCommand comm;
                if (ddlOptions.SelectedItem.Text == "Normal")
                {
                    comm = new SqlCommand("Insert into UploadQuestion(BatchID,SchoolName,Question,Class,Subject,ExamType,Chapter,Topic,Marks,UploadedDate,Status) values('" + batch + "','" + txtSchoolName.Text + "','" + GridView1.Rows[i].Cells[0].Text + "','" + GridView1.Rows[i].Cells[1].Text + "','" + GridView1.Rows[i].Cells[2].Text + "','" + GridView1.Rows[i].Cells[3].Text + "','" + GridView1.Rows[i].Cells[4].Text + "','" + GridView1.Rows[i].Cells[5].Text + "','" + txtMarks.Text + "','" + System.DateTime.Now + "','" + "0" + "')", gc.Con);
                }
                else
                {
                    comm = new SqlCommand("Insert into Questions(BatchID,SchoolName,que,O1,O2,O3,O4,ans,class,Subject,ExamType,Chapter,Topic,marks,UploadedDate) values('" + batch + "','" + txtSchoolName.Text + "','" + GridView1.Rows[i].Cells[0].Text + "','" + GridView1.Rows[i].Cells[1].Text + "','" + GridView1.Rows[i].Cells[2].Text + "','" + GridView1.Rows[i].Cells[3].Text + "','" + GridView1.Rows[i].Cells[4].Text + "','" + GridView1.Rows[i].Cells[5].Text + "','" + GridView1.Rows[i].Cells[6].Text + "','" + GridView1.Rows[i].Cells[7].Text + "','" + GridView1.Rows[i].Cells[8].Text + "','" + GridView1.Rows[i].Cells[9].Text + "','" + GridView1.Rows[i].Cells[10].Text + "','" + txtMarks.Text + "','" + System.DateTime.Now + "')", gc.Con);
                }
                gc.getconnection();
                comm.ExecuteNonQuery();
            }
          
            lblMessage.Text = "Data has been Uploaded successfully.";
           
        }
       
    }
 
Share this answer
 
v2
Comments
Baji Jabbar 13-Feb-12 7:54am    
Solution is to insert the data's in the excel sheet to database, But the question is to render the excel to word. I think your answer is wrong for this queston.
Ullas_Joseph 13-Feb-12 8:26am    
Yes M@BJ. This is not what i am looking for. here you are loading the contents to a Grid and inserting to DB. My requirement is like described below...

If you have an Excel document and if you try to take a printout to a paper, how does it look like. it will take care of the Print Area Option, Landscape and Portrait Orientation etc.
I need exactly the same behavior that all contents and objects of excel should rendered to a word document.

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