| --- |
| id: 'simple' |
| title: 'Simple' |
| --- |
| |
| <!-- |
| - Licensed to the Apache Software Foundation (ASF) under one or more |
| - contributor license agreements. See the NOTICE file distributed with |
| - this work for additional information regarding copyright ownership. |
| - The ASF licenses this file to You under the Apache License, Version 2.0 |
| - (the "License"); you may not use this file except in compliance with |
| - the License. You may obtain a copy of the License at |
| - |
| - http://www.apache.org/licenses/LICENSE-2.0 |
| - |
| - Unless required by applicable law or agreed to in writing, software |
| - distributed under the License is distributed on an "AS IS" BASIS, |
| - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| - See the License for the specific language governing permissions and |
| - limitations under the License. |
| --> |
| |
| # Simple Writing |
| |
| This chapter introduces how to use Fesod to perform simple spreadsheet writing operations. |
| |
| ## Overview |
| |
| Use Fesod for simple spreadsheet data writing to quickly write entity objects to spreadsheet files. |
| This is the most basic and commonly used writing approach. |
| |
| ## Code Examples |
| |
| ### POJO Class |
| |
| The `DemoData` POJO class corresponding to the spreadsheet structure: |
| |
| ```java |
| |
| @Getter |
| @Setter |
| @EqualsAndHashCode |
| public class DemoData { |
| @ExcelProperty("字符串标题") |
| private String string; |
| @ExcelProperty("日期标题") |
| private Date date; |
| @ExcelProperty("数字标题") |
| private Double doubleData; |
| @ExcelIgnore |
| private String ignore; // Ignored field |
| } |
| ``` |
| |
| ### Data List |
| |
| ```java |
| private List<DemoData> data() { |
| List<DemoData> list = ListUtils.newArrayList(); |
| for (int i = 0; i < 10; i++) { |
| DemoData data = new DemoData(); |
| data.setString("String" + i); |
| data.setDate(new Date()); |
| data.setDoubleData(0.56); |
| list.add(data); |
| } |
| return list; |
| } |
| ``` |
| |
| ### Writing Methods |
| |
| Fesod provides multiple writing methods, including `Lambda` expressions, data lists, `ExcelWriter` objects, etc. |
| |
| #### `Lambda` Expression |
| |
| ```java |
| |
| @Test |
| public void simpleWrite() { |
| String fileName = "simpleWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| FesodSheet.write(fileName, DemoData.class) |
| .sheet("Sheet1") |
| .doWrite(() -> data()); |
| } |
| ``` |
| |
| #### Data List |
| |
| ```java |
| |
| @Test |
| public void simpleWrite() { |
| String fileName = "simpleWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| FesodSheet.write(fileName, DemoData.class) |
| .sheet("Sheet1") |
| .doWrite(data()); |
| } |
| ``` |
| |
| #### `ExcelWriter` Object |
| |
| ```java |
| |
| @Test |
| public void simpleWrite() { |
| String fileName = "simpleWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| try (ExcelWriter excelWriter = FesodSheet.write(fileName, DemoData.class).build()) { |
| WriteSheet writeSheet = FesodSheet.writerSheet("Sheet1").build(); |
| excelWriter.write(data(), writeSheet); |
| } |
| } |
| ``` |