Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Please help on my below query which is on Export to Excel on IE11. below code is working fine on IE8 but it's not wokring on IE11 browser.
C#
HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.Buffer = true;
               // HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
                HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml";
                HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=ExportGridviewData.xlsx");
                HttpContext.Current.Response.Charset = "";

                MemoryStream ms = new MemoryStream();

                using (var excelSpreadSheet = SpreadsheetDocument.Create(ms, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
                {
                    var excelworkbookPart = excelSpreadSheet.AddWorkbookPart();
                    excelSpreadSheet.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();
                    excelSpreadSheet.WorkbookPart.Workbook.Sheets = new DocumentFormat.OpenXml.Spreadsheet.Sheets();

                    //Add a stylesheet to support the formatting
                    // Stylesheet
                    DocumentFormat.OpenXml.Packaging.WorkbookStylesPart workbookStylesPart;

                    workbookStylesPart = excelSpreadSheet.WorkbookPart.AddNewPart<DocumentFormat.OpenXml.Packaging.WorkbookStylesPart>();
                    workbookStylesPart.Stylesheet = new DocumentFormat.OpenXml.Spreadsheet.Stylesheet();
                    workbookStylesPart.Stylesheet.Save();

                    AddBasicStyles(excelSpreadSheet);

                    uint sheetId = 1;

                    foreach (System.Data.DataTable dataTable in pExportedDataSet.Tables)
                    {
                        var sheetPart = excelSpreadSheet.WorkbookPart.AddNewPart<WorksheetPart>();
                        var sheetData = new DocumentFormat.OpenXml.Spreadsheet.SheetData();
                        sheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(sheetData);

                        DocumentFormat.OpenXml.Spreadsheet.Sheets excelSheets = excelSpreadSheet.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>();
                        string relationshipId = excelSpreadSheet.WorkbookPart.GetIdOfPart(sheetPart);

                        if (excelSheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Count() > 0)
                        {
                            sheetId = excelSheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Select(s => s.SheetId.Value).Max() + 1;
                        }

                        DocumentFormat.OpenXml.Spreadsheet.Sheet excelsheet = new DocumentFormat.OpenXml.Spreadsheet.Sheet() { Id = relationshipId, SheetId = sheetId, Name = dataTable.TableName };
                        excelSheets.Append(excelsheet);

                        DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row();

                        List<String> dspColumns = new List<string>();
                        foreach (DataColumn HZLstcolumn in dataTable.Columns)
                        {
                            dspColumns.Add(HZLstcolumn.ColumnName);

                            DocumentFormat.OpenXml.Spreadsheet.Cell exlcell = new DocumentFormat.OpenXml.Spreadsheet.Cell();

                            //Headers are strings
                            exlcell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;

                            //Write out the string
                            exlcell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(HZLstcolumn.ColumnName);
                            headerRow.AppendChild(exlcell);

                        }

                        sheetData.AppendChild(headerRow);
                        foreach (DataRow exlDataRow in dataTable.Rows)
                        {
                            DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
                            foreach (String HZExlcol in dspColumns)
                            {
                                DocumentFormat.OpenXml.Spreadsheet.Cell rowCell = new DocumentFormat.OpenXml.Spreadsheet.Cell();


                                rowCell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
                                rowCell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(exlDataRow[HZExlcol].ToString());


newRow.AppendChild(rowCell);
            }
            sheetData.AppendChild(newRow);

        }

        //added for Merge

        //end added for meger

    }
}

ms.WriteTo(HttpContext.Current.Response.OutputStream);

//HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
HttpContext.Current.Response.Close();
System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();
Posted
Updated 25-Jun-15 9:43am
v2
Comments
ZurdoDev 25-Jun-15 16:09pm    
What does "not working" mean?
Member 11723737 25-Jun-15 16:51pm    
Thank you for reply. By using this code i am able to open the EXCEL sheet correctly if i am using IE8. The same code is throwing the error message while opening excel in IE11 browser.

Error Message Like:

Problem signature:
Problem Event Name: APPCRASH
Application Name: EXCEL.EXE
Application Version: 14.0.6117.5003
Application Timestamp: 4f622ef8
Fault Module Name: ntdll.dll
Fault Module Version: 6.3.9600.17736
Fault Module Timestamp: 550f42c2
Exception Code: c0000022
Exception Offset: 0009d4f2
OS Version: 6.3.9600.2.0.0.16.7
Locale ID: 1033
Additional Information 1: 1abe
Additional Information 2: 1abee00edb3fc1158f9ad6f44f0f6be8
Additional Information 3: 1abe
Additional Information 4: 1abee00edb3fc1158f9ad6f44f0f6be8

Read our privacy statement online:
http://go.microsoft.com/fwlink/?linkid=280262

If the online privacy statement is not available, please read our privacy statement offline:
C:\Windows\system32\en-US\erofflps.txt



This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900