Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java创建csv文件,具有;及;在其中,在Excel中正确打开_Java_Excel_Csv - Fatal编程技术网

Java创建csv文件,具有;及;在其中,在Excel中正确打开

Java创建csv文件,具有;及;在其中,在Excel中正确打开,java,excel,csv,Java,Excel,Csv,需要创建包含以下文本的csv文件: <p><span style="color: #000000;"><strong>TEXT: </strong></span>another TEXT</strong></span> 文本:另一个文本 这是我的代码: PrintWriter testFile = new PrintWriter("C:\\JAVA\\test01.csv", "UTF-8"); String

需要创建包含以下文本的csv文件:

<p><span style="color: #000000;"><strong>TEXT: </strong></span>another TEXT</strong></span>
文本:另一个文本
这是我的代码:

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();
PrintWriter testFile=新的PrintWriter(“C:\\JAVA\\test01.csv”、“UTF-8”);
字符串测试=“Dostupnosť:do 2-14 dni”;
print(“零;一;”+test);
testFile.close();
它创建了文件,但由于某种原因,我无法在Excel中打开它,当我打开它时,它只给我一个空白文件,其中没有文本。而主要的问题是,“;”在正文的中间。它用作单元格分隔符,将文本分隔在另一个单元格中,但我需要它保留在一个单元格中

当我直接在excel中创建文件时,效果很好。它甚至会忽略“;”并将文本保留在一个单元格中。
到目前为止,答案是,这可能是因为编码错误,应该是UTF-8,但这应该没问题。

您的内容不允许包含保留字符,例如分号。 为了解决这个问题,你可以使用双引号


总结如何以及何时在.csv中进行分隔和双引号

如果您创建了一个用于excel的csv,为什么不考虑使用类似的方法?如果你花了太多的时间来寻找一个写CSV的方法,这个库可以帮你解决这个问题。它很容易实现。下面是一个代码示例:

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();
    }
}

这是我的。这本书展示了如何在单元格上添加边框,但也展示了创建工作簿并在其中添加内容是多么容易。你不必担心将来可能会出现的离奇怪癖,如逃过分号或任何其他怪癖。

好了,我现在知道如何添加语音标记和逗号,所以Excel接受它们,但不要接受分号。你有没有找到解决问题的方法?嗯,没有,但我发现了,代码设计不需要分号,因此我没有问题。:)但我仍然不知道我的问题的答案——这已经不重要了。