Poi工具使用

 引入poi依赖



     org.apache.poi
     poi
     3.17

POI导出

Poi工具使用_第1张图片

@RestController
@RequestMapping("/poi")
public class PoiController {

    @RequestMapping("/poiOut")
    public void poiImport(HttpServletResponse response) throws IOException {

        //新建excel文档
        HSSFWorkbook workbook = new HSSFWorkbook();

        //字体样式
        HSSFFont font = workbook.createFont();
        font.setColor(Font.COLOR_RED);
        font.setBold(true);

        //日期格式化
        HSSFDataFormat dataFormat = workbook.createDataFormat();
        short format = dataFormat.getFormat("yyyy-MM-dd");

        //标题单元格样式
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFont(font);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);

        //内容单元格样式
        HSSFCellStyle cellStyle1 = workbook.createCellStyle();
        cellStyle1.setDataFormat(format);

        //创建excel工作表
        HSSFSheet sheet = workbook.createSheet("aaa");
        sheet.setColumnWidth(3,40*256);  //必须乘于256

        //创建行
        HSSFRow row = sheet.createRow(0);

        String[] arr = {"ID","文章标题","文章描述","创建时间","热度"};

        for (int i=0;i list = new ArrayList
(); list.add(new Article("001","HTML5","文章没有内容,就是凑字数",new Date(),1)); list.add(new Article("002","HTML5","文章没有内容,就是凑字数",new Date(),2)); list.add(new Article("003","HTML5","文章没有内容,就是凑字数",new Date(),3)); list.add(new Article("004","HTML5","文章没有内容,就是凑字数",new Date(),4)); for (int i=0;i

你可能感兴趣的:(java)