在Java程序中使用jXLS導出Excel格式文檔

 

jXLS是什麼東東?

  • 官方介紹:jXLS is a small and easy-to-use Java library for writing Excel files using XLS templates and reading data from Excel into Java objects using XML configuration.
  • 哎呀這是什麼呀,來點能看的懂的吧,這個大體的意思就是:jXLS是一個基於XML配置的小型,易於使用java類庫,可以用來寫Excel格式文檔,也可以用來讀取Excel格式文檔中的數據。
  • 通俗的說,在java程序中,我們可以基於Excel的模板和最終Excel文檔中顯示的數據,使用jXLS完成Excel格式文檔的生成。這就要求我們在java程序中完成以下幾項內容:
    • 添加jXLS相關的jar包
    • 在java程序中準備好需要生成到Excel文檔中的數據
    • 製作Excel格式的模板
    • 編寫基於jXLS的代碼,把Excel模板和數據整合,生成Excel格式文檔

基本使用

  • 需求描述:把100名員工的信息(包含員工編號,員工姓名,員工性別,員工年齡,員工薪資)寫入到Excel格式文檔中。
  • 實現
    • 步驟一:創建一個maven工程,並添加依賴

    [Java] 純文本查看 複製代碼

    ?

    01

    02

    03

    04

    05

    06

    07

    08

    09

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    <dependencies>

                    <!-- jxls核心依賴 -->

                    <dependency>

                            <groupId>net.sf.jxls</groupId>

                            <artifactId>jxls-core</artifactId>

                            <version>1.0.6</version>

                    </dependency>

                    <!-- junit單元測試依賴 -->

                    <dependency>

                            <groupId>junit</groupId>

                            <artifactId>junit</artifactId>

                            <version>4.12</version>

                    </dependency>

                    <!-- 日誌的依賴 -->

                    <dependency>

                            <groupId>org.slf4j</groupId>

                            <artifactId>slf4j-log4j12</artifactId>

                            <version>1.7.12</version>

                    </dependency>

            </dependencies>

     
    • 步驟二:編寫員工信息實體類

    [Java] 純文本查看 複製代碼

    ?

    01

    02

    03

    04

    05

    06

    07

    08

    09

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    public class EmployeeInfo {

     

            private String empNo;//員工編號

            private String empName;//員工姓名

            private String empSex;//員工性別:1代表男,0代表女

            private Integer empAge;//員工年齡

            private Double empSalary;//員工薪資

            public String getEmpNo() {

                    return empNo;

            }

            public void setEmpNo(String empNo) {

                    this.empNo = empNo;

            }

            public String getEmpName() {

                    return empName;

            }

            public void setEmpName(String empName) {

                    this.empName = empName;

            }

            public String getEmpSex() {

                    return empSex;

            }

            public void setEmpSex(String empSex) {

                    this.empSex = empSex;

            }

            public Integer getEmpAge() {

                    return empAge;

            }

            public void setEmpAge(Integer empAge) {

                    this.empAge = empAge;

            }

            public Double getEmpSalary() {

                    return empSalary;

            }

            public void setEmpSalary(Double empSalary) {

                    this.empSalary = empSalary;

            }

    }

     
    • 步驟三:模擬查詢數據庫得到100名員工的信息

    [Java] 純文本查看 複製代碼

    ?

    01

    02

    03

    04

    05

    06

    07

    08

    09

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    public class EmployeeDao {

     

            /**

             * 模擬查詢數據庫得到100名員工的信息

             * @return :100名員工的信息

             */

            public List<EmployeeInfo> findEmployees(){

                    List<EmployeeInfo> list = new ArrayList<EmployeeInfo>();

                     

                    for(int i=1;i<=100;i++){

                            EmployeeInfo emp = new EmployeeInfo();

                            emp.setEmpNo(UUID.randomUUID().toString());//使用UUID生成隨機

                            emp.setEmpName("員工姓名"+i);

                            emp.setEmpAge(new Random().nextInt(40));//隨機生成年齡

                            emp.setEmpSex(i%2+"");//根據序號生成員工的性別

                            emp.setEmpSalary(new Random().nextDouble()*10000);//隨機生成薪資

                             

                            list.add(emp);

                    }

                     

                    return list;

            }

    }

     
    • 步驟四:製作Excel文檔模板,複製到程序中src目錄下
           說明:在模板中使用jxls的表達式`<jx:forEach items="" var="" ></jx:forEach>`,js:forEach的作用類似於jstl中的c:forEach,用來遍歷集合,items的值要和步驟五中的map集合中的key值一致,var值是每次遍歷得到的元素,取元素中的值使用 ${元素的名稱} 格式的表達式。
    • 步驟五:編寫導出Excel文檔的代碼

    [Java] 純文本查看 複製代碼

    ?

    01

    02

    03

    04

    05

    06

    07

    08

    09

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    public class ExportExcel {

     

            public void expExcelWithJxls() throws Exception{

                    //1、獲取導出文檔中的數據,存放到一個Map集合中

                    EmployeeDao empDao = new EmployeeDao();

                    List<EmployeeInfo> employees = empDao.findEmployees();

                    Map<String,Object> map = new HashMap<String,Object>();

                    map.put("emps", employees);

                     

                    //2、使用ClassLoader讀取模板

                    InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("emp_template.xlsx");

                     

                    //3、創建Excel文檔轉換器

                    XLSTransformer xlsTransformer = new XLSTransformer();

                     

                    //4、基於數據和模板,轉換成Excel文檔

                    //第一個參數是模板的輸入流

                    //第二個參數是需要生成到文檔中的數據

                    Workbook workbook = xlsTransformer.transformXLS(inputStream, map);

                     

                    //5、把Excel文檔輸出到指定位置

                    FileOutputStream outputStream =  new FileOutputStream("d:/jxls/empInfo.xlsx");

                    workbook.write(outputStream);

            }

    }

     
    • 步驟六:編寫測試程序,測試導出員工信息的文檔

    [Java] 純文本查看 複製代碼

    ?

    01

    02

    03

    04

    05

    06

    07

    08

    09

    10

    11

    12

    13

    public class ExcelTest {

     

            @Test

            public void exportExcel(){

                    ExportExcel excel = new ExportExcel();

                    try {

                            excel.expExcelWithJxls();

                    } catch (Exception e) {

                            e.printStackTrace();

                            System.out.println("導出文檔出現錯誤了,錯誤信息"+e.getMessage());

                    }

            }

    }

  • 導出Excel文檔的效果
     

      

  • 其中存在問題
    • 問題描述:從導出的Excel文檔的效果可以看出其中的性別顯示的1或者0,而正常情況下應該顯示男或者女,接下來就需要對此問題進行修復
    • 問題修復:使用jXLS提供的另一個指令<jx:if test=""></jx:if>,其作用是判斷值,從而指定顯示的內容
    • 修改後的模板
    • 生成Excel文檔的效果
  • 需求變更:在生成Excel文檔的同時,計算出員工薪資總金額,以及平均值
    • 若要完成該需求,只需要修改Excel模板,修改之後的模板
    • 生成Excel文檔的效果
    • 更多免費技術資料可關注:annalin1203
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章