每行仅打印5个项目

时间:2016-11-15 03:28:35

标签: c# excel for-loop

我目前有一份物品清单。

例如,

List <string> listOfItem = new List <string>()
{
  "item1",
  "item2",
  "item3",
  "item4",
  "item5",
  "item6",
  "item7",
  "item8",
  "item9",
  "item10",
  "item11",
}

我正在尝试进行Excel导出,我只打印5个第一项,然后移动到下一页并打印接下来的五个项目并重复该过程。

这是我尝试过的

int j = 0; 

for (int i = 0; i < listOfItem.Count; i+=4)
{
    if (listOfItem[i] != null){
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j], xlWorkSheet.Cells[8, (j + 1)]].Merge();
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j], xlWorkSheet.Cells[8, (j + 1)]] = orderDate[i];
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j], xlWorkSheet.Cells[8, (j + 1)]].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j], xlWorkSheet.Cells[8, (j + 1)]].VerticalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j], xlWorkSheet.Cells[8, (j + 1)]].Font.Size = "11";
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j], xlWorkSheet.Cells[8, (j + 1)]].Font.Name = "Arial";
    }
    if (listOfItem[i +1] != null){
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j+2], xlWorkSheet.Cells[8, (j + 3)]].Merge();
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j + 2], xlWorkSheet.Cells[8, (j + 3)]] = listOfItem[i+1];
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j + 2], xlWorkSheet.Cells[8, (j + 3)]].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j + 2], xlWorkSheet.Cells[8, (j + 3)]].VerticalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j + 2], xlWorkSheet.Cells[8, (j + 3)]].Font.Size = "11";
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j + 2], xlWorkSheet.Cells[8, (j + 3)]].Font.Name = "Arial";
    }
    if (listOfItem[i+2] != null){
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j+3], xlWorkSheet.Cells[8, (j + 5)]].Merge();
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j+4], xlWorkSheet.Cells[8, (j + 5)]] = listOfItem[i+2];
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j+4], xlWorkSheet.Cells[8, (j + 5)]].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j+4], xlWorkSheet.Cells[8, (j + 5)]].VerticalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j+4], xlWorkSheet.Cells[8, (j + 5)]].Font.Size = "11";
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j+4], xlWorkSheet.Cells[8, (j + 5)]].Font.Name = "Arial";
    }
    if (listOfItem[i+3] != null){
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j+6], xlWorkSheet.Cells[8, (j + 7)]].Merge();
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j+6], xlWorkSheet.Cells[8, (j + 7)]] = listOfItem[i+3];
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j+6], xlWorkSheet.Cells[8, (j + 7)]].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j+6], xlWorkSheet.Cells[8, (j + 7)]].VerticalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j+6], xlWorkSheet.Cells[8, (j + 7)]].Font.Size = "11";
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j+6], xlWorkSheet.Cells[8, (j + 7)]].Font.Name = "Arial";
    }
    if (listOfItem[i+4] != null){
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j+8], xlWorkSheet.Cells[8, (j + 9)]].Merge();
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j+8], xlWorkSheet.Cells[8, (j + 9)]] = listOfItem[i+4];
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j+8], xlWorkSheet.Cells[8, (j + 9]].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j+ 8], xlWorkSheet.Cells[8, (j + 9)]].VerticalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j + 8], xlWorkSheet.Cells[8, (j + 9)]].Font.Size = "11";
        xlWorkSheet.Range[xlWorkSheet.Cells[8, j + 8], xlWorkSheet.Cells[8, (j + 9)]].Font.Name = "Arial";
    }
}

我完成了if else是因为如果列表只有...示例,3个项目,那么由于if else循环,代码将基于3个项目执行。

但是,这段代码很冗长,有更精致/优雅的方法吗?

1 个答案:

答案 0 :(得分:0)

这可能会为你做到这一点

int j = 0; 
for (int i = 0; i < listOfItem.Count; i++)
{
    for(int j=0;j<((listOfItem.Count*2)-2);j+=2)
    {
        if (listOfItem[i] != null)
        {
            xlWorkSheet.Range[xlWorkSheet.Cells[8, j], xlWorkSheet.Cells[8, (j + 1)]].Merge();
            xlWorkSheet.Range[xlWorkSheet.Cells[8, j], xlWorkSheet.Cells[8, (j + 1)]] = listOfItem[i];
            xlWorkSheet.Range[xlWorkSheet.Cells[8, j], xlWorkSheet.Cells[8, (j + 1)]].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            xlWorkSheet.Range[xlWorkSheet.Cells[8, j], xlWorkSheet.Cells[8, (j + 1)]].VerticalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            xlWorkSheet.Range[xlWorkSheet.Cells[8, j], xlWorkSheet.Cells[8, (j + 1)]].Font.Size = "11";
            xlWorkSheet.Range[xlWorkSheet.Cells[8, j], xlWorkSheet.Cells[8, (j + 1)]].Font.Name = "Arial";
        }
    }
}

这将是一张工作表,您还可以提取更多工作表并将代码包装在foreach循环中,以便相应地将数据发送到另一张工作表。