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:
I'm trying to import specific Columns from Excel spreadsheets. No spreadsheets are the same and all have different headers. I have a solution for the headers being different, the main issue I have is I only want specific columns, which I can search via user modified headers, but how would I go about saying if these are the titles of the headers I want, just grab those columns? the code below imports the whole spreadsheet. I'm guessing the answer is somewhere in binding [which I really need to find time to play around with more], or hiding unwanted columns, but I'm not having luck with either.

Just to be clear, if I have 20 columns in a spreadsheet, all I want at most is 4 in my datagrid

* XAML

XML
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="7*"/>
        <ColumnDefinition Width="124*"/>
        <ColumnDefinition Width="127*"/>
        <ColumnDefinition Width="253*"/>
        <ColumnDefinition Width="6*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="5*"/>
        <RowDefinition Height="35*"/>
        <RowDefinition Height="33*"/>
        <RowDefinition Height="241*"/>
        <RowDefinition Height="6*"/>
    </Grid.RowDefinitions>
    <TextBox Name="filePath" Margin="3,5,4,2" FontSize="15" Opacity="1" Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2"  />
    <Button Content="Browse" Name="browse" FontSize="14" FontWeight="Bold" Click="browse_Click" Margin="3,5,2,2" Grid.Row="1" Grid.Column="1"  />
    <DataGrid AutoGenerateColumns="True" IsReadOnly="True" x:Name="dtGrid" Margin="3,2.568,4,4" Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="3" />
    <Button Content="Template 1" x:Name="template1" FontSize="14" FontWeight="Bold" Click="template1_Click" Margin="3,3,2,2.432" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2"  />
    <Button Content="Template 2" x:Name="template2" FontSize="14" FontWeight="Bold" Click="template2_Click" Margin="2,3,4,2.432" Grid.Row="2" Grid.Column="3"  />
</Grid>


* CODE-BEHIND

private void template1_Click(object sender, RoutedEventArgs e)
{
    Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
    Workbook excelBook = excelApp.Workbooks.Open(filePath.Text.ToString(), 0, true, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
    Worksheet excelSheet = (Worksheet)excelBook.Worksheets.get_Item(1); ;
    Range excelRange = excelSheet.UsedRange;

    string strCellData = "";
    double douCellData;
    int rowCnt = 0;
    int colCnt = 0;

    System.Data.DataTable dt = new System.Data.DataTable();
    for (colCnt = 1; colCnt <= excelRange.Columns.Count; colCnt++)
    {
        string strColumn = "";
        strColumn = (string)(excelRange.Cells[1, colCnt] as Range).Value2;
        dt.Columns.Add(strColumn, typeof(string));
    }

    for (rowCnt = 2; rowCnt <= excelRange.Rows.Count; rowCnt++)
    {
        string strData = "";
        for (colCnt = 1; colCnt <= excelRange.Columns.Count; colCnt++)
        {
            try
            {
                strCellData = (string)(excelRange.Cells[rowCnt, colCnt] as Range).Value2;
                strData += strCellData + "|";
            }
            catch
            {
                douCellData = (excelRange.Cells[rowCnt, colCnt] as Range).Value2;
                strData += douCellData.ToString() + "|";
            }
        }
        strData = strData.Remove(strData.Length - 1, 1);
        dt.Rows.Add(strData.Split('|'));
    }

    dtGrid.ItemsSource = dt.DefaultView;

    excelBook.Close(true, null, null);
    excelApp.Quit();
}
Posted

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