过滤w.r.t日期并从datagrid写入excel。 [英] Filter w.r.t dates and write to excel from datagrid.
本文介绍了过滤w.r.t日期并从datagrid写入excel。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hi Team,
I am trying to put a filter with input dates (From and To) before writing to excel as output.
I tried the below logic but it is writing all the data as it is in Grid to Excel. I am unable to go ahead.
Appreciates if someone can help me doing this?
The Grid displays as below :
Dates Name School Place
5/13/15 Shreyas ABC BLR
4/12/15 Amit XYZ MYS
6/24/15 Niraj MES BJP
11/10/15 Akshay NIT ENG
8/26/15 Natraj GVC BGK
Expected :
Examaple if the DateFrom : 5/13/15 and DateTo : 6/24/15.The output expected is :
Dates Name School Place
5/13/15 Shreyas ABC BLR
6/24/15 Niraj MES BJP
我的尝试: < br $>
What I have tried:
void WriteToExcel(DateTimePicker DateFrom,DateTimePicker DateTo)
{
ExcelWriter exlWtr = new ExcelWriter();
int rows = Grid.RowCount + 1;
int cols = Grid.ColumnCount;
if (rows == 0 || cols == 1)
return;
string[] colNames = new string[cols];
// Populate the Header names first
int i = 0;
for (int colItr = 0; colItr < cols; j++)
{
if (Grid.Columns[j].Visible)
{
colNames[i] = Grid.Columns[j].Name;
i++;
}
}
object[,] values = new object[rows, i];
//Copy the Column names
for (int j = 0; j < i; j++)
{
values[0, j] = Grid.Columns[colNames[j]].HeaderText;
}
//Get the data now
foreach (DataGridViewRow row in Grid.Rows)
{
for (int j = 0; j < i; j++)
{
//Grid[colNames[0], j].FormattedValue; // This displays the dates from the Grid
//May be here i need to add the logic to filter with dates and write
values[row.Index + 1, j] = Grid[colNames[j], row.Index].FormattedValue;
}
}
exlWtr.createFile(false);
exlWtr.writeRange(1, 1, 1, values); // Writes to Excel
exlWtr.SetHorizontalAllignment(1, 1, Grid.Rows.Count + 1, i, "LEFT");
exlWtr.SetVisibility(true);
}
推荐答案
Google搜索是你的朋友:c# export datagrid要擅长 [ ^ ]
Google Search is your friend: c# export datagrid to excel[^]
感谢Graeme提供的链接。
是的,我确实采取了谷歌的帮助,并尝试了我可能的方式,并最终编写excel来自网格的所有数据。
我以为任何大师都可以帮我看看代码我试着帮我解决错误的地方。
谢谢
Sharan
Thanks Graeme for the link provided.
Yes I did took help of google and tried the possible way I can and end up writing to excel all the data from Grid.
I thought any of the masters can help me out seeing the code which I tried and help me out where I am going wrong.
Thanks
Sharan
这篇关于过滤w.r.t日期并从datagrid写入excel。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文