通过java将csv转换为excel

时间:2015-06-23 14:28:33

标签: java apache-poi

我必须通过poi将csv转换为xls格式,因为我正在通过下面的poi对xls表进行一些操作是ii已经显示的代码

File file = new File("C:\\abc.csv");
            FileInputStream fin = null;
            fin = new FileInputStream(file);
            HSSFWorkbook workbook = new HSSFWorkbook(fin); 
            HSSFSheet firstSheet1 = workbook.getSheetAt(0);

现在我想写一个函数让我们说方法名称是convertcsvtoexcel,它将接受文件obj,作为回报,它将给我转换的xls文件,该文件将存储在我的c:驱动器中,名称为abcout.xls,稍后我会将它传递给工作簿如图所示,我已经尝试了以下代码,请告知我如何保管它以使其适合我的代码

ArrayList arList=null;
        ArrayList al=null;
        String fName = "test.csv";
        String thisLine; 
        int count=0; 

        FileInputStream file = null ;
        file   = new FileInputStream(new File("C:\\abc.csv"));

         //FileInputStream fis = new FileInputStream(file);
         DataInputStream myInput = new DataInputStream(file);
        int i=0;
        arList = new ArrayList();
        while ((thisLine = myInput.readLine()) != null)
        {
         al = new ArrayList();
         String strar[] = thisLine.split(",");
         for(int j=0;j<strar.length;j++)
         {
         al.add(strar[j]);
         }
         arList.add(al);
         System.out.println();
         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(Cell.CELL_TYPE_STRING);
             data=data.replaceAll("\"", "");
             data=data.replaceAll("=", "");
             cell.setCellValue(data);
            }else if(data.startsWith("\"")){
                data=data.replaceAll("\"", "");
                cell.setCellType(Cell.CELL_TYPE_STRING);
                cell.setCellValue(data);
            }else{
                data=data.replaceAll("\"", "");
                cell.setCellType(Cell.CELL_TYPE_NUMERIC);
                cell.setCellValue(data);
            }
            //*/
         //   cell.setCellValue(ardata.get(p).toString());
           }
           System.out.println();
          } 
         FileOutputStream fileOut = new FileOutputStream("C:\\abcout.xls");
         hwb.write(fileOut);
         fileOut.close();
         System.out.println("Your excel file has been generated");
        } catch ( Exception ex ) {
             ex.printStackTrace();
        } //main method end

    }

1 个答案:

答案 0 :(得分:0)

public static void csvToXLSX() {
    try {
        String csvFileAddress = "test.csv"; //csv file address
        String xlsxFileAddress = "test.xlsx"; //xlsx file address
        XSSFWorkbook workBook = new XSSFWorkbook();
        XSSFSheet 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++;
            XSSFRow 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");
    }
}