1。用poi的HSSF组件将汉字写入EXCEL中出现乱码的问题我已经解决了。
写了一个方法,调用一下就可以了。方法的源代码如下:
public static String getUnicode(String toEncoded,String encoding)
{
String retString="";
if(toEncoded.equals("")||toEncoded.trim().equals(""))
{
return toEncoded;
}
try
{
byte[] b=toEncoded.getBytes(encoding);
sun.io.ByteToCharConverter convertor=sun.io.ByteToCharConverter.getConverter(encoding);
char [] c=convertor.convertAll(b);
for(int i=0;i<c.length;i++)
{
retString+=String.valueOf(c[i]);
}
}catch(java.io.UnsupportedEncodingException usee)
{
System.out.println("不支持"+encoding+"编码方式");
usee.printStackTrace();
}catch(sun.io.MalformedInputException mfie)
{
System.out.println("输入参数无效!!!");
mfie.printStackTrace();
}
return retString;
}
使用方法为:
cell=row.createCell((short)2);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
String temp="中央人民广播天台3台";
temp=getUnicode(temp,"gb2312");
cell.setCellvalue(temp);
写了一个方法,调用一下就可以了。方法的源代码如下:
public static String getUnicode(String toEncoded,String encoding)
{
String retString="";
if(toEncoded.equals("")||toEncoded.trim().equals(""))
{
return toEncoded;
}
try
{
byte[] b=toEncoded.getBytes(encoding);
sun.io.ByteToCharConverter convertor=sun.io.ByteToCharConverter.getConverter(encoding);
char [] c=convertor.convertAll(b);
for(int i=0;i<c.length;i++)
{
retString+=String.valueOf(c[i]);
}
}catch(java.io.UnsupportedEncodingException usee)
{
System.out.println("不支持"+encoding+"编码方式");
usee.printStackTrace();
}catch(sun.io.MalformedInputException mfie)
{
System.out.println("输入参数无效!!!");
mfie.printStackTrace();
}
return retString;
}
使用方法为:
cell=row.createCell((short)2);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
String temp="中央人民广播天台3台";
temp=getUnicode(temp,"gb2312");
cell.setCellvalue(temp);