java csv 0开头问题

最近正在做项目时遇到一个问题,就是将网页中的表格,导出csv文件,用的java Web技术,在将纯数字导出CSV文件,开头的0消失,0123导出后为123。这并不是想要的结果。发现,在写入的地方加上tab,就可以成功导入了。源码如下:

public File putOutTaskToExcelFile(List<User> getPutOutTaskResult) {
// 在我的代码里,getPutOutTaskResult是我需要导出的信息列表,你换成你的就行
BufferedWriter out = null;
int random = (int) (Math.random() * 1000 + 1);
// 这个随机数只是为了让后面生成的文件名不重复而已
File excelFile = null;
try {
excelFile = File.createTempFile("CSV" + random, ".csv");
// 生成一个csv临时文件
excelFile.deleteOnExit();
} catch (IOException e1) {
e1.printStackTrace();
}
int i = 1;
try {
out = new BufferedWriter(new FileWriter(excelFile));
out.write("email" + "," + "Other_name" + "," + "true_name" + ","
+ "gender" + "," + "password" + "," + "org_unit_id" + ","
+ "cos_id" + "," + "cos_name" + "," + "quota_delta" + ","
+ "user_status" + "," + "user_expiry_date" + ","
+ "mobile_number" + "," + "address" + "," + "zipcode" + ","
+ "nf_quota_delta" + "," + "privacy_level" + ","
+ "user_list_rank");// 换成你需要的表头
out.newLine();
Iterator<User> resultIterator = getPutOutTaskResult.iterator();
while (resultIterator.hasNext()) {
User user = resultIterator.next();

//一个小小的\t解决大问题
out.write(user.getUser_id() + "@ujs.edu.cn" + ",\t"
+ user.getOther_name() + ",\t" + user.getUD_true_name()
+ "," + "" + ",\t" + user.getUD_password().toString()
+ ",,,,,,,,,,,");

// A、B、C等等都换上你自己的就可以 i是一个自增序号
out.newLine();
i++;
}
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return excelFile;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值