Hi, guys in my previous I have shown how to export the Gridview into Document using OPENXML.

Now in this article I am gone to show how to export the grid view into Excel using OPENXML.

Open a new solution in your IDE.

Create XML file which help you to know the Structure of the excel document. like below:

Create XML

Now import the Following the Namespace:

  1. using DocumentFormat.OpenXml.Spreadsheet;
  2. using DocumentFormat.OpenXml.Packaging;
  3. using DocumentFormat.OpenXml;
  4. using System.IO.Packaging;
Now create a Excel template dynamically according to the column available in the Gridview. Use the Belo code:
  1. saveFileDialog1.ShowDialog();
  2. if (saveFileDialog1.FileName != "")
  3. {
  4. int columncount = dataGridView1.Columns.Count;
  5. string filepath = saveFileDialog1.FileName.ToString();
  6. SpreadsheetDocument ssd = SpreadsheetDocument.Create(filepath + ".xlsx", SpreadsheetDocumentType.Workbook);
  7. //Add work part to the Document
  8. WorkbookPart wbp = ssd.AddWorkbookPart();
  9. wbp.Workbook = new Workbook();
  10. //add work sheet to the work part
  11. WorksheetPart wsp = wbp.AddNewPart < WorksheetPart > ();
  12. wsp.Worksheet = new Worksheet(new SheetData());
  13. // add sheets
  14. Sheets sht = ssd.WorkbookPart.Workbook.AppendChild < Sheets > (new Sheets());
  15. // Append a new worksheet and associate it with the workbook.
  16. Sheet sheet = new Sheet()
  17. {
  18. Id = ssd.WorkbookPart.
  19. // create an new sheet
  20. GetIdOfPart(wsp), SheetId = 1, Name = "mySheet"
  21. };
  22. sht.Append(sheet);
  23. Worksheet worksheet = new Worksheet();
  24. SheetData sheetData = new SheetData();
  25. //create a new row, cell
  26. Row row = new Row();
  27. Cell[] cell = new Cell[columncount];
  28. // the below used to create temple of the existing gridview
  29. for (int i = 0; i < columncount; i++)
  30. {
  31. string[] columnhead = new string[columncount];
  32. string[] columnheadname = new string[]
  33. {
  34. "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"
  35. };
  36. columnhead[i] = dataGridView1.Columns[i].HeaderText.ToString();
  37. cell[i] = new Cell();
  38. //passing the cell value
  39. {
  40. CellReference = columnheadname[0].ToString(), DataType = CellValues.String,
  41. CellValue = new CellValue(columnhead[i])
  42. };
  43. row.Append(cell[i]);
  44. }
  45. }
  46. sheetData.Append(row);
  47. worksheet.Append(sheetData);
  48. wsp.Worksheet = worksheet;
  49. wbp.Workbook.Save();
  50. ssd.Close();
  51. exceldata(filepath);
After creating the Excel now pass the Gridview values one by one by using the Below Code.
  1. public void exceldata(String docName)
  2. {
  3. int rowcount = dataGridView1.Rows.Count;
  4. int columncount = dataGridView1.Columns.Count;
  5. using(SpreadsheetDocument document = SpreadsheetDocument.Open(docName + ".xlsx", true))
  6. {
  7. WorkbookPart wbPart = document.WorkbookPart;
  8. // check whether the sheet is exist or not
  9. IEnumerable < Sheet > sheets = document.WorkbookPart.Workbook.GetFirstChild < Sheets > ().Elements < Sheet > ().Where(s = > s.Name == "mySheet");
  10. if (sheets == null)
  11. {
  12. throw new ArgumentException("sheetName");
  13. }
  14. else
  15. {
  16. //get the ID of the sheet
  17. string sheetss = sheets.First().Id.Value;
  18. // get the workpartsheet of the exesting data
  19. WorksheetPart worksheetPart = (WorksheetPart) document.WorkbookPart.GetPartById(sheetss);
  20. //get the sheet data of the exsting data
  21. SheetData sheetData = worksheetPart.Worksheet.GetFirstChild < SheetData > ();
  22. for (int i = 0; i < rowcount; i++)
  23. {
  24. //Assign the column name dynamically using array
  25. string[] columnheadname = new string[]
  26. {
  27. "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"
  28. };
  29. //create a row
  30. Row row = new Row();
  31. //create the cell dynamically using array
  32. Cell[] cell = new Cell[columncount];
  33. for (int j = 0; j < columncount; j++)
  34. {
  35. // get the value in the grid view
  36. string data1 = dataGridView1.Rows[i].Cells[j].Value.ToString();
  37. cell[j] = new Cell()
  38. {
  39. CellReference = columnheadname[0].ToString(), DataType = CellValues.String,
  40. CellValue = new CellValue(data1)
  41. };
  42. row.Append(cell[j]);
  43. }
  44. sheetData.Append(row);
  45. }
  46. worksheetPart.Worksheet.Save();
  47. }
Design the Excel according to your wish.

Have fun!!!
Happy coding