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);
    }
manoj0790
  • 473
  • 4
  • 10
Hala
  • 121
  • 8
  • Don't use Interop - that's way to stressful. Have a look at this: https://stackoverflow.com/questions/151005/how-do-i-create-an-excel-xls-and-xlsx-file-in-c-sharp-without-installing-mic – kara Dec 06 '19 at 08:18

1 Answers1

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());
Falk
  • 23
  • 7
  • Thank you for answer, where should I put your code ? – Hala Dec 06 '19 at 08:40
  • 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 – Falk Dec 06 '19 at 08:43
  • I have error at foreach line code " col.Cells", the message error show me that datagridview dose not have defination of cells – Hala Dec 06 '19 at 08:50
  • 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. – Falk Dec 06 '19 at 09:04