将CSV文件转换为XLS文件,逗号有问题

时间:2019-04-29 20:01:37

标签: java csv split

我正在将CSV文件转换为XLS文件。 当我进行转换时,一切正常。但是,当单元格中有逗号时,它将一列分为两列。

EX: |你好,世界! | CSV文件|

转换后

|你好世界! | CSV文件| X ---我现在得到什么

|你好,世界! | CSV文件|哦---我想要什么

String ab = thisLine.replaceAll(“,”,“”);

假设每个人在使用逗号后都使用空格,那么replaceAll可以工作,但这不是理想的解决方案。

public void csv2excel(String csv) throws Exception
    {
        String inputCSVFile = csv + ".csv";
        ArrayList arList=null;
        ArrayList al=null;
        String fName = inputCSVFile;
        String thisLine; 
        int count=0; 
         FileInputStream fis = new FileInputStream(fName);
         DataInputStream myInput = new DataInputStream(fis);
        int i=0;
        arList = new ArrayList();
        while ((thisLine = myInput.readLine()) != null)
        {
         al = new ArrayList();
         //String ab = thisLine.replaceAll(", ", " ");
         //String strar[] ab.split(",");

         String strar[] = thisLine.split(",");
//Here is where I split the columns.


         for(int j=0;j<strar.length;j++)
         {
         al.add(strar[j]);
         }
         arList.add(al);
         i++;
        } 
        try
        {
         HSSFWorkbook hwb = new HSSFWorkbook();
         HSSFSheet sheet = hwb.createSheet("new sheet");
          for(int k=0;k<arList.size();k++)
          {
           ArrayList ardata = (ArrayList)arList.get(k);
           HSSFRow row = sheet.createRow((short) 0+k);
           for(int p=0;p<ardata.size();p++)
           {
            HSSFCell cell = row.createCell((short) p);
            String data = ardata.get(p).toString();
            if(data.startsWith("=")){
             cell.setCellType(CellType.STRING);
             data=data.replaceAll("\"", "");
             data=data.replaceAll("=", "");
             cell.setCellValue(data);
            }else if(data.startsWith("\"")){
                data=data.replaceAll("\"", "");
                cell.setCellType(CellType.STRING);
                cell.setCellValue(data);
            }else{
                data=data.replaceAll("\"", "");
                cell.setCellType(CellType.NUMERIC);
                cell.setCellValue(data);
            }
            //*/
         //   cell.setCellValue(ardata.get(p).toString());
           }
          } 
         FileOutputStream fileOut = new FileOutputStream(csv + ".xls");
         hwb.write(fileOut);
         fileOut.close();
        } catch ( Exception ex ) {
             ex.printStackTrace();
        }
        myInput.close();
        fis.close();
    }

我想知道是否只有一种方法可以逐列拆分。

1 个答案:

答案 0 :(得分:0)

public void csv2excel(String csv) throws Exception
    {
        String inputCSVFile = csv + ".csv";
        ArrayList arList=null;
        ArrayList al=null;
        String fName = inputCSVFile;
        String thisLine; 
        int count=0; 
         FileInputStream fis = new FileInputStream(fName);
         DataInputStream myInput = new DataInputStream(fis);
        int i=0;
        arList = new ArrayList();
        while ((thisLine = myInput.readLine()) != null)
        {
         al = new ArrayList();
         StringBuilder myLine = new StringBuilder(thisLine);
         int lineLength = thisLine.length();
         int bool = 0;
         for (int k = 0; k < lineLength; k++)
         {
             if (thisLine.charAt(k) == '"')
             {
                 bool++;
             }
             else if (thisLine.charAt(k) == ',' && bool % 2 != 0)
             {
                 myLine.setCharAt(k, ' ');
             }
         }

         //System.out.println(myLine);
         //String ab = thisLine.replaceAll(", ", " ");
         String strar[] = myLine.toString().split(",");
         for(int j=0;j<strar.length;j++)
         {
         al.add(strar[j]);
         }
         arList.add(al);
         i++;
        } 
        try
        {
         HSSFWorkbook hwb = new HSSFWorkbook();
         HSSFSheet sheet = hwb.createSheet("new sheet");
          for(int k=0;k<arList.size();k++)
          {
           ArrayList ardata = (ArrayList)arList.get(k);
           HSSFRow row = sheet.createRow((short) 0+k);
           for(int p=0;p<ardata.size();p++)
           {
            HSSFCell cell = row.createCell((short) p);
            String data = ardata.get(p).toString();
            if(data.startsWith("=")){
             cell.setCellType(CellType.STRING);
             data=data.replaceAll("\"", "");
             data=data.replaceAll("=", "");
             cell.setCellValue(data);
            }else if(data.startsWith("\"")){
                data=data.replaceAll("\"", "");
                cell.setCellType(CellType.STRING);
                cell.setCellValue(data);
            }else{
                data=data.replaceAll("\"", "");
                cell.setCellType(CellType.NUMERIC);
                cell.setCellValue(data);
            }
            //*/
         //   cell.setCellValue(ardata.get(p).toString());
           }
          } 
         FileOutputStream fileOut = new FileOutputStream(csv + ".xls");
         hwb.write(fileOut);
         fileOut.close();
        } catch ( Exception ex ) {
             ex.printStackTrace();
        }
        myInput.close();
        fis.close();
    }

我编写了代码以解决该问题... 看起来很丑,但是行得通...

想知道是否有人有更好的答案。