1

My below code is working well except for one slight issue. When I open the excel sheet, the column headers are pulling directly from the code below I.E.data.FleetNumber displays as FleetNumber for the column header. Is it possible to overwrite this? E.G I want it to display Fleet No.

I've done a lot of searching but I cant find an answer. Any help is appreciated.

    public void ExportToBeDoneVehiclesToExcel()
    {
        int count = 10;

        var grid = new System.Web.UI.WebControls.GridView();

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);

        var list = vehicleBusinessLogic.GetToBeDoneVehicles(count);
        grid.DataSource = from data in list
                            select new
                            {
                                data.RegistrationID,
                                data.FleetNumber,
                                data.VIN,
                                data.LocationDescription,
                                data.ServiceDescription,
                                data.NextInspectionDueDate
                            };
        grid.DataBind();

        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=Exported_ToBeDoneVehicles.xls");
        Response.ContentType = "application/excel";

        grid.RenderControl(htw);

        Response.Write(sw.ToString());
        Response.End();
    }
1
  • What type is list Commented Mar 28, 2017 at 15:33

1 Answer 1

2

i can't test this but it seems logical to me

try specifying the variables names in the list

grid.DataSource = from data in list
                        select new
                        {
                            Reg = data.RegistrationID,
                            FleetNumber = data.FleetNumber,
                            VIN = data.VIN,
                            LocationDescription = data.LocationDescription,
                            ServiceDescription = data.ServiceDescription,
                            NextInspectionDueDate = data.NextInspectionDueDate
                        };
Sign up to request clarification or add additional context in comments.

2 Comments

Yes that does work for displaying as Reg but it's not exactly what I'm looking for. Your suggestion does not work if I want to have spaces E.G Fleet No. Sorry I should have given FleetNumber as the example originally. I have edited my question. Thank you for your help!
Check comments is this what you looking for .. if yes then check this too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.