i wrote a code to create a csv file in java and it works fine for english characters but not for chinese characters. How can i Solve this. i tried modifying it but then it does not show comma separated values when opened in text file. please reply.
package com.hp.col.smartquote.web.action;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.hp.col.smartquote.dao.DistributorResellerMapDAO;
import com.hp.col.smartquote.model.DistributorResellerMap;
import com.hp.col.smartquote.model.User;
import com.hp.col.smartquote.service.translation.TranslationManager;
import com.hp.col.smartquote.util.IOUtils;
import com.hp.col.smartquote.util.LocalizationManager;
import com.hp.col.smartquote.web.util.NullForward;
import com.hp.col.smartquote.web.util.WebUtils;
public class ViewDistributorResellerMapping extends BaseAction{
/*private static final String DISTRESS_CSV = "DISTRESS_CSV";
private static final String CSV_LIST_SEPARATOR = ", ";*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
User user = LocalizationManager.getUser();
ServletOutputStream fileOut = response.getOutputStream();
List distResellerMapLst = new ArrayList();
distResellerMapLst = DistributorResellerMapDAO.getInstance().getValueFromDistReseMap(user);
StringBuffer sb = exportResult(distResellerMapLst,request);
String encoding = "UTF-16LE";
WebUtils.prepareStreamedResponse(response, "application/x-download;charset="+ encoding , "quoteList.csv", sb.length(), true);
//SQ LTP 1A changes done by Aswani for distributor reseller mapping excel
byte[] bytes = sb.toString().getBytes(encoding);
bytes[0] = (byte)255;
bytes[1] = (byte)254;
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
IOUtils.copyStream(bis, response.getOutputStream());
//fileOut.write(sb.toString().getBytes());
//fileOut.flush();
return NullForward.getInstance();
}
protected StringBuffer exportResult(List distResellerMapLst,HttpServletRequest req)
{
for (Iterator iter = distResellerMapLst.iterator(); iter.hasNext();)
{
DistributorResellerMap distResellerMap = (DistributorResellerMap) iter.next();
}
int size = distResellerMapLst.size();
StringBuffer sb = new StringBuffer(Math.max(1, size) * 100);
Locale loc = LocalizationManager.getLocale();
//StringBuffer sb = new StringBuffer();
sb.append(" ");
sb.append(TranslationManager.getService().translateKey("common.countryCode", loc));
sb.append("\t");
sb.append(TranslationManager.getService().translateKey("quote.resellerPartnerProId", loc));
sb.append("\t");
sb.append(TranslationManager.getService().translateKey("quote.distributorPartnerProId", loc));
sb.append("\t");
sb.append(TranslationManager.getService().translateKey("quote.activeFlag", loc));
sb.append("\t");
sb.append("\n");
DistributorResellerMap distResellerMap ;
for (Iterator iter = distResellerMapLst.iterator(); iter.hasNext();)
{
distResellerMap = (DistributorResellerMap) iter.next();
sb.append(distResellerMap.getCountryCode());
sb.append("\t");
sb.append(distResellerMap.getResellerPartnerProId());
sb.append("\t");
sb.append(distResellerMap.getDistributorPartnerProId());
sb.append("\t");
sb.append(distResellerMap.getActiveFlag());
sb.append("\n");
}
return sb;
}
}