Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.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# 将两个列表的内容写入同一excel工作簿中的两个不同工作表_C#_Excel_List - Fatal编程技术网

C# 将两个列表的内容写入同一excel工作簿中的两个不同工作表

C# 将两个列表的内容写入同一excel工作簿中的两个不同工作表,c#,excel,list,C#,Excel,List,我正在处理两个C列表,需要将它们写入excel工作簿的两个不同的工作表Sheet1和Sheet2中,但不知道如何使用interop处理excel,我使用以下方法 public void ExportListToExcel(List<String> listExport,string sheetName) { try { Microsoft.Office.Interop.Excel._Application a

我正在处理两个C列表,需要将它们写入excel工作簿的两个不同的工作表Sheet1和Sheet2中,但不知道如何使用interop处理excel,我使用以下方法

public void ExportListToExcel(List<String> listExport,string sheetName)
        {
 try
            {

                Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
                Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
                worksheet = (Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets[sheetName];
                worksheet = (Microsoft.Office.Interop.Excel._Worksheet)workbook.ActiveSheet;

                for (int i = 1; i < listExport.Count + 1; i++)
                {
                    //int count = 1;
                    worksheet.Cells[i, 1] = listExport[i - 1];
                    //count++;
                }
                string fileDestination = @"C:\Atlas Applications\AxiomParser\axiom.xls";
                if (File.Exists(fileDestination))
                {
                    File.Delete(fileDestination);
                }

                workbook.SaveAs(fileDestination, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                workbook.Close(true, Type.Missing, Type.Missing);
                Process.Start(fileDestination);
                app.Quit();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

你犯了什么错误。。你不能只调用应用程序。退出你熟悉在使用互操作时处理ComObject吗。。。?请显示您如何两次调用它,以及获取的异常/错误消息。它在..处出错的是哪一行。。?或者I的值是多少当它出错时,我怀疑您的计数器是关闭的,这意味着您应该用I=0启动for循环,或者您需要执行listExport.Count vs listExport.Count+1尝试第一个工作表=Microsoft.Office.Interop.Excel.\u Worksheetworkbook.ActiveSheet;这是必需的吗。。只是想一想……这一行看起来也很混乱;能否在.cs文件顶部显示使用声明,说明如何使用Microsoft.Interop.Excel。。?
**Is there a better way to do it?**