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

耗尽温柔 提交于 2019-12-10 09:36:24

问题


Our application has distribution functionality. It takes several Excel 2007 spreadsheets, copies them into a single sheet, then emails them to the users. 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. In fact trying to copy over in OpenXML corrupted the files so badly that Excel could not read or recover them. 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).

 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. Please stackoverflow community, you're my only hope.

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.


回答1:


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




回答2:


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

Good luck!




回答3:


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




回答4:


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


来源:https://stackoverflow.com/questions/6283135/how-do-you-copy-an-excel-worksheet-into-a-new-workbook-and-bring-all-the-charts

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