有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Excel2007中打开CSV文件时,任何列中前导零的java将丢失

我正在寻找一种在Java中导出到CSV文件时保留整数前导零的方法。以下是将前导零生成CSV的示例

public class TestLargeDataset {

    int one_million = 1000000;

    public void writeMillionRowsToCSVFile(String fqname) throws Exception {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(fqname)));

        byte[] newLine = "\n".getBytes();

        for(int x = 0; x < one_million; x++) {
            bos.write(("0" + x + ",").getBytes());
            bos.write(("0" + (x+1) + ",").getBytes());
            bos.write(("0" + (x+2) + ",").getBytes());
            bos.write(("0" + (x+3)).getBytes());
            bos.write(newLine);
        }
        bos.close();
    }

    /*
     * Main.
     */
    public static void main(String a[]) throws Exception {
        TestLargeDataset tlr = new TestLargeDataset();      

        long startTime2 = System.nanoTime();
        tlr.writeMillionRowsToCSVFile("C:\\output_" + System.currentTimeMillis() + ".csv");
        long diff2 = (System.nanoTime() - startTime2)/1000000000;
        tlr.log("Executed file write operation in " + diff2 + " seconds (~" + (diff2/60) + " minutes)");

        tlr.getStatement().close();
        tlr.getConnection().close();

        tlr.log("Execution complete.");
    }

}

在Excel2007中打开输出文件时,前导零丢失。有没有一种方法可以保留这些零并在Excel中打开相同的零,而无需将其转换为字符串?不将它们转换为字符串的原因是,我需要在excel中对这些值应用整数算术

想法


共 (1) 个答案

  1. # 1 楼答案

    在另一个论坛上找到了这个解决方案,它对我很有效

    附加双引号和制表符,问题将得到纠正。制表符防止当数字中有逗号(,)时,前导Zeor截断和双引号分隔单元格

    String yourString=“002015”

    解决方案:

    “\”\t“+您的字符串+“\”