Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I need to export the datagrid in only MSExcel not in CSV. Datagrid is populated through PagedviewCollection.

Can someone please help? Its urgent!!!
Posted
Comments
[no name] 21-Sep-13 8:44am    
I can assure you, it's not urgent at all.
"I need to export", okay ... and? Did you have some sort of a question or maybe your code is giving you some error? We cannot see your code so it's really hard to tell what it is that you have tried.
Trushal19 21-Sep-13 10:51am    
Here I am getting the error as "Current context not supported"

private void ExportExcel(object sender, RoutedEventArgs e)
{
try
{
int rowIndex = 1;
int coulmnIndex = 1;


dynamic excel = AutomationFactory.CreateObject("Excel.Application");

excel.workbooks.Add();

dynamic sheet = excel.ActiveSheet;

for (int i = 0; i < DataGrid.Columns.Count; ++i)
{
dynamic headerCell = sheet.Cells[rowIndex, coulmnIndex + i];
headerCell.Value = DataGrid.Columns[i].Header;
headerCell.Font.Bold = true;
headerCell.Interior.Color = 0xFF00;
}
PagedCollectionView well = model.Wells;
foreach (Well att in well.SourceCollection)
{
rowIndex++;

//column 1
dynamic cellFirstName = sheet.Cells[rowIndex, 1];

cellFirstName.Value = att.EngineeringComment;
cellFirstName.Font.Color = 003399;

//column 2
dynamic cellLastName = sheet.Cells[rowIndex, 2];
cellLastName.Value = att.Name;
cellLastName.Font.Color = 003399;

//column 3
dynamic cellAge = sheet.Cells[rowIndex, 3];
cellAge.Value = att.ChokePosition;
cellAge.Font.Color = 003399;

//column 4

dynamic cellSubscribed = sheet.Cells[rowIndex, 4];
cellSubscribed.Value = att.GasMRP;
cellSubscribed.Font.Color = 003399;

// Column5

dynamic cellSubscrib = sheet.Cells[rowIndex, 4];
cellSubscrib.Value = att.InterventionStatus;
cellSubscrib.Font.Color = 003399;

}
excel.Visible = true;
}
catch (Exception ex)
{
MessageBox.Show("Error generating excel: " + ex.Message);
}
}

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900