我想将一个特定的列从DataGrid导出到Excel

时间:2018-12-11 07:41:34

标签: c# excel datagridview

这是我的代码。我想将Column6导出,并将其行导出到Excel工作表。我该怎么办?

        Excel.Application excel = new Excel.Application();
        excel.Visible = true;
        object Missing = Type.Missing;
        Workbook workbook = excel.Workbooks.Add(Missing);
        Worksheet worksheet = (Worksheet)workbook.Sheets[1];
        for (int i = 5; i < dataGridView6.Columns.Count; i++)
        {
            worksheet.Cells[1, 1] = dataGridView6.Columns[i].HeaderText;
        }

        for (int i = 0; i < dataGridView6.Rows.Count - 1; i++)
        {   
            for (int j = 5; j < dataGridView6.Columns.Count - 1; j++)
            {
                worksheet.Cells[1, 1] = dataGridView6.Rows[i].Cells[j].Value.ToString();
            }
        }


    }

0 个答案:

没有答案