2

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.

Yograj Gupta
  • 9,811
  • 3
  • 29
  • 48
nilrem
  • 21
  • 5
  • Did you ever find a solution to your problem? – LumberSzquatch Jul 20 '16 at 18:41
  • Well, not really, but I found out, that the code desn't need the semicolon to be working, so I don't have the problem. :) But I still don't know the answer to my question - it just don't matter anymore. – nilrem Jul 21 '16 at 19:40

2 Answers2

0

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

Community
  • 1
  • 1
Abaddon666
  • 1,533
  • 15
  • 31
  • OK, I now know how to add speech marks and commas, so Excel accept them, but not the semicolon. – nilrem Jul 18 '16 at 16:24
0

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.

LumberSzquatch
  • 1,054
  • 3
  • 23
  • 46