Currently attempting to export datagrid to an excel file.Right now as my code is the only thing that gets exported are the Headers. Below is the datagrid
<Page x:Class="ABSCCoastRefining.EndOfYear"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ABSCCoastRefining"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="EndOfYear">
<Page.Resources>
<CollectionViewSource x:Key="ItemCollectionViewSources" CollectionViewType="{x:Type ListCollectionView}"/>
</Page.Resources>
<Grid Background="#FFD1F1D1" Margin="0,-10,0,10">
<TextBlock Text="End Of Year" FontSize="18" FontWeight="Bold" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="22,10,0,0" Width="124" Background="White"/>
<DataGrid x:Name="dgEndOfYear"
ItemsSource="{Binding}"
DataContext="{StaticResource ItemCollectionViewSources}"
Margin="65,112,285,237"
Background="#FFE0F0E0"
AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserDeleteRows="False"
HorizontalAlignment="Center"
VerticalContentAlignment="Center"
IsReadOnly="True"
IsSynchronizedWithCurrentItem ="False" Width="450">
<DataGrid.Columns>
<DataGridTextColumn Header="Calendar Year" Binding="{Binding CalendarYear}" Width="Auto" FontWeight="Bold" />
<DataGridTextColumn Header="Month" Binding="{Binding MonthName}" Width="Auto" FontWeight="Bold"/>
<DataGridTextColumn Header="Total Gallons" Binding="{Binding SumGal, StringFormat=N0}" Width="Auto" FontWeight="Bold" />
</DataGrid.Columns>
And below is the code for the click event to export the data. dgEndOfYear.Items[i].Cells is causing issues and compiler states I am missing a reference I have made several attempts to resolve this on my own but have had no luck any help would be appreciated.
private void btnExcel_Click(object sender, EventArgs e)
{
// 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 = "Exported from gridview";
// storing header part in Excel
for (int i = 1; i < dgEndOfYear.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dgEndOfYear.Columns[i - 1].Header;
}
// storing Each row and column value to excel sheet
for (int i = 0; i < dgEndOfYear.Columns.Count - 1; i++)
{
for (int j = 0; j < dgEndOfYear.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dgEndOfYear.Items[i].Cells[j].Value.ToString();
}
}
// save the application
workbook.SaveAs("c:\\output.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// Exit from the application
app.Quit();
}