Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.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
Spring 从csv转换xls/xlsx时输出错误_Spring_Apache Poi_Xssf_Poi Hssf - Fatal编程技术网

Spring 从csv转换xls/xlsx时输出错误

Spring 从csv转换xls/xlsx时输出错误,spring,apache-poi,xssf,poi-hssf,Spring,Apache Poi,Xssf,Poi Hssf,所以我把csv改成了xls/xlsx,但每个单元格有一个字符。我在csv中使用了管道(|)作为分隔符。 以下是csv中的一行: 4.0|sdfa@sdf.nb|plplp | plplplp | plplplp | 1988-11-11 | M|asdasd@sdf.ghgh|sdfsadfasdfasdfasdfasdf | asdfasdf | 3.4253242E7 | 234234.0 |正确|正确| 但在excel中,我得到了 4.0 | s d f a 代码如下: try {

所以我把csv改成了xls/xlsx,但每个单元格有一个字符。我在csv中使用了管道(|)作为分隔符。 以下是csv中的一行: 4.0|sdfa@sdf.nb|plplp | plplplp | plplplp | 1988-11-11 | M|asdasd@sdf.ghgh|sdfsadfasdfasdfasdfasdf | asdfasdf | 3.4253242E7 | 234234.0 |正确|正确|

但在excel中,我得到了 4.0 | s d f a

代码如下:

    try {
        String csvFileAddress = "manage_user_info.csv"; //csv file address
        String xlsxFileAddress = "manage_user_info.xls"; //xls file address
        HSSFWorkbook workBook = new HSSFWorkbook();
        HSSFSheet sheet = workBook.createSheet("sheet1");
        String currentLine=null;
        int RowNum=0;
        BufferedReader br = new BufferedReader(new FileReader(csvFileAddress));
        while ((currentLine = br.readLine()) != null) {
            String str[] = currentLine.split("|");
            RowNum++;
            HSSFRow currentRow=sheet.createRow(RowNum);
            for(int i=0;i<str.length;i++){
                currentRow.createCell(i).setCellValue(str[i]);
            }
        }

        FileOutputStream fileOutputStream =  new FileOutputStream(xlsxFileAddress);
        workBook.write(fileOutputStream);
        fileOutputStream.close();
        System.out.println("Done");
    } catch (Exception ex) {
        System.out.println(ex.getMessage()+"Exception in try");
    }
试试看{
字符串csvFileAddress=“manage\u user\u info.csv”;//csv文件地址
String xlsxFileAddress=“manage_user_info.xls”//xls文件地址
HSSFWorkbook=新的HSSFWorkbook();
HSSFSheet sheet=workBook.createSheet(“sheet1”);
字符串currentLine=null;
int RowNum=0;
BufferedReader br=新的BufferedReader(新文件读取器(csvFileAddress));
而((currentLine=br.readLine())!=null){
字符串str[]=currentLine.split(“|”);
RowNum++;
HSSFRow currentRow=sheet.createRow(RowNum);

对于(int i=0;i管道符号必须在正则表达式中转义:

String str[]=currentLine.split(“\\\\”);
它是一个逻辑运算符(引自):

X | Y或者X或者Y


@阿什瓦尼这个答案以前是被接受的,现在却被拒绝了。但我看不出有什么原因(比如错误的答案,更好的答案)?