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