Save files from a DataGridVIew

7

I would like to know how do I get all the content that was passed to a DataGridView and save it to an Excel file. So far I have this:

private void btnDiretorio_Click(object sender, EventArgs e)
        {

            folderBrowserDialog.RootFolder = Environment.SpecialFolder.DesktopDirectory;
            folderBrowserDialog.SelectedPath = openFileDialog.InitialDirectory;
            folderBrowserDialog.ShowNewFolderButton = true;
            DialogResult result = folderBrowserDialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                List<string> selectedPath = listaArquivos(folderBrowserDialog.SelectedPath);

                foreach (string s in selectedPath)
                {
                    grvShowFile.Rows.Add(Path.GetFileName(s), s);
                }
            }
        }

This code is a button that selects all files in a directory and its subfolders. Note that the DataGridView is being filled in normally.

 private void btnPesquisar_Click(object sender, EventArgs e)
        {
            if (this.cboParametro.Text == "" || this.cboParametro.Text == "Ola"){
                MessageBox.Show("Parametro inválido, por favor tente novamente.");
            }

            string[] arrayParam = { "FROM", "SELECT", "WHERE", "UPDATE", "" };
            //Dictionary<string, string> getParam = new Dictionary<string, string>();
            //getParam.Add("FROM", "WHERE");
            //getParam.Add("SELECT", "Private");
            StreamReader DirectorySR = new StreamReader(folderBrowserDialog.SelectedPath);
            while (!DirectorySR.EndOfStream)
            {
                string read = DirectorySR.ReadToEnd();
                DirectorySR.Close();

            }
        }

This is where I'm getting lost, this button needs to receive a parameter that will be passed by the user (OK!) and then load the files that were passed in the DataGridView (That's where I can not continue) and read file by file passing that array that was created with the specific words.

I am a beginner in C # and can not continue this part.

    
asked by anonymous 17.12.2014 / 14:39

1 answer

1

You can work with Office interoperability objects

Here is an example of a function that exports data from a DataGridView

private void ExportToExcel()
{
    // Creating a Excel object.
    Microsoft.Office.Interop.Excel._Application excel = new Microsoft.Office.Interop.Excel.Application();
    Microsoft.Office.Interop.Excel._Workbook workbook = excel.Workbooks.Add(Type.Missing);
    Microsoft.Office.Interop.Excel._Worksheet worksheet = null;

    try
    {

        worksheet = workbook.ActiveSheet;

        worksheet.Name = "ExportedFromDatGrid";

        int cellRowIndex = 1;
        int cellColumnIndex = 1;

        //Loop through each row and read value from each column.
        for (int i = 0; i < dgvCityDetails.Rows.Count - 1; i++)
        {
            for (int j = 0; j < dgvCityDetails.Columns.Count; j++)
            {
                // Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
                if (cellRowIndex == 1)
                {
                    worksheet.Cells[cellRowIndex, cellColumnIndex] = dgvCityDetails.Columns[j].HeaderText;
                }
                else
                {
                    worksheet.Cells[cellRowIndex, cellColumnIndex] = dgvCityDetails.Rows[i].Cells[j].Value.ToString();
                }
                cellColumnIndex++;
            }
            cellColumnIndex = 1;
            cellRowIndex++;
        }

        //Getting the location and file name of the excel to save from user.
        SaveFileDialog saveDialog = new SaveFileDialog();
        saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
        saveDialog.FilterIndex = 2;

        if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            workbook.SaveAs(saveDialog.FileName);
            MessageBox.Show("Export Successful");
        }               
    }
    catch (System.Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        excel.Quit();
        workbook = null;
        excel = null;
    }

}

The full example you can find at the following link link

    
17.02.2017 / 20:15