0

I'm sending an array of Objects back to the server(WebApi), using EEPlus to do a ExcelPackage.GetAsByteArray(); Then sending that back to an AngularJS frontend.

I'm storing the Information in a DataTable to begin with.

DataTable table = new DataTable();
        table.Columns.Add("WOS Name", typeof(string));
        table.Columns.Add("Consultant Name", typeof(string));
        table.Columns.Add("Client", typeof(string));
        table.Columns.Add("Cohort", typeof(string));

        table.Columns.Add("Not Started", typeof(int));
        table.Columns.Add("Awaiting Wos Release", typeof(int));
        table.Columns.Add("Released To Consultant", typeof(int));
        table.Columns.Add("Released To Wos", typeof(int));
        table.Columns.Add("Awaiting Wos Signoff", typeof(int));
        table.Columns.Add("Current", typeof(int));

        table.Columns.Add("WOS Overdues", typeof(int));
        table.Columns.Add("Consultant Overdues", typeof(int));

        foreach (var stat in statistics)
        {
            table.Rows.Add(
                stat.WosName,
                stat.ConsultantName,
                stat.Client,
                stat.Cohort,

                stat.NotStarted,
                stat.AwaitingWosRelease,
                stat.ReleasedToConsultant,
                stat.ReleasedToWos,
                stat.AwaitingWosSignoff,
                stat.Current,

                stat.WosOverdues,
                stat.ConsultantOverdues
                );
        }

I'm saving it as a Byte Array.

using (ExcelPackage pck = new ExcelPackage())
        {
            //Create the worksheet
            ExcelWorksheet ws = pck.Workbook.Worksheets.Add("AppraisalStatistic");

            //Load the datatable into the sheet, starting from cell A1.
            ws.Cells["A1"].LoadFromDataTable(table, true);



            //Excel is properly formatted.
            //pck.SaveAs(new System.IO.FileInfo("C:\\Users\\Burton\\Desktop\\a.csv")); 

            return pck.GetAsByteArray();
        }

Then I'm sending it back to Angular.

var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(outputBytes) };

            result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = "AppraisalStatisics.csv"
            };
            return result;

This is what I have for Angular. $scope.statistics is the data I'm sending back to form the DataTable.

    $scope.getExcel = function () {
        AdminAjax.getAppraisalStatExcel($scope.statistics).then(
            function (byteData) {
                console.log(byteData);
                var anchor = angular.element('<a/>');
                anchor.attr({
                    href: 'data:attachment/csv;charset=utf-8,' + encodeURIComponent(byteData),
                    target: '_blank',
                    download: 'AppraisalStatisics.csv'
                })[0].click();
            },
            function (errorResult) {
                console.log(errorResult);
            });
    }

When the Excel is downloaded, It is not proper. This is what I see when I open the excel sheet.

    "PK}p�F�H��3[Content_Types].xml���J1�_e�U��D���U�>���vC��̴�oo6+""��z�$����3_�k����Њ�����ƆM+�Ö�[�C0�b�V��r1_RSf��gNwJ���ɘ0����kÞ¨zT�����10���!�{�`��Y���u+ %g5p�R�L4�""���]�an�    ��Dft��z���4��4$<������""b�Y�&��/#�RF0�#�w�V���1�2?�/����G�����j""����*��evA��C:G1*���!�y�\��<�φoU�~�    PK�H��3PK}p�F��닮'_rels/.rels����0�WYz��c��1�j��V�e�"      
o��b<xl���Ӳ^��=ч���"Ë�U�k���� Di��Ȣ��Uy�I�t����1��A�8Ë�C�6�Y�4zÃT�4��y~��Ӏ��-�7�֮�����gR�m�Q��H����e�/���h�    
�*����        
PK��닮'PK}p�FU|��Wxl/workbook.xml���j�0D�����DI99P�K �@�����E$��UH?���C{�mfg�1�A|���wIn�PJn�7`YS�T��8B�����w�N�1��l��1@��d�Y�L�K2#t2 �fT��a����}z|�d�a�;��K�o�N����T���~͙���R�J�N�)?v��jÅ���]���:�ª���V�9��ϱmͲ��PKU|��WPK}p�F�b���4xl/_rels/workbook.xml.rels���j�0�_���8�`�Q��1��@�J�������k6VR(c���d��>���kÔ‰2�1h�]:����+(���D��rKJYa�'V�؀IoZ��4"W1Q(/m�#Jis��#v�u�����L�q��5��S���c���ޣ�)È}��ȞH      
sGb�:b�]��PAߗY<R�e�_^M~�����1��I.��[�ǿ2��ګPK�b���4PK}p�F����hxl/worksheets/sheet1.xml���r�0�_E�{#lH�z�L�͡�C��ab�G���[}��Be�1���&��v��v���'|���БrѲ>“#ڗ�j�m�����_�����h(�H὘�7R�g����]!؞�J��      
��|KX]�%]����^���=Nw�TG���|���\b�iQ     
%t�K��h{�U����G��~���#����'q�F盼3�q�dU���%��稢uq�����v�He���Q�"9;!e��8  "Ï«W���U�1�BrT����MLt��&�:��    _'�6�Db�:���'�Hm��Nd6��k����c���]%�M�^M�^M� �6�0z�@���a���8z�h�q�$���A�Z���@��Q��-�ms�)�;�bbL�Â����K'>�HL!1��5$n 1�#j]�.�9�_8w f���.��W��Bb��t(2�#^"  &7o@��'��$�Ór}3�PK����hPK}p�F���Z
"xl/styles.xml�T�n�0�W�?X|@��C�TB꥗�Ы   6XZ?d;����MD�J�������Һ衧ԡA��U�;��0�Ǟ"       
b���rb�2��)5��P�ڹI.v�W "�����R�D-�EGu���vž`JF������_�C�����w�%��xcxdK�(\�5��]�>�� X�<G'u5q�YO "   
�רi�I%�jFl��DgȘ/w�X����ap�(�R��������2����]���i�(��H���NI�NÉK�:R��|}���rC��h�+�Ӟ��7��0���@[鮫���� "g��S�%��[mv�u��¡��V���<!QlN�������j��/a�PK���ZPK}p�F���6�xl/sharedStrings.xml}R�N1��Mߥȃ1���b|k����=�-�_o�FE;3�3�6���輶���^_RekM�\<-�/�xVT+c   s��^��ȼg���s�0w7R���V����""���U�n-}�PÕ¾A���A�%[�I@eq.�5�~8���.2.�����Lr��=���" �`b2>M���Pc]�N-C��1����Ni�]��z��A������C�s����Q��dW�$}p�ĥ�
�����Z�O����je�-%Ɍ9���ea"-�H0���4&N.�1�V����3L^�ѱ�        
���b��s[��M�����PK���6�PK-}p�F�H��3[Content_Types].xmlPK-}p�F��닮'Y_rels/.relsPK-}p�FU|��W@xl/workbook.xmlPK-}p�F�b���4Hxl/_rels/workbook.xml.relsPK-}p�F����hfxl/worksheets/sheet1.xmlPK-}p�F���Z     
"�xl/styles.xmlPK-}p�F���6�xl/sharedStrings.xmlPK�� "   

Edit: It seems that this might be a response for downloading through a AJAX request. However, I'm using an and href.

Burton
  • 53
  • 6

1 Answers1

1

EPPlus does not generate CSV files - only XLSX. Excel can open the one you generated locally (the commented out line) because it is a very common scenario it specifically looks out for - take any XLSX on your hard drive and rename it to .csv and excel will open it, usually with a warning.

So if the browser is expecting a csv as dictated by your response stream and gets an binary zip file (which is what an XLSX is) it is not going to like that. You will have to adjust your href for the proper context

If all you need is a csv output you dont really need EPPlus plus - just loop through the table. There are many ways to do this with methods posted here on stackoverflow:

Writing a CSV file in .net

Community
  • 1
  • 1
Ernie S
  • 13,902
  • 4
  • 52
  • 79