【问题标题】:Hide datagridview column when exporting it to excel导出到excel时隐藏datagridview列
【发布时间】:2016-11-21 07:48:44
【问题描述】:

例如,如果 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; 
}

【问题讨论】:

  • 不清楚你在问什么。您想在 datagridview 中隐藏一列,还是不想将其导出到 excel 文件中?
  • 不导出到excel文件
  • 为您的列导出使用 2 个循环。例如,一个从 0 到您不想导出的索引 -1,另一个从索引到 dgven.Columns.Count。或者看看@mjb 答案

标签: c# winforms datagridview


【解决方案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; 
}

【讨论】:

    猜你喜欢
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    相关资源
    最近更新 更多