首頁 > Java > java教程 > 主體

如何使用SpringMVC產生一個Excel檔案?

不言
發布: 2018-10-20 16:55:33
轉載
2590 人瀏覽過

本篇文章帶給大家的內容是關於如何使用SpringMVC產生一個Excel檔案?有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

透過Java來產生Excel檔案或匯入一個Excel檔案的資料都需要用到Apache的一個POI包,這裡我就先把這個包提供出來。

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.9</version>
</dependency>
登入後複製

這裡我用的SpringBoot創建的項目,創建過程就不做累述了,直接操刀,開始把資料庫的資料導成一個Excel檔案
正式開始寫這個Demo
1.建立一個ExcelUtil類,這個類別就是對Excel進行操作,主要兩個方法。

1-1.exportFile():把資料匯出成一個Excel檔案;
1-2.importFile():把指定檔案的資料匯入進來;

users表結構如下:

 id | username  | password  |
+----+-----------+-----------+
|  1 | yx12156ok | yx27787ok |
|  2 | yangxiang | 123456    |
|  3 | zhangsan  | 666666    |
|  4 | wangwu    | 999999    |
|  5 | xiaoming  | xiaoming
登入後複製
這裡先貼出ExcelUtil類別導入的實現,下面再做詳細解釋:
public class ExcelUtil {
    
    private final String excel2003 = "xls";
    private final String excel2007 = "xlsx";
    
    private Workbook workbook;
    private Sheet sheet;
    private Row row;
    private Cell cell;
    private CellStyle style;
    
    private File file;
    
    //初始化表结构和生成表头
    public ExcelUtil(String[] titles,File file) {
        this.file = file;
        String fileName = this.file.getName();
        this.workbook = getWorkbook(fileName);
        if(workbook == null) return;
        this.sheet = this.workbook.createSheet();
        this.row = this.sheet.createRow(0);
        this.style = this.workbook.createCellStyle();
        this.style.setAlignment(CellStyle.ALIGN_CENTER);
        for(int i = 0 ; i < titles.length ; i ++) {
            cell = row.createCell(i);    //创建列
            cell.setCellValue(titles[i]);    //赋值
            cell.setCellStyle(style);    //样式
        }
    }
    
    public void genertedExportUsersFile(List<Users> data) throws IOException {    
        //遍历每一行数据
        for(int i = 0; i < data.size() ; i ++) {
            int j = 0;
            Users user = data.get(i);
            row = sheet.createRow(i+1);
            row.setRowStyle(style);
            row.createCell(j++).setCellValue(Optional.of(user.getId()).orElse(null));
            row.createCell(j++).setCellValue(Optional.of(user.getUsername()).orElse(null));
            row.createCell(j++).setCellValue(Optional.of(user.getPassword()).orElse(null));
        }
    }
    
    public void genertedExportStudentFile(List<Student> data) {
        //遍历每一行数据 
        for(int i = 0,j = 0 ; i < data.size() ; i ++) {
            Student student = data.get(i);
            row = sheet.createRow(i+1);
            row.setRowStyle(style);
            row.createCell(j++).setCellValue(Optional.of(student.getId()).orElse(null));
            row.createCell(j++).setCellValue(Optional.of(student.getUsername()).orElse(null));
            row.createCell(j++).setCellValue(Optional.of(student.getPassword()).orElse(null));
            row.createCell(j++).setCellValue(Optional.of(student.getStuname()).orElse(null));
            row.createCell(j++).setCellValue(Optional.of(student.getStusex()).orElse(null));
}
}
    
public void write() throws IOException {
    //把数据写入表格文件
    OutputStream out = new FileOutputStream(this.file);
    this.workbook.write(out);
    out.close();
}
private Workbook getWorkbook(String fileName) {
        //根据文件后缀名来获取Excel文件是2003版的还是2007版的
        String type = checkFileType(fileName);
        //根据版本的不同实例不同的对象
        if(type.equals(this.excel2003))
            return new HSSFWorkbook();
        else if(type.equals(this.excel2007))
            return new XSSFWorkbook();
        return null;
    }

private String checkFileType(String fileName) {
    return fileName.substring(fileName.lastIndexOf(".")+1);
}
}
登入後複製

現在我就來講一講幾個方法的作用:
Constructor方法:對錶結構物件進行初始化以及生產表頭;
genertedExportUsersFile方法:生成表的數據,此時資料應該還未真正寫入;
write方法:把產生的表寫入傳入的File類別裡,也就是建立的檔案;
getWorkbook方法:取得2003版本的或2007版本的Excel物件;
checkFileType方法:取得檔案的後綴名稱。

#

以上是如何使用SpringMVC產生一個Excel檔案?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:segmentfault.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!