简体   繁体   English

如何将 Excel 工作表复制到新工作簿中并带上所有图表、图像等?

[英]How do you copy an Excel worksheet into a new workbook AND bring all the charts, images, etc.?

Our application has distribution functionality.我们的应用程序具有分发功能。 It takes several Excel 2007 spreadsheets, copies them into a single sheet, then emails them to the users.它需要几个 Excel 2007 电子表格,将它们复制到一个工作表中,然后通过电子邮件将它们发送给用户。 The problem is that images and charts are not copying over.问题是图像和图表没有复制过来。 I have tried everything I can find on here and various other sources, but sadly nothing seems to work.我已经尝试了在这里和其他各种资源中可以找到的所有内容,但遗憾的是似乎没有任何效果。 Our application is written using VSTO, and I have also tried OpenXML, but nothing seems to work.我们的应用程序是使用 VSTO 编写的,我也尝试过 OpenXML,但似乎没有任何效果。 In fact trying to copy over in OpenXML corrupted the files so badly that Excel could not read or recover them.事实上,试图在 OpenXML 中复制会严重损坏文件,以至于 Excel 无法读取或恢复它们。 Here's the code we use (note: we have ExcelWrapper simply calls the same functions on the Excel App but hides all the optional items).这是我们使用的代码(注意:我们有 ExcelWrapper 只是在 Excel 应用程序上调用相同的函数,但隐藏了所有可选项目)。

 private void CreateWorkbook(string filePath, string batchReportsPath)
    {
        //place the xlsx file into a workbook.
        //call getfilesnames

        Excel.Workbook bookToCopy;
        Excel.Workbook newWorkbook;
        Excel.Worksheet tempSheet = new Excel.Worksheet();

        newWorkbook = ExcelWrapper.WorkbooksAdd(ExcelApp.Workbooks);

        if (File.Exists(filePath))
            File.Delete(filePath);

        ExcelWrapper.WorkbookSaveAs(newWorkbook, filePath);

        List<string> filePaths = new List<string>(Directory.GetFiles(batchReportsPath));
        filePaths.ForEach(delegate(string reportPath)
        {
            string reportPathAndName = reportPath;

            bookToCopy = ExcelWrapper.WorkbooksOpen(ExcelApp.Workbooks, reportPathAndName);

            int nextSheetNumber = newWorkbook.Sheets.Count;
            ((Excel.Worksheet)sheetToSend.Sheets[1]).Copy(Type.Missing, newWorkbook.Sheets[nextSheetNumber]);


            ExcelWrapper.WorkbookClose(bookToCopy);
        });


        newWorkbook.Save();
        ExcelWrapper.WorkbookClose(newWorkbook);
        bookToCopy= null;
        tempSheet = null;
        newWorkbook = null;

        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }

I have tried every promising option and searched both the VSTO and OpenXML object models and I am stumped.我已经尝试了所有有希望的选项,并搜索了 VSTO 和 OpenXML object 模型,我被难住了。 Please stackoverflow community, you're my only hope.请stackoverflow社区,你是我唯一的希望。

UPDATE: Here's the answer folks:更新:这是答案的人:

 //Copy all the images, Charts, shapes
   foreach (Excel.Shape o in copySheet.Shapes)
   {
         if (o.Type == Microsoft.Office.Core.MsoShapeType.msoPicture)
             o.CopyPicture();
         else
             o.Copy();

         newWorkbook.Save();
   }

You need to do the save after each copy to get the Paste to finalize.您需要在每次复制后进行保存以使粘贴完成。 Thanks for your input.感谢您的输入。

You'll need to check the WORKSHEET object of the worksheet you're looking to copy, then run through all the "*Objects" properties, and, for each of those collections, write code to manually copy all the elements in that collection to the new sheet.您需要检查您要复制的工作表的 WORKSHEET object,然后运行所有“*Objects”属性,并且对于每个 collections,编写代码以手动将该集合中的所有元素复制到新表。

For example, you've got:例如,您有:

ChartObjects ListObjects OleObjects Shapes (Which might get copied along with the sheet, I'm not sure). ChartObjects ListObjects OleObjects Shapes (我不确定它可能与工作表一起复制)。

Perhaps you can get there by a copy/paste?也许您可以通过复制/粘贴到达那里? Here's an example that copies cell data, charts and images: MSDN这是一个复制单元格数据、图表和图像的示例: MSDN

Good luck!祝你好运!

This is how I do it -- moves the entire worksheet from source workbook to a brand new workbook:我就是这样做的——将整个工作表从源工作簿移动到一个全新的工作簿:

public static void MoveWorksheet()
{
public static Excel.Application oXL;
public static Excel._Worksheet ws;
if (File.Exists(Global.Variables.GlobalVariables.recap))
{
//workbookToOpen is location and name
oXL.Workbooks.Open(workbookToOpen, Missing.Value, true);
object ws = null;
try
{
    ws = wb.Sheets["Sheet 1"];
    if (ws != null)
    {
        ((Worksheet)ws).UsedRange.Interior.ColorIndex = Constants.xlNone;
        ((Worksheet)ws).Copy();
        oXL.ActiveWorkbook.SaveAs("SaveAsFileName" + ".xlsx");
        oXL.ActiveWorkbook.Close(true);
    }
}
catch {}
}
}

I don't have an answer but I can give you some pointers.我没有答案,但我可以给你一些指示。 First, this is not an openxml question, it's a vsto question.首先,这不是一个 openxml 问题,而是一个 vsto 问题。 Second, images and charts are attached to a spreadsheet, they are not content in it (a subtle distinction).其次,图像和图表附加到电子表格中,它们不是其中的内容(细微的区别)。 There is probably a property of all charts and one of all images attached to the sheet.可能有所有图表的属性和附加到工作表的所有图像之一。 Copy that across.把它复制过来。

(I use the IExtensibility interface instead of VSTO so I know what's in the underlying API, but not the property names - sorry.) (我使用 IExtensibility 接口而不是 VSTO,所以我知道底层 API 中有什么,但不知道属性名称 - 抱歉。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将Excel图表复制到新工作簿并断开外部链接 - How to copy Excel charts to a new workbook and break external links Excel 互操作:如何将工作表复制到另一个工作簿? - Excel Interop: How to copy a worksheet to another workbook? 如何在新的excel工作簿中创建空白工作表 - How to create a blank worksheet into the new excel workbook 将Excel DropDown从WorkBook复制到新工作簿 - Copy Excel DropDown from WorkBook to a new Workbook OpenXML:如何将工作表复制到另一个工作簿? - OpenXML: How to copy a worksheet to another workbook? c#-(Excel互操作)如何在VSTO中将一个工作表复制到另一工作簿? - c# - (Excel interop)How can I copy one worksheet to another workbook in VSTO? C# - 如何将单个 Excel 工作表从一个工作簿复制到另一个工作簿? - C# - How to copy a single Excel worksheet from one workbook to another? 将excel工作簿中的第一行复制到新的Excel工作簿 - Copy first row in excel workbook to a new excel workbook 打印Excel工作表/工作簿 - Print Excel Worksheet/Workbook 如何在不打开另一个workBook / excel应用程序的情况下将外部数据(.csv文件)导入为新工作表 - c# - How to import external data (.csv file) as a new worksheet without opening another workBook/excel application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM