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

不打扰是莪最后的温柔 提交于 2019-12-05 14:23:39

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.

For example, you've got:

ChartObjects ListObjects OleObjects Shapes (Which might get copied along with the sheet, I'm not sure).

Perhaps you can get there by a copy/paste? Here's an example that copies cell data, charts and images: MSDN

Good luck!

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. 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.)

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 {}
}
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!