java如何向csv文件中写数字格式的文本,
package java_csv;
import java.io.FileWriter;
import java.util.HashMap;
import org.supercsv.cellprocessor.ParseBigDecimal;
import org.supercsv.cellprocessor.constraint.StrMinMax;
import org.supercsv.cellprocessor.constraint.Unique;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvMapWriter;
import org.supercsv.io.ICsvMapWriter;
import org.supercsv.prefs.CsvPreference;
public class CSVWriteExample {
public static void main(String[] args) throws Exception{
final CellProcessor[] processors = new CellProcessor[] {
new Unique(new StrMinMax(1, 20)),
new ParseBigDecimal()
};
ICsvMapWriter writer = new CsvMapWriter(new FileWriter("e:/person.csv"), CsvPreference.EXCEL_PREFERENCE);
try {
final String[] header = new String[] { "name","total" };
// set up some data to write
final HashMap<String, Object> data1 = new HashMap<String, Object>();
data1.put(header[0], "Raj");
data1.put(header[1], "1301699234566666");
// the actual writing
writer.writeHeader(header);
writer.write(data1, header);
} finally {
writer.close();
}
}
}
这样导出的csv文件用excel打开会显示:
name total
Raj 1.3017E+15
而且这样显示的话,也会把最后一位的数字丢失,变成了0;失去了数据的完整性了。
但是我这不想这样的结果,我想要的结果是:
name total
Raj 1301699234566666
直接显示原来的数字文本形式。
请问我该怎么解决呢,
我现在在程序中使用的CsvMapWriter向外输出,用CellProcessor怎么实现呢。