2016-09-22 48 views
0

我正在使用ClosedXML將gridview導出爲ex​​cel,我希望我的輸出具有一列分開。喜歡這個。 enter image description here導出gridview到excel使用封閉xml與1列分開

這是我對gridview標題部分的代碼。

  for (col = 0; col < headerCount; col++) 
      { 
       worksheet.Cell(3, current + 1).Value = GridView1.HeaderRow.Cells[col].Text; 

      } 

注意:我的gridview列數是動態的。

+0

你在哪裏定義和增量'current'? –

+0

對不起,應該是'col'不是當前,錯字錯誤。如果它能夠工作,我會在下面嘗試您的解決方案。 –

回答

1

試試這個:

for (col = 0; col < headerCount; col++) 
{ 
    worksheet.Cell(3, current * 2 + 1).Value = GridView1.HeaderRow.Cells[col].Text; 
} 
+0

非常感謝。有用! –

+0

如果我只希望第4列和後續列間隔1列,而前4列不是? –

+0

您將不得不使用正常的編程語句,「if」等,將值輸出到相關列。 –