CSV文件在Mac计算机上无法正确打开

时间:2018-10-29 11:17:07

标签: java csv jackson

我正在使用jackson-dataformat-csv库生成一个csv文件,但无法在Mac Computers上正确打开它。这是生成csv的Java代码,我不知道我在这里做错了什么:

public void generate(OutputStream outputStream, ArrayList arrlist)
        throws SQLException, IOException, NestableException {
    try {
        int currentRow = 0;
        int i = 0;
        for (int counter = 0; counter < arrlist.size(); counter++) {

            HSSFRow row = sheet.createRow(currentRow);
            System.out.println("Description is" + arrlist.get(counter));

            String c = (String) arrlist.get(counter);

            HSSFCell cell = HSSFCellUtil.createCell(row, i, null);
            cell.setCellValue(c);

            i++;
            workbook.write(outputStream);
            currentRow++;
            currentRow++;

        }
    } catch (IOException e) {

    } finally {
        outputStream.close();
    }
}

模型类:

import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class CsvDownload {
    public static void main(String[] args) throws  IOException {

        User user1 = new User("Cow",25, "xxxxxx@gmail.com","1234567890",
                "Street","Heights","7100",
                "Male","Town");

        User user2 = new User("John",26, "xxxxxxx@gmail.com","+1234567890",
                "Street","Heights","7100",
                "Male","Town");


        List<User> users = new ArrayList<User>();
        users.add(user1);
        users.add(user2);

        // create mapper and schema
        CsvMapper mapper = new CsvMapper();
        CsvSchema schema = mapper.schemaFor(User.class).withHeader();
        schema = schema.withColumnSeparator(',');

        // output writer
        ObjectWriter myObjectWriter = mapper.writer(schema);
        File tempFile = new File("users.csv");
        FileOutputStream tempFileOutputStream = new FileOutputStream(tempFile);
        BufferedOutputStream bufferedOutputStream = new 
        BufferedOutputStream(tempFileOutputStream, 1024);
        OutputStreamWriter writerOutputStream = new 
        OutputStreamWriter(bufferedOutputStream, "UTF-8");
        myObjectWriter.writeValue(writerOutputStream, users);
    }
}

此代码用于生成csv文件,但无法在Mac计算机上正确打开,在Windows上没有问题。 Screen image

0 个答案:

没有答案