Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7518169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:40:49+00:00 2026-05-30T01:40:49+00:00

I am using jqgrid (standard) with EF 4 + MVC3 . I’d like to

  • 0

I am using jqgrid (standard) with EF 4 + MVC3. I’d like to implement excel export. Which method you would suggest me?

To generate excel, I’d like to use this library by Dr Stephen Walther, which has three types of output and allows to define headers too. Please tell me if you find it valid for my purpose.

I ask this question because I am still approaching to implement excel export and I found several techniques. Some suggest making a csv export, others indicate that it should return a JSON output and it is not clear to me whether this capability is present in the free version of jqgrid. In any case, I would like to pass the data to Walther’s object.

About the jqgrid code, I found this interesting answer by Oleg, but I do not understand if could be applied to my needs.

Unfortunately, by now I only found parts of solutions for excel export with EF MVC, but no solution or complete examples…

About the MVC logic, I am going to implement and develop this code as kindly suggested by @Tommy.

Please sorry if the question could be silly, I am just a (enthusiast) beginner.

Thanks for your precious help!
Best Regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:40:51+00:00Added an answer on May 30, 2026 at 1:40 am

    As I wrote before (see here and here for example) the best way to export grid data to XML is the usage of Open XML SDK 2.0.

    The post of Dr Stephen Walther shows how to create HTML file which can be read by Excel. It’s not Excel file and have to be still converted to Excel format. The usage of CSV has even more problems. Depend on the content in the source table the automatic conversion to Excel data types can be absolutely wrong. In one project which I developed for a customer the grid contained information about software products: product name, version, and so on. The software version looks sometime as the date (1.3.1963 for example) and such cells will be wrong converted (in German one use ‘.’ as the separator in the date). As the result one had really hard problems. The usage of CSV with texts having commas inside will be also frequently wrong imported. Even when one quotes the cells having commas (,) and escaped the texts having quotas the import still be wrong especially in the first column. I don’t want to explain here the whole history of all attempts and errors, but after all I decide to give up with the usage of CSV and HTML and started to use Open XML SDK 2.0 which allows to create real Excel files with extension XLSX. The way seems me perfect because one don’t need any Office
    components installed on the server, no additional licenses.

    The only restriction is that one should be able to use DocumentFormat.OpenXml.dll, so your server program should run on any Windows operation system. As it’s well known, XLSX file is ZIP file which contains some XML files inside. If you still don’t know that I recommend you to rename the XLSX file to ZIP file and extract it. The Open XML SDK 2.0 is the library which works with XLSX file like with XML files. So no additional Office components are required.

    One can find a lot of information how to use Open XML SDK 2.0 (see here, here and here). Many helpful code examples one cam find directly on the MSDN (see here). Nevertheless the practical usage of Open XML SDK 2.0 is not so easy at least at the first time. So I created a demo from the parts of the code which I used myself.

    You can download the demo project from here. The demo is an extension of the demos from the answer and this one.

    To export data I use the DataForExcel helper class. It has constructor in the form

    DataForExcel(string[] headers, DataType[] colunmTypes, List<string[]> data,
                 string sheetName)
    

    or in a little simplified form

    DataForExcel(string[] headers, List<string[]> data, string sheetName)
    

    and the only public method

    CreateXlsxAndFillData(Stream stream)
    

    The usage of the class to create Excel file can be like the following

    var excelData = new DataForExcel (
        // column Header
        new[]{"Col1", "Col2", "Col3"},
        new[]{DataForExcel.DataType.String, DataForExcel.DataType.Integer,
              DataForExcel.DataType.String},
        new List<string[]> {
            new[] {"a", "1", "c1"},
            new[] {"a", "2", "c2"}
        },
        "Test Grid");
    Stream stream = new FileStream ("Test.xlsx", FileMode.Create);
    excelData.CreateXlsxAndFillData (stream);
    stream.Close();
    

    The usage in the demo from ASP.NET MVC is the following

    static readonly string[] HeadersQuestions = {
        "Id", "Votes", "Title"
    };
    static readonly DataForExcel.DataType[] ColunmTypesQuestions = {
        DataForExcel.DataType.Integer,
        DataForExcel.DataType.Integer,
        DataForExcel.DataType.String
    };
    
    public ActionResult ExportAllQuestionsToExcel () {
        var context = new HaackOverflowEntities ();
        var questions = context.Questions;
        questions.MergeOption = MergeOption.NoTracking; // we don't want to update the data
    
        // to be able to use ToString() below which is NOT exist in the LINQ to Entity
        // we should include in query only the properies which we will use below
        var query = questions.ToList ();
        if (query.Count == 0)
            return new EmptyResult ();
        var data = new List<string[]> (query.Count);
        data.AddRange (query.Select (item => new[] {
            item.Id.ToString(CultureInfo.InvariantCulture),
            item.Votes.ToString(CultureInfo.InvariantCulture),
            item.Title
        }));
    
        return new ExcelResult (HeadersQuestions, ColunmTypesQuestions, data,
                                "Questions.xlsx", "Questions");
    }
    

    where ExcelResult are defined as

    public class ExcelResult : ActionResult {
        private readonly DataForExcel _data;
        private readonly string _fileName;
    
        public ExcelResult (string[] headers, List<string[]> data, string fileName, string sheetName) {
            _data = new DataForExcel (headers, data, sheetName);
            _fileName = fileName;
        }
    
        public ExcelResult (string[] headers, DataForExcel.DataType[] colunmTypes, List<string[]> data, string fileName, string sheetName) {
            _data = new DataForExcel (headers, colunmTypes, data, sheetName);
            _fileName = fileName;
        }
    
        public override void ExecuteResult (ControllerContext context) {
            var response = context.HttpContext.Response;
            response.ClearContent();
            response.ClearHeaders();
            response.Cache.SetMaxAge (new TimeSpan (0));
    
            using (var stream = new MemoryStream()) {
                _data.CreateXlsxAndFillData (stream);
    
                //Return it to the client - strFile has been updated, so return it. 
                response.AddHeader ("content-disposition", "attachment; filename=" + _fileName);
    
                // see http://filext.com/faq/office_mime_types.php
                response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                response.ContentEncoding = Encoding.UTF8;
                stream.WriteTo (response.OutputStream);
            }
            response.Flush();
            response.Close();
        }
    }
    

    To make the code full I have to include the code of the class DataForExcel:

    public class DataForExcel {
        public enum DataType {
            String,
            Integer
        }
        private readonly string[] _headers;
        private readonly DataType[] _colunmTypes;
        private readonly List<string[]> _data;
        private readonly string _sheetName = "Grid1";
        private readonly SortedSet<string> _os = new SortedSet<string> ();
        private string[] _sharedStrings;
    
        private static string ConvertIntToColumnHeader(int index) {
            var sb = new StringBuilder ();
            while (index > 0) {
                if (index <= 'Z' - 'A') // index=0 -> 'A', 25 -> 'Z'
                    break;
                sb.Append (ConvertIntToColumnHeader (index / ('Z' - 'A' + 1) - 1));
                index = index % ('Z' - 'A' + 1);
            }
            sb.Append ((char)('A' + index));
            return sb.ToString ();
        }
    
        private static Row CreateRow(UInt32 index, IList<string> data) {
            var r = new Row { RowIndex = index };
            for (var i = 0; i < data.Count; i++)
                r.Append (new OpenXmlElement[] { CreateTextCell (ConvertIntToColumnHeader (i), index, data[i]) });
    
            return r;
        }
    
        private Row CreateRowWithSharedStrings(UInt32 index, IList<string> data) {
            var r = new Row { RowIndex = index };
            for (var i = 0; i < data.Count; i++)
                r.Append (new OpenXmlElement[] { CreateSharedTextCell (ConvertIntToColumnHeader (i), index, data[i]) });
    
            return r;
        }
    
        private Row CreateRowWithSharedStrings(UInt32 index, IList<string> data, IList<DataType> colunmTypes) {
            var r = new Row { RowIndex = index };
            for (var i = 0; i < data.Count; i++)
                if (colunmTypes != null && i < colunmTypes.Count && colunmTypes[i] == DataType.Integer)
                    r.Append (new OpenXmlElement[] { CreateNumberCell (ConvertIntToColumnHeader (i), index, data[i]) });
                else
                    r.Append (new OpenXmlElement[] { CreateSharedTextCell (ConvertIntToColumnHeader (i), index, data[i]) });
    
            return r;
        }
    
        private static Cell CreateTextCell(string header, UInt32 index, string text) {
            // create Cell with InlineString as a child, which has Text as a child
            return new Cell (new InlineString (new Text { Text = text })) {
                // Cell properties
                DataType = CellValues.InlineString,
                CellReference = header + index
            };
        }
    
        private Cell CreateSharedTextCell(string header, UInt32 index, string text) {
            for (var i=0; i<_sharedStrings.Length; i++) {
                if (String.Compare (_sharedStrings[i], text, StringComparison.Ordinal) == 0) {
                    return new Cell (new CellValue { Text = i.ToString (CultureInfo.InvariantCulture) }) {
                        // Cell properties
                        DataType = CellValues.SharedString,
                        CellReference = header + index
                    };
                }
            }
            // create Cell with InlineString as a child, which has Text as a child
            throw new InstanceNotFoundException();
        }
    
        private static Cell CreateNumberCell(string header, UInt32 index, string numberAsString) {
            // create Cell with CellValue as a child, which has Text as a child
            return new Cell (new CellValue { Text = numberAsString }) {
                // Cell properties
                CellReference = header + index
            };
        }
    
        private void FillSharedStringTable(IEnumerable<string> data) {
            foreach (var item in data)
                _os.Add (item);
        }
    
        private void FillSharedStringTable(IList<string> data, IList<DataType> colunmTypes) {
            for (var i = 0; i < data.Count; i++)
                if (colunmTypes == null || i >= colunmTypes.Count || colunmTypes[i] == DataType.String)
                    _os.Add (data[i]);
        }
    
        public DataForExcel(string[] headers, List<string[]> data, string sheetName) {
            _headers = headers;
            _data = data;
            _sheetName = sheetName;
        }
    
        public DataForExcel(string[] headers, DataType[] colunmTypes, List<string[]> data, string sheetName) {
            _headers = headers;
            _colunmTypes = colunmTypes;
            _data = data;
            _sheetName = sheetName;
        }
    
        private void FillSpreadsheetDocument(SpreadsheetDocument spreadsheetDocument) {
            // create and fill SheetData
            var sheetData = new SheetData ();
    
            // first row is the header
            sheetData.AppendChild (CreateRow (1, _headers));
    
            //const UInt32 iAutoFilter = 2;
            // skip next row (number 2) for the AutoFilter
            //var i = iAutoFilter + 1;
            UInt32 i = 2;
    
            // first of all collect all different strings in OrderedSet<string> _os
            foreach (var dataRow in _data)
                if (_colunmTypes != null)
                    FillSharedStringTable (dataRow, _colunmTypes);
                else
                    FillSharedStringTable (dataRow);
            _sharedStrings = _os.ToArray ();
    
            foreach (var dataRow in _data)
                sheetData.AppendChild (_colunmTypes != null
                                          ? CreateRowWithSharedStrings (i++, dataRow, _colunmTypes)
                                          : CreateRowWithSharedStrings (i++, dataRow));
    
            var sst = new SharedStringTable ();
            foreach (var text in _os)
                sst.AppendChild (new SharedStringItem (new Text (text)));
    
            // add empty workbook and worksheet to the SpreadsheetDocument
            var workbookPart = spreadsheetDocument.AddWorkbookPart ();
            var worksheetPart = workbookPart.AddNewPart<WorksheetPart> ();
    
            var shareStringPart = workbookPart.AddNewPart<SharedStringTablePart> ();
            shareStringPart.SharedStringTable = sst;
    
            shareStringPart.SharedStringTable.Save ();
    
            // add sheet data to Worksheet
            worksheetPart.Worksheet = new Worksheet (sheetData);
            worksheetPart.Worksheet.Save ();
    
            // fill workbook with the Worksheet
            spreadsheetDocument.WorkbookPart.Workbook = new Workbook (
                    new FileVersion { ApplicationName = "Microsoft Office Excel" },
                    new Sheets (
                        new Sheet {
                            Name = _sheetName,
                            SheetId = (UInt32Value)1U,
    
                            // generate the id for sheet
                            Id = workbookPart.GetIdOfPart (worksheetPart)
                        }
                    )
                );
            spreadsheetDocument.WorkbookPart.Workbook.Save ();
            spreadsheetDocument.Close ();
        }
    
        public void CreateXlsxAndFillData(Stream stream) {
            // Create workbook document
            using (var spreadsheetDocument = SpreadsheetDocument.Create (stream, SpreadsheetDocumentType.Workbook)) {
                FillSpreadsheetDocument (spreadsheetDocument);
            }
        }
    }
    

    The above code create new XLSX file directly. You can extend the code to support more data types as String and Integer which I used in the code.

    In more professional version of your application you can create some XLSX templates for exporting different tables. In the code you can place the data in the cells instead, so modify the spreadsheet instead of creating. In the way you can create perfect formatted XLSX files. The examples from the MSDN (see here) will help you to implement the way when it will be required.

    UPDATED: The answer contains updated code which allows generate Excel documented with more cell formatting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using jqgrid (standard) with EF 4 + MVC3 . I'd like to
I'd like to implement my own delete functionality in jqGrid. I'm currently using the
I'm using jqGrid 4.2 with the filterToolbar , which works great. I'd like to
I am using jqGrid control in an ASP.NET application. Export to Excel feature is
I have a asp.net webforms page in which I'm using the jqGrid component. The
I am having trouble constructing a jqGrid TreeGrid using local data. This method works
I am using JqGrid with javascript. I would set the height of each table
I am using the latest version of jqGrid : 3.6.4 This seems like a
I am using JQGrid in my solution in which i load tabs dynamically, but
I am using jqGrid with ui.multiselect.js for column choosing and reordering. The dialog which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.