Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.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# 从WPF导出到Excel_C#_Wpf_Excel_Winforms - Fatal编程技术网

C# 从WPF导出到Excel

C# 从WPF导出到Excel,c#,wpf,excel,winforms,C#,Wpf,Excel,Winforms,我创建了一个WinForm,它将文本框中的数据添加到列表中,然后当表单关闭时,将数据导出到excel电子表格中。我正在尝试将我的WinForm转换为WPF,代码不再工作。我没有收到任何错误,只是没有创建电子表格。我是不是遗漏了什么 protected void OnFormClosing(object sender, EventArgs e) { if(signees.Count > 0) { initExcelSheet(

我创建了一个WinForm,它将文本框中的数据添加到列表中,然后当表单关闭时,将数据导出到excel电子表格中。我正在尝试将我的WinForm转换为WPF,代码不再工作。我没有收到任何错误,只是没有创建电子表格。我是不是遗漏了什么

protected void OnFormClosing(object sender, EventArgs e)
    {

        if(signees.Count > 0)
        {
            initExcelSheet();
        }

    }


    public void initExcelSheet()
    {

        EXL.Application oXL;
        EXL._Workbook oWB;
        EXL._Worksheet oWS;
        EXL.Range oRng;
        oXL = new Microsoft.Office.Interop.Excel.Application();
        oXL.Visible = true;

        //Get a new workbook.
        oWB = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add(""));
        oWS = (Microsoft.Office.Interop.Excel._Worksheet)oWB.ActiveSheet;

        oWS.Cells[1, 1] = "First Name";
        oWS.Cells[1, 2] = "Last Name";
        oWS.Cells[1, 3] = "Email";
        oWS.Cells[1, 4] = "Expected Graduation Date";
        oWS.Cells[1, 5] = "School";
        oWS.Cells[1, 6] = "Major";
        oWS.Cells[1, 7] = "Favorite Languages";

        oWS.get_Range("A1", "G1").Font.Bold = true;
        oWS.get_Range("A1", "G1").VerticalAlignment =
            Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;

        int count = 2;
        foreach(Person x in signees)
        {
            oWS.Cells[count, 1] = x.firstName;
            oWS.Cells[count, 2] = x.lastName;
            oWS.Cells[count, 3] = x.email;
            oWS.Cells[count, 4] = x.gradDate.ToString("yyyy-MM-dd");
            oWS.Cells[count, 5] = x.schoolName;
            oWS.Cells[count, 6] = x.fos;
            oWS.Cells[count, 7] = x.fcl;
            count += 1;
        }


        oRng = oWS.get_Range("A1", "G1");
        oRng.EntireColumn.AutoFit();
        try
        {
            oWB.SaveAs(fileName(school, DateTime.Now));
        }
        catch(Exception e)
        {
            Console.Write(e.ToString());
        }

        oWB.Close();

是否确实要使用Office互操作?由于未正确清理,在后台运行Excel时可能会遇到一些问题。并且需要在机器上安装Excel。使用托管库(如EPPlus、NPOI或OpenXMLSDK)或只输出CSV文件可能会更幸运。