无法按照模板创建excel文件

时间:2013-12-27 18:43:43

标签: c# excel interop

问题点: 1.我有一个对象列表,我需要在excel文件中导出它们。每个对象必须位于一个单独的工作表上,工作表由模板构成。  我的算法:    1.在项目中创建excel模板文件    2.对于列表中的每个对象,在excel文件中复制模板表并填充内容。

为此,我创建了ExcelWorker工作类并将对象列表传递给AutomateExcelImpl方法。问题是app。抛出。一个例外 oXL.Workbooks [1] .Worksheets.Copy(片材);  我用谷歌搜索它,但几乎打破了我的头脑挖掘这个问题。 请帮忙。

using JiraExample.Entities.Projects;
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace JiraExample.Helpers
{
    class ExcelWorker
    {
        public static void AutomateExcel()
        {
            //AutomateExcelImpl();


            // Clean up the unmanaged Excel COM resources by forcing a garbage 
            // collection as soon as the calling function is off the stack (at 
            // which point these objects are no longer rooted).

            GC.Collect();
            GC.WaitForPendingFinalizers();
            // GC needs to be called twice in order to get the Finalizers called 
            // - the first time in, it simply makes a list of what is to be 
            // finalized, the second time in, it actually is finalizing. Only 
            // then will the object do its automatic ReleaseComObject.
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        public static void AutomateExcelImpl(List<ProjectDescription> projects)
        {
            object missing = Type.Missing;

            try
            {
                // Create an instance of Microsoft Excel and make it invisible.

                Microsoft.Office.Interop.Excel.Application oXL = new Microsoft.Office.Interop.Excel.Application();
                oXL.Visible = false;

                // Create a new Workbook.

                Microsoft.Office.Interop.Excel.Workbook oWB = oXL.Workbooks.Add(1);



                foreach (ProjectDescription project in projects)
                {
                    createContent(project, oXL);
                }

                // Save the workbook as a xlsx file and close it.

                Console.WriteLine("Save and close the workbook");

                string fileName = Path.GetDirectoryName(
                    Assembly.GetExecutingAssembly().Location) + "\\Sample2.xlsx";
                oWB.SaveAs(fileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook,
                    missing, missing, missing, missing,
                    Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
                    missing, missing, missing, missing, missing);
                oWB.Close(missing, missing, missing);

                // Quit the Excel application.

                Console.WriteLine("Quit the Excel application");

                // Excel will stick around after Quit if it is not under user 
                // control and there are outstanding references. When Excel is 
                // started or attached programmatically and 
                // Application.Visible = false, Application.UserControl is false. 
                // The UserControl property can be explicitly set to True which 
                // should force the application to terminate when Quit is called, 
                // regardless of outstanding references.
                oXL.UserControl = true;

                oXL.Quit();
                AutomateExcel();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Solution2.AutomateExcel throws the error: {0}",
                    ex.Message);
            }
        }
        private static void createSheet(Microsoft.Office.Interop.Excel.Application oXL, ProjectDescription project)
        {
            Worksheet sheet = getTemplate();
            oXL.Workbooks[1].Worksheets.Copy(sheet);
        }

        private static Worksheet getTemplate()
        { 
            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
            excelApp.Visible = false;
            string path = (Path.GetDirectoryName(
                    Assembly.GetExecutingAssembly().Location) + "\\Template.xlsx");
            Microsoft.Office.Interop.Excel.Workbook workbook = excelApp.Workbooks.Open(path,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
            Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            // The key line:
            Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];

            return worksheet;
        }

        private static void createContent(ProjectDescription project,Microsoft.Office.Interop.Excel.Application oXL)
        {
            createSheet(oXL, project);            
        }
    }
}

0 个答案:

没有答案