将datagridview列导出到excel时隐藏它

时间:2016-11-21 07:48:45

标签: c# winforms datagridview

如果datagridview视图有3列,我怎么能不导出第三列。

我怎样才能实现这一点。

导出代码

private void btnexcel_Click(object sender, EventArgs e)
{
    Microsoft.Office.Interop.Excel.Application objexcelapp = new Microsoft.Office.Interop.Excel.Application();
    objexcelapp.Application.Workbooks.Add(Type.Missing);
    objexcelapp.Columns.ColumnWidth = 25;

    for (int i = 1; i < dgven.Columns.Count + 1; i++)
    {
        objexcelapp.Cells[1, i] = dgven.Columns[i - 1].HeaderText;
    }
    /*For storing Each row and column value to excel sheet*/
    for (int i = 0; i < dgven.Rows.Count; i++)
    {
        for (int j = 0; j < dgven.Columns.Count; j++)
        {
            if (dgven.Rows[i].Cells[j].Value != null)
            {
                objexcelapp.Cells[i + 2, j + 1] = dgven.Rows[i].Cells[j].Value.ToString();
            }
        }
    }
    string excelFilename = "Vendors";
    MessageBox.Show("Your excel file exported successfully at D:\\" + excelFilename + ".xlsx");
    objexcelapp.ActiveWorkbook.SaveCopyAs("D:\\" + excelFilename + ".xlsx");
    objexcelapp.ActiveWorkbook.Saved = true; 
}

1 个答案:

答案 0 :(得分:1)

private void btnexcel_Click(object sender, EventArgs e)
{
    Dictionary<int,bool> dicSkip = new Dictionary<int,bool>();
    dicSkip[dgvColThree.Index] = true;
    dicSkip[dgvColFour.Index] = true;

    Microsoft.Office.Interop.Excel.Application objexcelapp = new Microsoft.Office.Interop.Excel.Application();
    objexcelapp.Application.Workbooks.Add(Type.Missing);
    objexcelapp.Columns.ColumnWidth = 25;

    int colcount = 0;

    for (int i = 1; i < dgven.Columns.Count + 1; i++)
    {
        if (dicSkip.ContainsKey(i-1))
            continue;

        colcount++;
        objexcelapp.Cells[1, colcount] = dgven.Columns[colcount - 1].HeaderText;
    }

    /*For storing Each row and column value to excel sheet*/

    for (int i = 0; i < dgven.Rows.Count; i++)
    {
        colcount=0;

        for (int j = 0; j < dgven.Columns.Count; j++)
        {
            if (dicSkip.ContainsKey(j))
               continue;

            colcount++;

            if (dgven.Rows[i].Cells[j].Value != null)
            {
                objexcelapp.Cells[i + 2, colcount + 1] = dgven.Rows[i].Cells[j].Value.ToString();
            }
        }
    }
    string excelFilename = "Vendors";
    MessageBox.Show("Your excel file exported successfully at D:\\" + excelFilename + ".xlsx");
    objexcelapp.ActiveWorkbook.SaveCopyAs("D:\\" + excelFilename + ".xlsx");
    objexcelapp.ActiveWorkbook.Saved = true; 
}