poi生成excel文件

一, poi生成excel文件

  1)引入依賴前端

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
    <!-- 報表 -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.9</version>
    </dependency>
    
    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.9</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.9</version>
    </dependency>

二,調用

@Controller
public class PoiController {
    
    @Autowired
    private LogMapper logMapper;
    
    @RequestMapping("getpoi")
    public void getpoi(HttpServletResponse response) {
        //獲取登陸日誌表
        List<Log> logList=logMapper.getloglist();
        //建立對象 SXSSFWorkbook用於處理大量數據問題,使用內存達到效果,再結合分頁查詢,一個表達到指定容量就開啓下張表;
        //SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
        HSSFWorkbook workbook = new HSSFWorkbook();
        //建立表
        HSSFSheet sheet = workbook.createSheet("登陸日誌表");
        //建立第一行,填充名稱
        HSSFRow row = sheet.createRow(0);
        
        //獲取第一行每一個單元格進行填充
        HSSFCell createCell0 = row.createCell(0);
        createCell0.setCellValue("id");
        
        HSSFCell createCell1 = row.createCell(1);
        createCell1.setCellValue("用戶IP地址");
        
        HSSFCell createCell2 = row.createCell(2);
        createCell2.setCellValue("用戶登陸時間");
        
        HSSFCell createCell3 = row.createCell(3);
        createCell3.setCellValue("用戶名");
        
        HSSFCell createCell4 = row.createCell(4);
        createCell4.setCellValue("前端登陸/後端登陸");
        HSSFRow rowi = null;
        HSSFCell createCell =null;
        for (int i = 0; i < logList.size(); i++) {
            //建立第二行,填充內容
             rowi = sheet.createRow(i+1);
             
            createCell = rowi.createCell(0);//獲取第一個單元格
            createCell.setCellValue(logList.get(i).getiId());
            
            createCell = rowi.createCell(1);//獲取第二個單元格
            createCell.setCellValue(logList.get(i).getiIp());
            
            createCell = rowi.createCell(2);//獲取第三個單元格
            createCell.setCellValue(logList.get(i).getiDate());
            
            createCell = rowi.createCell(3);//獲取第四個單元格
            createCell.setCellValue(logList.get(i).getuName());
            
            createCell = rowi.createCell(4);//獲取第五個單元格
            createCell.setCellValue("1".equals(logList.get(i).getiType())?"前端登陸":"後端登陸");
                
        }
        //寫出
        //這個流從response獲取,再配置響應頭信息就能夠實現下載;
        OutputStream out=null;
        try {
            out = new FileOutputStream(new File("F:/test","test.xls"));
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        
        try {
            
            workbook.write(out);
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

三,效果展現

 

 

 

相關文章
相關標籤/搜索