NutzCN Logo
问答 用nutz和poi导出Excel表格中文乱码怎么解决
发布于 2168天前 作者 qq_e8bc609b 1552 次浏览 复制 上一个帖子 下一个帖子
标签:
@At
	@Ok("raw")
	public File down() throws FileNotFoundException, IOException {
		HSSFWorkbook wk=new HSSFWorkbook();
		HSSFSheet st=wk.createSheet("用户基本信息");
		List<ExcelBean> list = dao.query(ExcelBean.class,null);
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
			HSSFRow row = st.createRow(i);
			HSSFCell c1 = row.createCell((short) 0);
			HSSFCell c2 = row.createCell((short) 1);
			HSSFCell c3 = row.createCell((short) 2);
			HSSFCell c4 = row.createCell((short) 3);
			c1.setCellValue(list.get(i).getId());
			c2.setCellValue(list.get(i).getName());
			c3.setCellValue(list.get(i).getSex());
			c4.setCellValue(list.get(i).getAge());
		}
		File file=new File("d:temp/a.xls");
		if(!file.exists()) {
			System.out.println("文件不存在,創建文件中");
			file.createNewFile();
		}
		FileOutputStream io=new FileOutputStream(file);
		wk.write(io);
		io.close();
		return file;
	}
ExcelBean [id=27, name=***, sex=男, age=24]
ExcelBean [id=28, name=***, sex=男, age=23]
ExcelBean [id=29, name=**, sex=女, age=23]
ExcelBean [id=30, name=***, sex=女, age=23]
ExcelBean [id=31, name=***, sex=男, age=24]
ExcelBean [id=32, name=***, sex=男, age=28]
ExcelBean [id=33, name=***, sex=女, age=24]

2 回复

使用环境的编码设置成UTF8了吗?

搞定了,把单元格里面的编码设置一下就好了,但不是很清楚原理,总觉得之后可能还是会出现乱码。

c2.setEncoding(HSSFCell.ENCODING_UTF_16);
c3.setEncoding(HSSFCell.ENCODING_UTF_16);
添加回复
请先登陆
回到顶部