C# 将列表显示到excel单元格中
我想用c#将列表显示到excel单元格中。问题是只有列表的最后一项显示在excel工作表中。有什么帮助吗? 这是我的密码:C# 将列表显示到excel单元格中,c#,excel,C#,Excel,我想用c#将列表显示到excel单元格中。问题是只有列表的最后一项显示在excel工作表中。有什么帮助吗? 这是我的密码: Excel.Application xlApp ; Excel.Workbook xlWorkBook ; Excel.Worksheet xlWorkSheet ; object misValue = System.Reflection.Missing.Value; xlApp = new Excel.A
Excel.Application xlApp ;
Excel.Workbook xlWorkBook ;
Excel.Worksheet xlWorkSheet ;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "Nb de fonction";
xlWorkSheet.Cells[1, 3] = "list de fonction supprimé";
xlWorkSheet.Cells[1, 8] = "list de fonction ajouté";
foreach (var item in listfunc1)
{
xlWorkSheet.Cells[3, 8] = item;
xlWorkSheet.Cells[7, 8] = item;
}
xlWorkSheet.Cells[1, 12] = "list de commentaire supprimé";
xlWorkSheet.Cells[1, 16] = "list de commentaire ajouté";
xlWorkSheet.Cells[1, 19] = "list d'entête supprimé";
xlWorkSheet.Cells[1, 16] = "list d'entête ajouté";
xlWorkBook.SaveAs("csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
MessageBox.Show("Excel file created , you can find the file c:\\Documents\\csharp-Excel.xls");
尝试更改此选项:
foreach (var item in listfunc1)
{
xlWorkSheet.Cells[3, 8] = item;
xlWorkSheet.Cells[7, 8] = item;
}
为此:
int i=0;
foreach (var item in listfunc1)
{
xlWorkSheet.Cells[3+i, 8] = item;
xlWorkSheet.Cells[7+i, 8] = item;
i++;
}
我做了一些改变,现在可以用了
int row = 3;
int col = 8;
foreach (var item in listfunc1)
{
xlWorkSheet.Cells[row, col] = item;
row++;
if (listfunc1.IndexOf(item) == listfunc1.Count - 1)
col = 1;
}
这是因为您将所有项目放在相同的两个单元格中,因此每个项目都会覆盖上一个单元格,而循环结束时留在那里的项目是列表中的最后一个项目。为其中一个单元索引使用一个变量,并在foreach循环的每次迭代中修改该变量;这将导致每个项写入不同的单元格。即使这样,我也得到了相同的旧结果。您能告诉我如何获得
listfunc1
值吗?这就是如何获得listfunc1 List listfunc1=c.checkfunction(this.txtDirectory1.Text,this.txtDirectory.Text);foreach(listfunc1中的字符串项){MessageBox.Show(“les entete de fonction modifiédans la version 1:“+item”);}在MessageBox中,它显示了列表中的两个项。抱歉,我只能发表评论–感谢@user3801697的回复。调试时是否尝试检查listfunc1
的值?是不同的值还是相同的值?listfunc1返回不同的值