This is a migrated thread and some comments may be shown as answers.

Issue when exporting to excel a grid with a detail grif

5 Answers 202 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Victor
Top achievements
Rank 1
Victor asked on 26 Jan 2016, 08:36 PM

I have a Grid with a detail template to show another records related to the main data set. I'm trying to export everyting to excel but when i export it excel says "Excel found unreadable content" if i make the function to return an empty list to the export function the excel file has no problem it shows nothing about details but columns names. but if i return the dataset that i need the message of unreadable content is shown.

i've followed the example http://docs.telerik.com/kendo-ui/aspnet-mvc/helpers/grid/how-to/excel/detail-grid-export  and http://docs.telerik.com/kendo-ui/controls/data-management/grid/how-to/excel/detail-grid-export so the functions are practically the same

 

var detailExportPromises = [];

        var dataSource = new kendo.data.DataSource({
            type: "aspnetmvc-ajax",
            transport: {
                read: "@Url.Action("Fracciones_Read", "Home")",
                type: "POST"
            },
            schema: {
                data: "Data",
                total: "Total",
                error: "Errors"
            }
        });

        dataSource.read();

        function dataBound() {
            detailExportPromises = [];
            this.expandRow(this.tbody.find("tr.k-master-row").first());
        }
        function parameters() {
            return {
                fechaIni: $("#fechaIni").data("kendoDatePicker").value(),
                fechaFin: $("#fechaFin").data("kendoDatePicker").value(),
                operacion: "Imp"
            }
        }

        function fracciones_excelExport(e) {
            e.preventDefault();
        }

        function excelExport(e) {
            e.preventDefault();

            var workbook = e.workbook;

            detailExportPromises = [];

            var masterData = e.data;

            for (var rowIndex = 0; rowIndex < masterData.length; rowIndex++) {
                exportChildData(masterData[rowIndex].NumeroPedimento, rowIndex);
            }

            // wait for all detail grids to finish exporting
            $.when.apply(null, detailExportPromises)
            .then(function () {
                // get the export results
                var detailExports = $.makeArray(arguments);

                // sort by masterRowIndex
                detailExports.sort(function (a, b) {
                    return a.masterRowIndex - b.masterRowIndex;
                });

                // add an empty column
                workbook.sheets[0].columns.unshift({ width: 30 });

                // prepend an empty cell to each row
                for (var i = 0; i < workbook.sheets[0].rows.length; i++) {
                    workbook.sheets[0].rows[i].cells.unshift({});
                }

                // merge the detail export sheet rows with the master sheet rows
                // loop backwards so the masterRowIndex doesn't need to be updated
                for (var i = detailExports.length - 1; i >= 0; i--) {
                    var masterRowIndex = detailExports[i].masterRowIndex + 1;

                    var sheet = detailExports[i].sheet;

                    // prepend an empty cell to each row
                    for (var ci = 0; ci < sheet.rows.length; ci++) {
                        if (sheet.rows[ci].cells[0].value) {
                            sheet.rows[ci].cells.unshift({});
                        }
                    }

                    // insert the detail sheet rows after the master row
                    [].splice.apply(workbook.sheets[0].rows, [masterRowIndex + 1, 0].concat(sheet.rows));
                }

                // save the workbook
                kendo.saveAs({
                    dataURI: new kendo.ooxml.Workbook(workbook).toDataURL(),
                    fileName: "Importaciones.xlsx"
                });
            });
        }

        function exportChildData(NumeroPedimento, rowIndex) {
            var deferred = $.Deferred();

            detailExportPromises.push(deferred);

            var rows = [{
                cells: [
                    { value: "DescripcionFactura" },
                    { value: "DescripcionPedimento" },
                    { value: "Arancel" }
                ]
            }];

            dataSource.filter({ field: "NumeroPedimento", operator: "eq", value: NumeroPedimento });

            var exporter = new kendo.ExcelExporter({
                columns: [
                    { field: "DescripcionFactura" },
                    { field: "DescripcionPedimento" },
                    { field: "Arancel" }
                ],
                dataSource: dataSource
            });

            exporter.workbook().then(function (book, data) {
                deferred.resolve({
                    masterRowIndex: rowIndex,
                    sheet: book.sheets[0]
                });
            });
        }
    </script>

 

5 Answers, 1 is accepted

Sort by
0
Kostadin
Telerik team
answered on 28 Jan 2016, 11:40 AM
Hello Victor,

Thank you for contacting us.

The presented error actually is caused by a security feature of Windows. 

See the thread below for more information on this matter and possible solutions:
http://superuser.com/questions/401714/how-do-i-resolve-the-error-excel-found-unreadable-content-in-filename


I hope this information helps.

Regards,
Kostadin
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Victor
Top achievements
Rank 1
answered on 28 Jan 2016, 03:41 PM

I've tried all those all ready. i think the issue is on your side cause it doesn't happen always, the fact is that the issue exist only when exporting some text fields with a '/', '//', '\' or '\\' in any other case there is no problem.

those caracters are allowed in excel except in the name of the sheet or file.

0
Kostadin
Telerik team
answered on 01 Feb 2016, 08:42 AM
Hi Victor,

I am afraid I am still not able to replicate the issue. Please check out the attached sample where I used some of the characters that you mentioned are causing the issue but on my end seems to work correctly. Could you please let me know how it differs from your real setup? I would appreciate if you can replicate the issue either in my sample or in a small runnable one in order to investigate it further.

I am looking forward to your reply.

Regards,
Kostadin
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Victor
Top achievements
Rank 1
answered on 02 Feb 2016, 02:38 PM

After cleaning the test fields with this fuction everyting works fine.

 private static string CleanInput(string strIn)
        {
            // Replace invalid characters with empty strings.
            try
            {
                return Regex.Replace(strIn, @"[^\w\.@-]", "",
                                     RegexOptions.None, TimeSpan.FromSeconds(1.5));
            }
            // If we timeout when replacing invalid characters,
            // we should return Empty.
            catch (RegexMatchTimeoutException)
            {
                return String.Empty;
            }
        }

 

So, there must be something wrong when exporting to excel with especial characters.

0
Kostadin
Telerik team
answered on 04 Feb 2016, 08:50 AM
Hello Victor,

Could you please provide a small runnable sample where the issue can be observed in order to investigate it further? I tried a couple of times but without success. You can use a dummy database to populate the grid control.

Regards,
Kostadin
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Victor
Top achievements
Rank 1
Answers by
Kostadin
Telerik team
Victor
Top achievements
Rank 1
Share this question
or