csv转excle
package com.test.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;
@Component
public class FileConverter {
    /**
     * csv文件转excel
     * @param filepath  文件路径,转换到同目录下
     * @param filename  文件名,转换为相同文件名
     * @return
     */
    public static String convertCsv2Excel(String filepath,String filename) {
        try {
//            String csvFileAddress = "F:\\bgt\\api\\aaaaaaaaaaa.csv"; //csv file address
//            String xlsxFileAddress = "F:\\bgt\\api\\aaaaaaaaaaa.xlsx"; //xlsx file address
            String csvFileAddress = filepath.concat(File.separator).concat(filename).concat(".csv");
            String xlsxFileAddress = filepath.concat(File.separator).concat(filename).concat(".xlsx");
            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(",");
                XSSFRow currentRow=sheet.createRow(RowNum);
                for(int i=0;i<str.length;i++){
                    currentRow.createCell(i).setCellValue(str[i]);
                }
                RowNum++;
            }
            FileOutputStream fileOutputStream =  new FileOutputStream(xlsxFileAddress);
            workBook.write(fileOutputStream);
            fileOutputStream.close();
            br.close();
            return xlsxFileAddress;
        } catch (Exception ex) {
            System.out.println(ex.getMessage()+"Exception in try");
        }
        return "";
    }
}
本文来自博客园,作者:bgtong,转载请注明原文链接:https://www.cnblogs.com/bgtong/p/16309446.html
 
                    
                     
                    
                 
                    
                 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号