简体   繁体   English

将DataTable导出到Excel工作表-创建时获取空行

[英]Export DataTable to Excel Sheet - Getting Empty Rows When Creating

I have a dataset that consists of 6 tables. 我有一个由6个表格组成的数据集。 When I place them into my method below, it works flawlessly on the first table. 当我将它们放入下面的方法中时,它在第一个表上可以正常工作。 However, I notice in the second table the second row (under the columns) is blank and then the table data is shown. 但是,我注意到在第二个表中,第二行(在列下方)为空白,然后显示了表数据。 This is the same for table three only this time there are two rows which are blank. 对于表三,这是相同的,只是这次有两行是空白的。 This increases the blank row as each remaining table is built. 随着其余每个表的建立,这将增加空白行。 I verified that the tables themselves do not have these empty rows. 我验证了表本身没有这些空行。 I am hoping that someone here would be so kind to look at this code and let me know what would be the cause of this. 我希望这里有人会很友好地看一下这段代码,并让我知道这是什么原因。

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;
}

I think it's your int k variable and iRow that are the problems. 我认为这是您的int k变量和iRow造成的。
You set 你设置

int k = 0;

outside your looping through the tables. 在您遍历表格之外。
Then inside the loop you have 然后在循环内

int iRow = 0 + k;

and then you increment k at the end of the loop. 然后在循环末尾增加k。 So for the first table you've got iRow = 0; 因此对于第一个表,您需要iRow = 0; second table, iRow = 1; 第二个表,iRow = 1; third table, iRow = 2. 第三张表,iRow = 2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM