C#-使用Interop将Excel复制到Word

问题描述

我想在不使用剪贴板的情况下将excel复制为图片

剪贴板在程序运行时太容易使用户无法操作,有时会丢失项目。

我该怎么做?

这里我现在如何处理

foreach (Worksheet WS in WB.Worksheets)
                {
                    progresspercent += steps;
                    worker.ReportProgress(progresspercent);

                    if (WS.UsedRange.Count > 1 && WS != null)
                    {
                        Thread.Sleep(1000);
                        var height = WS.UsedRange.Height;
                        var width = WS.UsedRange.Width;
                        var MaxHeight = ((double)width * 8.5) / 6.5;
                        var heightRatio= (double)height/MaxHeight;
                        double TotalHeight = 0;
                        int RowsSoFar = 0;

                        int TotalRows = WS.UsedRange.Rows.Count;

                        int RowStart = 1;
                        int RowEnd = RowStart + (int)Math.Floor(TotalRows/heightRatio);
                        if (RowEnd > TotalRows + 1) { RowEnd = TotalRows + 1; }

                        ///Calculate Image sizes and size copy for page
                        while (TotalRows > RowsSoFar)
                        {

                            Range copyRange = WS.Range["A" + RowStart.ToString() + ":F" + RowEnd.ToString()];
                            double  SelectionHeight = (double)copyRange.Height;

                            while (SelectionHeight > MaxHeight )
                            {
                                //***************************************//
                                RowEnd = RowEnd - 1;
                                copyRange = WS.Range["A" + RowStart.ToString() + ":F" + RowEnd.ToString()];
                                SelectionHeight = (double)copyRange.Height; 
                            }
                             
                            FullWordDoc.Words.Last.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak);
                            string ContinuedPrompt = "";
                            if (RowStart!=1) { ContinuedPrompt = " (Continued)"; }

                            FullWordDoc.Words.Last.InsertAfter(WS.Name + ContinuedPrompt + "\r");
                            copyRange.copyPicture();
                            
                            
                            FullWordDoc.Words.Last.Paste();
                            
                            

                            Console.WriteLine(WS.Name);
                            RowsSoFar = RowsSoFar + RowEnd - RowStart + 1;
                            RowStart = RowEnd + 1;
                            RowEnd = RowStart + (int)Math.Floor(TotalRows / heightRatio);
                            if (RowEnd > TotalRows + 1) { RowEnd = TotalRows + 1; }
                        }
                    }
                }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)