Export to excel Using OPENXML  C# ,asp.net, mvc
:

public void exporttoexcelfromDataset(DataSet dataSet){

try{
                 
                        // Create a spreadsheet document by supplying the filepath.
                        // By default, AutoSave = true, Editable = true, and Type = xlsx.

                        MemoryStream ms = new MemoryStream();


                        HttpContext.Current.Response.ClearHeaders();
                        HttpContext.Current.Response.ClearContent();
                        HttpContext.Current.Response.Clear();
                        HttpContext.Current.Response.Buffer = true;
                        HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=Export.xlsx");
                        HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                        HttpContext.Current.Response.Charset = "";


                        SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(ms,
                         
                            SpreadsheetDocumentType.Workbook);


                        // Add a WorkbookPart to the document.
                        WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
                        workbookpart.Workbook = new Workbook();

                        // Add a WorksheetPart to the WorkbookPart.
                     
                   
                        // Add Sheets to the Workbook.
                           Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.
                            AppendChild(new Sheets());

                        UInt32Value o = 1;
                        // Append a new worksheet and associate it with the workbook.
                        foreach (DataTable dataTable in dataSet.Tables)
                        {
                         
                          WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
 worksheetPart.Worksheet = new Worksheet(new SheetData());
                        Sheet sheet = new Sheet()
                        {
                            Id = spreadsheetDocument.WorkbookPart.
                                GetIdOfPart(worksheetPart),
                            SheetId = o,
                            Name = dataTable.TableName
                        };

                        SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();

                        //Get number of columns & rows
                        int numberOfColumns = dataTable.Columns.Count;
                        int numberOfRows = dataTable.Rows.Count;

                        //Add column title as header
                        {
                            Row newExcelRow = new Row();
                            sheetData.Append(newExcelRow);
                            for (int colInx = 0; colInx < numberOfColumns; colInx++)
                            {
                                Cell cell = new Cell();
                                cell.CellReference = getCellName(0, colInx);
                                cell.DataType = CellValues.String;
                                CellValue cellValue = new CellValue();
                                cellValue.Text = dataTable.Columns[colInx].ColumnName;
                                cell.Append(cellValue);
                                newExcelRow.Append(cell);
                            }
                        }

                        for (int rowInx = 0; rowInx < numberOfRows; rowInx++)
                        {
                            DataRow dr = dataTable.Rows[rowInx];

                            Row newExcelRow = new Row();
                            sheetData.Append(newExcelRow);

                            for (int colInx = 0; colInx < numberOfColumns; colInx++)
                            {
                                Cell cell = new Cell();
                                cell.CellReference = getCellName(rowInx + 1, colInx);
                                cell.DataType = CellValues.String;
                                CellValue cellValue = new CellValue();
                                cellValue.Text = dr[colInx].ToString();
                                cell.Append(cellValue);
                                newExcelRow.Append(cell);
                            }
                        }


                        sheets.Append(sheet);
                         
                   
                            o++;
                        }
                   
 workbookpart.Workbook.Save();
                        // Close the document.
                        spreadsheetDocument.Close();

                        ms.WriteTo(HttpContext.Current.Response.OutputStream);
                        ms.Close();
                        HttpContext.Current.Response.End();
                    }
 catch (System.Threading.ThreadAbortException lException)
                    {
//as we are closing the response.end will end a thread so we will ignore it
                        // do nothing

                    }
}


 private StringValue getCellName(int row, int col)
        {
            row++;
            col++;

            StringBuilder sb = new StringBuilder();

            do
            {
                col--;
                sb.Insert(0, (char)('A' + (col % 26)));
                col /= 26;
            } while (col > 0);

            sb.Append(row);

            return sb.ToString();
        }

Comments

Popular posts from this blog