excel导出

public class ExporTest {  
    private Map<String, CellStyle> smap;// 存储模板中单元格样式  
        @Test  
    public void test() {  
        Workbook wb = getWorkbook(path);  
        initStyle(wb);  
        writeSheet(wb.getSheetAt(1));  
        writeExcel(wb);  
    }  
  
    private Workbook getWorkbook(String filepath) {  
  
        Workbook wb = null;  
        try {  
            wb = new XSSFWorkbook(filepath);  
        } catch (Exception ex) {  
            ex.printStackTrace();  
        }  
        return wb;  
    }  
  
    /** 
     * 从模板中取单元格样式, 
     *  
     * @param wb 
     */  
    private void initStyle(Workbook wb) {  
  
        CellStyle normal = wb.getSheetAt(0).getRow(5).getCell(0).getCellStyle();  
        smap.put("normal", normal);  
    }  
  
    private void writeSheet(Sheet sh) {  
  
        for (int i = 0; i < ps.size(); i++) {  
            Row row = sh.createRow(i + 1);  
            Cell cell = row.createCell(0);  
            cell.setCellStyle(smap.get("normal"));  
            cell.setCellValue(permission.getName());  
        }  
    }  
  
    private void writeExcel(Workbook wb) {  
  
        try {  
            FileOutputStream out = new FileOutputStream("test.xls");  
            wb.write(out);  
            out.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
  
} 
原文地址:https://www.cnblogs.com/gym2017/p/7324520.html