Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by Seetharaman Srinivasan on Feb 20, 2009 09:34
open dhtmlx forum
Export to Excel using erializeToCSV

When I try to export the grid to excel, one of the filed which contains customer numbers like 0000237 is showing up as 239 in Excel.
Is there anything I should do in formatting the grid so that the export using         document.formstyle.csvBuffer.value=mygrid1.serializeToCSV();
will work?
Answer posted by Support on Feb 23, 2009 08:07
Grid putting data in CSV as is, but excel may count this value as number while parsing incoming data and show it accordingly. 
Unfortunately I don't see any good solution, the column type is excel specific property, which can't be set through CSV
Answer posted by Seetharaman Srinivasan on Feb 23, 2009 09:26
This is a must requirement for me. I solved this issue using this work around. This solution may be useful to some users who are looking for similar solution.
I always set the coltype of the columns which are to be right alined as 'ron' and align as right and sorttype as int. with this settings, here is the function I use to export any grid..
function gridExport(mygrid)
{
    var html="";

    var numRows=mygrid.getRowsNum();
    var numCols=mygrid.getColumnsNum();
    html=html+"<table border=1><tr>";
    for(i=0;i<numCols;i++)html=html+"<td align=left>"+mygrid.getColumnLabel(i)+"</td>";
    html=html+"</tr>";

    for(i=1;i<=numRows;i++)
        {
            html=html+"<tr>";
            for(j=0;j<numCols;j++)
            {
                if(mygrid.getColType(j).strip()=="ron"    )
                        html=html+"<td align=right>";
                else
                        html=html+"<td align=left>&nbsp;";
                var txt="";
                if(mygrid.cells(i,j).getValue()!=null)txt=mygrid.cells(i,j).getValue().toString();
                html=html+txt+"</td>";
            }
            html=html+"</tr>";
        }
    html=html+"</table>";
    document.formstyle.csvBuffer.value=html.strip();
    document.formstyle.method='POST';
    document.formstyle.action='/ccb/jsp/csvExport.jsp';
    document.formstyle.target='_blank';
    document.formstyle.submit();
    return;
}

The csvBuffer  variable contavis  an html table. This is sent to a jsp (it could be anything like php or whatever). The jsp simple dumps to the response object.

Following is my jsop to open the excel file show the exported data. The jsp can be rewritten in php or any other language using the same principle.
<%
response.setContentType("application/vnd.ms-excel");
String buf=request.getParameter("csvBuffer");
try{response.getWriter().println(buf);}catch(Exception e){}
%>

Answer posted by Venu S on Mar 22, 2009 17:29
Neat !!