excel에 대한 완벽한 조작

1510 단어 미니멀리즘

excel에 대한 완벽한 조작

package com.dxm.ide.util;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.List;

public class CreateExcel {


    // excel
    public static String create(List datas, String path) throws Exception {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet(" ");
        sheet.setDefaultColumnWidth(20);

        // , 
        int rowNum = 0;
        for (String i : datas) {

            HSSFRow row = sheet.createRow(rowNum);
            String[] splits = i.split("\t");
            int m = 0;
            for (String j : splits) {
                row.createCell(m).setCellValue(j);
                m++;

            }
            rowNum++;
        }

        // excel 
        return buildExcelFile(path, workbook);


    }

    // excel 
    protected static String buildExcelFile(String path, HSSFWorkbook workbook) throws Exception {
        File file = new File(path + new Date().getTime() + ".xlsx");
        file.createNewFile();
        // excel 
        FileOutputStream fos = FileUtils.openOutputStream(file);
        workbook.write(fos);
        fos.flush();
        fos.close();

        return file.getAbsolutePath();
    }


}

좋은 웹페이지 즐겨찾기