如何用阿拉伯语创建csv文件?

时间:2012-11-08 08:06:08

标签: java csv

我正在尝试用阿拉伯语创建CSV文件列,我正在使用资源包从属性文件中获取值,并且它能够在阿拉伯语中获取值,但在创建文件后,列名称显示为 “??????????????”而不是阿拉伯语。请帮我解决这个问题。

我正在使用以下代码:

public static void downloadCSVForReconcilationData(String downloadStr,HttpServletResponse response,String reportName) throws IOException{

    //response.setHeader("Content-Disposition", "inline;filename=\""+"BillingTrxReport.csv"+"\"");
    response.setContentType ("application/x-csv");
    response.setHeader("Content-Disposition","attachment; filename=\""+reportName+".csv"+"\";");
    //response.setHeader("application/vnd.ms-excel;charset=Cp1256","attachment; filename=\""+reportName+".csv"+"\";");// khushwant Patidar
    response.setContentType("application/vnd.ms-excel;charset=Cp1256");// khushwant Patidar
    response.setContentLength((int) downloadStr.length());

try{
    OutputStream out = response.getOutputStream();
    PrintWriter pw = new PrintWriter(out);
    BufferedWriter bufw=new BufferedWriter(pw);
    //BufferedOutputStream outBuf = new BufferedOutputStream(out);

    bufw.write(downloadStr);
    bufw.close();
    pw.close();
    out.flush();
    out.close();

    }catch(IOException ioe){
        ioe.printStackTrace();
        throw ioe;
    }
}

1 个答案:

答案 0 :(得分:2)

如果您在内容类型中使用CP1256,则在使用java.io.PrintStream创建文件时必须使用相同的内容。

我建议:

OutputStream out = response.getOutputStream();
PrintStream  ps  = new PrintStream( out, true, "CP1256" );
ps.println( downloadStr );
ps.close();

It's not necessary to flush and close输出变量。