将包含各种对象(图片,图表,OleObject,形状等)的Excel文档内容导出到Word文件中. [英] Export an Excel document contents which contains various Objects (Picture, Chart, OleObject, Shape etc) into a word file.

查看:136
本文介绍了将包含各种对象(图片,图表,OleObject,形状等)的Excel文档内容导出到Word文件中.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将Office 2007 Excel文档加载到表单中的应用程序.我需要将该Excel文件的所有内容导出到Office 2007 Word文档中.

输出应与从Excel提供打印时看到的预览完全匹配.它应该接受纵向和横向模式.需要检查打印区域选项".

我的Excel文档可能包含图表,图片,OleObject,Shape或Excel可接受的任何其他对象.

目前,我已经实现了一种方法,该方法需要遍历excel内容并检测每个可接受的对象.

我有许多如下所示的"For"循环,分别用于图表,图片,oleobject和形状以及普通文本.

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.

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;
     }
 }



这样做的缺点是对象可能因Excel而异,这迫使我为每个对象添加其他"For"循环.


有人可以帮我找到一种最佳方法,将Excel的全部内容导出到Word而不循环对象. Excel的所有选项都应该可用,例如设置打印区域",横向和纵向"等.

谢谢



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

推荐答案

我认为您已经给出了答案,将Excel的全部内容都放在Word中.因此,将Excel工作簿而不是工作簿中的所有对象都添加到Word文档中.您可以将工作簿放在Word中,并且所有Excel对象都保持在同一位置.

问候
Piet
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


朋友,

下面的代码可能对您有帮助,因为我是在asp .net C#中创建的...
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."; } }
"; 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."; } }


这篇关于将包含各种对象(图片,图表,OleObject,形状等)的Excel文档内容导出到Word文件中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆