简体   繁体   中英

Java create csv file, has " and ; in it, open correctly in Excel

need to create csv file with this text in it :

<p><span style="color: #000000;"><strong>TEXT: </strong></span>another TEXT</strong></span>

This is my code :

PrintWriter testFile = new PrintWriter("C:\\JAVA\\test01.csv", "UTF-8");
String test = "<p><span style=\"color: #000000;\"><strong>Dostupnosť: </strong></span>do 2 -14 dní</strong></span>";
testFile.print("zero;one;"+test);
testFile.close();

It creates the file, but I can't for some reason open it in Excel, when I open it, it only gives me blank file, with no text in it. And the main problem is, the ";" in the middle of the text. It works as cell separator and separates the text in another cell, but I need it to remain in one cell.

When I create the file direct in excel, it works fine. It even ignores the ";" and text remains in one cell. Answer so far was, that it might be because of wrong encoding, and it should be UTF-8, but that should be OK.

Your content is not allowed to contain reserved characters such as the semicolon. To cope with this you can use double-quoting.

This answer sums up how and when to delimit and double quote in .csv

If your creating a CSV for use in excel, why not think about using something like Apach POI ? If your spending too much time to find a work around for writing to CSV, this library could solve that for you. It's pretty easy to implement. Here is an example of the code:

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.*;

import java.io.FileOutputStream;

/**
 * Working with borders
 */
public class WorkingWithBorders {
    public static void main(String[] args) throws Exception {
        Workbook wb = new XSSFWorkbook();  //or new HSSFWorkbook();
        Sheet sheet = wb.createSheet("borders");

        // Create a row and put some cells in it. Rows are 0 based.
        Row row = sheet.createRow((short) 1);

        // Create a cell and put a value in it.
        Cell cell = row.createCell((short) 1);
        cell.setCellValue(4);

        // Style the cell with borders all around.
        CellStyle style = wb.createCellStyle();
        style.setBorderBottom(CellStyle.BORDER_THIN);
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderLeft(CellStyle.BORDER_THIN);
        style.setLeftBorderColor(IndexedColors.GREEN.getIndex());
        style.setBorderRight(CellStyle.BORDER_THIN);
        style.setRightBorderColor(IndexedColors.BLUE.getIndex());
        style.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED);
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
        cell.setCellStyle(style);

        // Write the output to a file
        FileOutputStream fileOut = new FileOutputStream("xssf-borders.xlsx");
        wb.write(fileOut);
        fileOut.close();
    }
}

This is from Apache's example page . This one shows how to put borders on cells, but it also showcases how easy it is to create a workbook and put stuff in it. And you shouldn't have to worry about weird quirks like escaping a semicolon or any other oddities that may pop up in the future.

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