6 개의 테이블로 구성된 데이터 집합이 있습니다. 내가 아래의 방법으로 그들을 넣으면, 그것은 첫 번째 테이블에서 완벽하게 작동합니다. 그러나 두 번째 테이블에서 두 번째 행 (열 아래)이 비어 있고 테이블 데이터가 표시됩니다. 이번에는 표 3에 대해 동일하게 두 행이 비어 있습니다. 나머지 테이블이 만들어지면 빈 행이 늘어납니다. 테이블 자체에 이러한 빈 행이 없는지 확인했습니다. 여기있는 누군가가이 코드를보고 그 원인이 무엇인지 알게되기를 바라고 있습니다.DataTable을 Excel 시트로 내보내기 - 빈 행 만들기
public Excel.Application PrepareForExport(System.Data.DataSet ds)
{
object missing = System.Reflection.Missing.Value;
Excel.Application excel = new Excel.Application();
Excel.Workbook workbook = excel.Workbooks.Add(missing);
int k = 0;
int tableCount = 6;
for (int j = 0; j < tableCount; j++)
{
Excel.Worksheet newWorksheet;
newWorksheet = (Excel.Worksheet)excel.Worksheets.Add(missing, missing, missing, missing);
System.Data.DataTable dt = new System.Data.DataTable();
dt = ds.Tables[j];
// name sheet
string tableName = string.Empty;
switch (j)
{
case 0:
tableName = "TEST1";
break;
case 1:
tableName = "TEST2";
break;
case 2:
tableName = "TEST3";
break;
case 3:
tableName = "TEST4";
break;
case 4:
tableName = "TEST5";
break;
case 5:
tableName = "TEST6";
break;
default:
tableName = "INVALID";
break;
}
newWorksheet.Name = tableName;
int iCol = 0;
foreach (DataColumn c in dt.Columns)
{
iCol++;
excel.Cells[1, iCol] = c.ColumnName;
}
int iRow = 0 + k;
foreach (DataRow r in dt.Rows)
{
iRow++;
for (int i = 1; i < dt.Columns.Count + 1; i++)
{
if (iRow == 1)
{
// Add the header the first time through
excel.Cells[iRow, i] = dt.Columns[i - 1].ColumnName;
}
if (r[1].ToString() != "")
{
excel.Cells[iRow + 1, i] = r[i - 1].ToString() + " - " + dt.Columns.Count;
}
}
}
k++;
}
return excel;
}
제목 앞에 "C#"등을 붙이지 마십시오. 그것이 바로 태그가있는 것입니다. –
죄송합니다! :( –
괜찮습니다. 이제 알았어. –