Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';t根据模板创建excel文件_C#_Excel_Interop - Fatal编程技术网

C# Can';t根据模板创建excel文件

C# Can';t根据模板创建excel文件,c#,excel,interop,C#,Excel,Interop,问题点: 1.我有一个对象列表,需要将它们导出到excel文件中。每个对象必须位于一张单独的图纸上,图纸由模板构成。 我的算法: 1.在项目中创建excel模板文件 2.对于列表中的每个对象,复制excel文件中的模板表并填写内容 为此,我创建了ExcelWorker类并将对象列表传递给AutomateExcelImpl方法。问题在于这个应用程序。投掷。例外 oXL.工作簿[1]。工作表。复印件(张); 我在谷歌上搜索了一下,但在这个问题上我差点撞到脑袋。 请帮忙 using JiraExamp

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

为此,我创建了ExcelWorker类并将对象列表传递给AutomateExcelImpl方法。问题在于这个应用程序。投掷。例外 oXL.工作簿[1]。工作表。复印件(张); 我在谷歌上搜索了一下,但在这个问题上我差点撞到脑袋。 请帮忙

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);            
        }
    }
}
使用JiraExample.Entities.Projects;
使用Microsoft.Office.Interop.Excel;
使用制度;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
运用系统反思;
使用系统文本;
名称空间JiraExample.Helpers
{
班主任
{
公共静态void AutomateExcel()
{
//AutomateExcelImpl();
//通过强制垃圾清理来清理非托管Excel COM资源
//调用函数离开堆栈后立即收集(在
//这些对象不再是根的点)。
GC.Collect();
GC.WaitForPendingFinalizers();
//需要调用两次GC才能调用终结器
//-第一次,它只是简单地列出了要做的事情
//定稿,第二次在,它实际上是定稿。只有
//然后,对象将执行其自动释放对象。
GC.Collect();
GC.WaitForPendingFinalizers();
}
公共静态void AutomateExcelImpl(列出项目)
{
对象缺失=类型。缺失;
尝试
{
//创建Microsoft Excel实例并使其不可见。
Microsoft.Office.Interop.Excel.Application oXL=新的Microsoft.Office.Interop.Excel.Application();
可见=假;
//创建新工作簿。
Microsoft.Office.Interop.Excel.Workbook oWB=oXL.Workbooks.Add(1);
foreach(项目中的项目描述项目)
{
createContent(项目,oXL);
}
//将工作簿另存为xlsx文件并将其关闭。
Console.WriteLine(“保存并关闭工作簿”);
字符串文件名=Path.GetDirectoryName(
Assembly.getExecutionGassembly().Location)+“\\Sample2.xlsx”;
oWB.SaveAs(文件名,Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook,
失踪,失踪,失踪,失踪,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
失踪,失踪,失踪,失踪,失踪,失踪);
oWB.关闭(缺失、缺失、缺失);
//退出Excel应用程序。
Console.WriteLine(“退出Excel应用程序”);
//如果Excel不在用户权限下,则退出后Excel将保留
//控件,并且存在未完成的引用
//以编程方式启动或附加,并且
//Application.Visible=false,Application.UserControl为false。
//UserControl属性可以显式设置为True
//应在调用Quit时强制应用程序终止,
//不考虑未完成的引用。
oXL.UserControl=true;
oXL.Quit();
AutomateExcel();
}
捕获(例外情况除外)
{
WriteLine(“Solution2.AutomateExcel抛出错误:{0}”,
例如,信息);
}
}
专用静态void createSheet(Microsoft.Office.Interop.Excel.Application oXL,ProjectDescription项目)
{
工作表=getTemplate();
oXL.工作簿[1]。工作表。复印件(张);
}
私有静态工作表getTemplate()
{ 
Microsoft.Office.Interop.Excel.Application excelApp=新的Microsoft.Office.Interop.Excel.Application();
excelApp.Visible=false;
字符串路径=(path.GetDirectoryName)(
Assembly.getExecutionGassembly().Location)+“\\Template.xlsx”);
Microsoft.Office.Interop.Excel.Workbook工作簿=excelApp.Workbooks.Open(路径,
类型。缺失,类型。缺失,类型。缺失,类型。缺失,类型。缺失,类型。缺失,
类型。缺失,类型。缺失,类型。缺失,类型。缺失,类型。缺失,类型。缺失,
类型。缺失,类型。缺失,类型。缺失,类型。缺失,类型。缺失);
//关键是:
Microsoft.Office.Interop.Excel.Worksheet Worksheet=(Microsoft.Office.Interop.Excel.Worksheet)工作簿。工作表[1];
返回工作表;
}
私有静态void createContent(ProjectDescription项目,Microsoft.Office.Interop.Excel.Application oXL)
{
createSheet(oXL,项目);
}
}
}

引发了什么异常?来自HRESULT的异常:0x800AC472我认为这个问题正是您想要的;见第二个答案: