2

I'am working for importing an excel file into my DataGridView. But how do I import excel file with selected rows and column in my DataGridView? I have only code for load the whole excel file to my DataGridView, I'm new in C#

I have open dialogfile and search for the excel file, let's say my data starts in C:34,D:34 and E:34 in one column or data that have a EmploayeeName and select the top 24 rows and load it to my DataGridView.

Thank you in advance for giving a hand! This is the only stuff that I have :(

private void OpenFile_Click(object sender, EventArgs e)
{
    OpenFileDialog fdlg = new OpenFileDialog();
    fdlg.Title = "Select file";
    fdlg.InitialDirectory = @"c:\";
    fdlg.FileName = txtFileName.Text;
    fdlg.Filter = "Excel Sheet(*.xlsx)|*.xlsx|All Files(*.*)|*.*";
    fdlg.FilterIndex = 1;
    fdlg.RestoreDirectory = true;
    if (fdlg.ShowDialog() == DialogResult.OK)
    {
        path = textBox1.Text;
        txtFileName.Text = fdlg.FileName;

        Application.DoEvents();
    }
}

private void LoadExcel_Click(object sender, EventArgs e)
{
    System.Data.OleDb.OleDbConnection MyConnection;
    System.Data.DataSet DtSet;
    System.Data.OleDb.OleDbDataAdapter MyCommand;
    MyConnection = new System.Data.OleDb.OleDbConnection(@"provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\csharp.net-informations.xls';Extended Properties=Excel 8.0;");
    MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
    MyCommand.TableMappings.Add("Table", "Net-informations.com");
    DtSet = new System.Data.DataSet();
    MyCommand.Fill(DtSet);
    dgrdReciver.DataSource = DtSet.Tables[0];
    MyConnection.Close();
}
zx485
  • 28,498
  • 28
  • 50
  • 59
Michael
  • 37
  • 1
  • 4
  • Your question is unclear. First, if you want to get the “selected” cells from an Excel file… then you are going to have to talk to “Excel.” When you “import” an excel file, the selected cells info is not going to be available. In addition, opening an excel and then immediately grabbing the selected cells… seems like a strange thing to do since you could not possibly know (in every case) what those “selected” cells are. The opening of the excel file “implies” that the user will select the cells and “then” signal through some mechanism that the copy should be performed. – JohnG Sep 14 '18 at 01:00
  • I am guessing you may be making this more complicated than it has to be. Have you considered, making two (2) grids. Initially, both grids would contain the same “copies” of the original excel data as you already have. Then… performing what you describe would be much easier as you would then “know” what cells “are” selected. In addition, it will be more work having to interface with an excel library to gain access to the excel variables. Just a thought. – JohnG Sep 14 '18 at 01:01
  • You can use OpenXml to read selected row/cells of excel. https://stackoverflow.com/questions/3321082/from-excel-to-datatable-in-c-sharp-with-open-xml – umesh shukla Sep 16 '18 at 11:12

1 Answers1

0

Well, I would start off by getting the path of the file and then using a file stream like this:

string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), file.Name);


    using (Stream fileStream = File.OpenWrite(path))
    {

        // do what you want with the file stream.
        sftp.DownloadFile(remoteDirectory + "/" + file.Name, fileStream);



    }

I would even put that data into a SQL Server so it is easier to put into a data grid view.

Justin W
  • 52
  • 1
  • 6