仅将可见的datagridview列导出为ex​​cel [英] exporting only visible datagridview columns to excel

查看:66
本文介绍了仅将可见的datagridview列导出为ex​​cel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要帮助才能将可见的DataGridView列导出为ex​​cel,我有这个代码用于隐藏DataGridView中的列。

  .dg1.Columns [ 0 ]。可见=  false ; 



然后我有按钮点击事件导出到excel。

  //  创建Excel应用程序 
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();

// 在Excel应用程序中创建新的WorkBook
Microsoft。 Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);

// 在工作簿中创建新的Excel表格
Microsoft.Office .Interop.Excel._Worksheet worksheet = null ;

// 查看该计划背后的Excel工作表
app .Visible = true ;

// 获取第一张表的参考。默认情况下,其名称为Sheet1。
// 存储其对工作表的引用
worksheet = workbook.Sheets [ Sheet1];
worksheet = workbook.ActiveSheet;


// 更改活动工作表的名称
worksheet.Name = PIN korisnici;

// 在Excel中存储标题部分
for int i = 1 ; i < dg1.Columns.Count + 1 ; i ++)
{
worksheet.Cells [ 1 ,i] = dg1.Columns [i - 1 ]。HeaderText;
}
// 将每行和每列值存储到Excel工作表
for int i = 0 ; i < dg1.Rows.Count - 1 ; i ++)
{
for int j = 0 ; j < dg1.Columns.Count; j ++)
{
worksheet.Cells [i + 2 ,j + 1 ] = dg1.Rows [i] .Cells [j] .Value.ToString();
}
}





但我想只导出可见列,而我得到所有列,任何人,帮助。 :)

解决方案

你应该先检查一下这行是否可见这样



  //  将每行和每列值存储到Excel表格 
for int i = 0 ; i < dg1.Rows.Count - 1 ; i ++)
{
if (dg1.Rows [i] .Visible)
{
for int j = 0 ; j < dg1.Columns.Count; j ++)
{
worksheet.Cells [i + 2 ,j + 1 ] = dg1。行[I] .Cells [j]的.Value.ToString();
}
}
}





希望这有帮助


< blockquote>试试这个代码



for(iC = 0; iC< = colsTotal; iC ++)

{

if(dgvstocklist.Columns [iC] .Visible)

{

_with1.Cells [1,iC + 1] .Value = dgvstocklist.Columns [iC]。 HeaderText;

}



}

for(I = 0; I< = rowsTotal - 1; I ++)

{for(j = 0; j< = colsTotal; j ++)

{

if(dgvstocklist.Columns [j] .Visible)

{

_with1.Cells [I + 2,j + 1] .value = dgvstocklist.Rows [I] .Cells [j] .Value;

}



}

}



并将列(无论你想隐藏什么)放在最后在select命令中,并使这些列不可见。


嗨看看下面的链接



http://www.dotnetpools.com/2012/10/how-to -export-data-from-gridview-to.html [ ^ ]



http://www.dotnetpools.com/2012/09/gridview-export-to-excel-in-aspnet-c。 html [ ^ ]

Need help on exporting only visible DataGridView columns to excel, I have this code for hiding columns in DataGridView.

this.dg1.Columns[0].Visible = false;


And then I have button click event for exporting to excel.

// creating Excel Application
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();

// creating new WorkBook within Excel application
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);

// creating new Excelsheet in workbook
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;

// see the excel sheet behind the program
app.Visible = true;

// get the reference of first sheet. By default its name is Sheet1.
// store its reference to worksheet
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;


// changing the name of active sheet
worksheet.Name = "PIN korisnici";

// storing header part in Excel
for (int i = 1; i < dg1.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dg1.Columns[i - 1].HeaderText;
}
// storing Each row and column value to excel sheet
for (int i = 0; i < dg1.Rows.Count - 1; i++)
{
for (int j = 0; j < dg1.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dg1.Rows[i].Cells[j].Value.ToString();
}
}



But I want to export only visible columns, while I get all of them, anyone, help on this. :)

解决方案

You should do a check first to see if the row is visible first like this

// storing Each row and column value to excel sheet
            for (int i = 0; i < dg1.Rows.Count - 1; i++)
            {
                if (dg1.Rows[i].Visible)
                {
                    for (int j = 0; j < dg1.Columns.Count; j++)
                    {
                        worksheet.Cells[i + 2, j + 1] = dg1.Rows[i].Cells[j].Value.ToString();
                    }
                }
            }



Hope this helps


Try this code

for (iC = 0; iC <= colsTotal; iC++)
{
if (dgvstocklist.Columns[iC].Visible)
{
_with1.Cells[1, iC + 1].Value = dgvstocklist.Columns[iC].HeaderText;
}

}
for (I = 0; I <= rowsTotal - 1; I++)
{ for (j = 0; j <= colsTotal; j++)
{
if (dgvstocklist.Columns[j].Visible)
{
_with1.Cells[I + 2, j + 1].value = dgvstocklist.Rows[I].Cells[j].Value;
}

}
}

And put the Columns (whatever you want to hide) at last in the select command,and make those columns invisible.


Hi have a look of the below link

http://www.dotnetpools.com/2012/10/how-to-export-data-from-gridview-to.html[^]

http://www.dotnetpools.com/2012/09/gridview-export-to-excel-in-aspnet-c.html[^]


这篇关于仅将可见的datagridview列导出为ex​​cel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆