文件预览乱码.zip (49.2 KB)
@Yuxuan
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): CELLSJAVA-45172
You can obtain Paid Support services if you need support on a priority basis, along with the direct access to our Paid Support management team.
@Yuxuan
因为文件中没有编码格式,所以你需要在打开这个文件的时候指定编码格式。 目前GridWeb没有指定的接口,我们已经记录在任务单中。
现在的话,你可以先用Aspose.Cells for Java 转换一下这个文件像下面的代码:
TxtLoadOptions loadOptions = new TxtLoadOptions();
loadOptions.setEncoding (Encoding.getEncoding(“GB2312”));
Workbook wb = new Workbook(dir + “中文显示乱码.csv”, loadOptions);
wb.save(dir+“dest.xlsx”);
然后交给Gridweb处理。
在项目上手动修改编码格式为GB2312,打开文件仍然乱码乱码.png (56.4 KB)
你说的 在项目上手动修改编码格式为GB2312 是什么意思,
具体是怎么操作的呢?
我们上面已经回答你了,现在直接用Gridweb还没有支持导入文件指定encoding 你必须先把文件用cells api转为xlsx才行。
这个问题是发生在解析文件的环节
对于csv,tsv等文本文件,如果您没有在loadoptions里面指定编码,我们固定的使用UTF8来读取。所以目前您需要对文本文件进行预处理,然后再用GridWeb加载。预处理的方式可以是前面我们所说的利用Aspose.Cells保存为xlsx,也可以直接使用java.io进行编码的转换。请参考如下示例代码:
java.io.BufferedReader br = new java.io.BufferedReader(new java.io.FileReader("中文显示乱码.csv"));
java.io.PrintWriter bw = new java.io.PrintWriter("utf8.csv", "utf8");
String line = br.readLine();
while(line != null)
{
bw.println(line);
line = br.readLine();
}
br.close();
bw.close();
转换编码后的utf8.csv文件应该就可以正确地被gridweb加载了
感谢,转换编码后,csv文件可以被正确地加载了。
GridWeb会新增编码转换接口吗?
CSV 是没有指定编码格式的文本文件。 因此,您处理文件的编码。 而且,GridWeb 也没有指定的接口来处理它。所以目前您需要在将文本文件加载到 GridWeb 之前对其进行预处理。
它将在下一个版本(23.3)中准备就绪,因此您将尝试以下代码,然后可以相应地加载它:
public void chinesegbcsv(GridWebBean gridweb,HttpServletRequest request, HttpServletResponse response) {
try {
//Charset cs;
GridTxtLoadOptions topt=new GridTxtLoadOptions();
topt.setEncoding(Encoding.getEncoding(“GB2312”));
gridweb.setLoadOptions(topt);
super.reloadfile(gridweb,request,“中文显示乱码.csv”);
gridweb.setEnableAJAX(true);
gridweb.setOnAjaxCallFinishedClientFunction(“TestAjaxCallFinish”);
gridweb.setOnCellUpdatedClientFunction(“CellUpdate”);
} catch (Exception e) {
e.printStackTrace();
}
}
The issues you have found earlier (filed as CELLSJAVA-45172) have been fixed in Aspose.Cells for Java 23.3.