简体   繁体   中英

The fomat and extension of “file.csv” doesn't match

I am trying to create the csv file with data as follows

        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Sheet1");
        String fileLocation = null;
        try { 
            // Set the header columns name for worksheet
            createHeader(workbook, sheet);
            // populate the data
            createWorkbookRows(ker, sheet, kerList, AerList);

            String tempDir = System.getProperty("java.io.tmpdir");
            File file = new File(tempDir);
            fileLocation = file.getAbsolutePath() + FileSystems.getDefault().getSeparator() + "MyData.csv";
            FileOutputStream outputStream = new FileOutputStream(fileLocation);
            workbook.write(outputStream);
            }catch(Exception e){
            }

With the above code I am able to generate the csv file, but when I try to open the file the below error it is showing

在此处输入图片说明

When I click on yes it is showing the data properly in excel file.

I have verified the csv file by opening in Notepad++ also it is showing in non understandable language instead of comma seperated.

Please suggest how can I solve the above issue.

POI writes a binary Excel file. Your extension "csv" is wrong. It should be "xlsx" because you use XSSFWorkbook.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM