Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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
通过java将csv转换为excel_Java_Apache Poi - Fatal编程技术网

通过java将csv转换为excel

通过java将csv转换为excel,java,apache-poi,Java,Apache Poi,我必须通过JavaPOI将CSV转换为XLS格式,因为我正在通过POI对XLS工作表进行一些操作。下面是我的代码: File file = new File("C:\\abc.csv"); FileInputStream fin = null; fin = new FileInputStream(file); HSSFWorkbook workbook = new HSSFWorkbook(fin); HSSFSheet firstSheet1 = workbook.get

我必须通过JavaPOI将CSV转换为XLS格式,因为我正在通过POI对XLS工作表进行一些操作。下面是我的代码:

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文件,该文件将存储在名为abcout.XLS的
C:
驱动器中,稍后我将把它传递到工作簿,如图所示。我尝试了以下代码。请告知我如何保管它,使其适合我的代码

ArrayList arList=null;
ArrayList al=null;
字符串fName=“test.csv”;
把这条线串起来;
整数计数=0;
FileInputStream文件=null;
文件=新文件输入流(新文件(“C:\\abc.csv”);
//FileInputStream fis=新的FileInputStream(文件);
DataInputStream myInput=新的DataInputStream(文件);
int i=0;
arList=新的ArrayList();
而((thisLine=myInput.readLine())!=null){
al=新的ArrayList();
字符串strar[]=thisLine.split(“,”);
对于(int j=0;j
公共静态void csvToXLSX(){
试一试{
字符串csvFileAddress=“test.csv”//csv文件地址
字符串xlsxFileAddress=“test.xlsx”;//xlsx文件地址
XSSFWorkbook工作簿=新XSSFWorkbook();
XSSFSheet sheet=workBook.createSheet(“sheet1”);
字符串currentLine=null;
int RowNum=0;
BufferedReader br=新的BufferedReader(新文件读取器(csvFileAddress));
而((currentLine=br.readLine())!=null){
字符串str[]=currentLine.split(“,”);
RowNum++;
XSSFRow currentRow=sheet.createRow(RowNum);

对于(int i=0;复制的ILOT:)等@Alex谢谢,但我想开发一个单独的函数
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");
    }
}