0

I have excel data as follow:


Project | person | Time | Date

support | A |50 | 2019-10-10

IT | B |1,20 |2019-10-10

debugg | A |30 |2019-10-11

support | c |20 |2019-10-11

support | A |30 |2019-10-12

IT | B | 1.20 |2019-10-12


In my code I can export all excel data from datagridview, how I do to export one column only. I want to export column Project and do sort for every type of project and sum e.g the exported new excel file should be as follow:

2019-10-10 to 2019-10-12

Project | Sum

support | 1.40

IT | 2.40

debugg | 30

Here is my export code. can you help me please.

Thank you for hand.

 private void copyAlltoClipboard()
    {
        dataGridView1.SelectAll();
        DataObject dataObj = dataGridView1.GetClipboardContent();
        if (dataObj != null)
            Clipboard.SetDataObject(dataObj);
    }

    private void Button4_Click(object sender, EventArgs e)
    {
        copyAlltoClipboard();
        Microsoft.Office.Interop.Excel.Application xlexcel;
        Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
        Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;
        xlexcel = new Excel.Application();
        xlexcel.Visible = true;
        xlWorkBook = xlexcel.Workbooks.Add(misValue);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1];
        CR.Select();
        xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
    }
1

1 Answer 1

1

For the way you did it you could use something like go through your columns, check which one is selected and then export it, or directly select your column. But there is still room for improvement, I just wrote down what came to my mind.

 StringBuilder strContent = new StringBuilder();
 foreach (DataGridViewColumn col in dgv.Columns)
 {
     if(col.Selected)
     {
         foreach (DataGridViewRow row in dgv.Rows)
         {
            strContent.Append(row.Cells[col.Index].Value.ToString());
            strContent.Append("\t");
        }
     }
  }
  Clipboard.SetText(strContent.ToString());
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for answer, where should I put your code ?
Either make a new function for just column selection or use it in your copyAlltoClipboard() function and check if you either selected one or multiple/all columns
I have error at foreach line code " col.Cells", the message error show me that datagridview dose not have defination of cells
Oh, my bad, I edited the code above. Then you have to iterate trough the rows instead of cells, which would make it slower if multiple are selected, as i said, there is surely some stuff to improve.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.