C#-使用互操作将Excel复制到Word
我想将excel作为图片复制到word,而不使用剪贴板 当程序运行时,剪贴板对于用户来说太容易操作,有时会遗漏项目 我该怎么做 这是我现在的处理方法C#-使用互操作将Excel复制到Word,c#,excel,ms-word,office-interop,C#,Excel,Ms Word,Office Interop,我想将excel作为图片复制到word,而不使用剪贴板 当程序运行时,剪贴板对于用户来说太容易操作,有时会遗漏项目 我该怎么做 这是我现在的处理方法 foreach (Worksheet WS in WB.Worksheets) { progresspercent += steps; worker.ReportProgress(progresspercent);
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; }
}
}
}
您的代码是否正常工作;如果没有,你能说问题出在哪里吗?@PeterSmith代码可以工作,但我宁愿直接把图表放到Word上,而不是复制和粘贴。当用户试图复制其他数据时,它最终会粘贴到word文档上。您可以将Excel电子表格中的链接插入到word文档中。请看。问题是,如果我将图表添加到word中,没有标准的缩放方式。至少有一张图片,我可以确保它适合这一页。