| --- |
| id: 'simple-example' |
| title: 'Simple example' |
| --- |
| |
| <!-- |
| - 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. |
| --> |
| |
| ## Fesod Sheet Examples |
| |
| ### Read the spreadsheet |
| |
| Below is an example of reading a spreadsheet document: |
| |
| ```java |
| // Implement the ReadListener interface to set up operations for reading data |
| public class DemoDataListener implements ReadListener<DemoData> { |
| @Override |
| public void invoke(DemoData data, AnalysisContext context) { |
| System.out.println("Parsed a data entry" + JSON.toJSONString(data)); |
| } |
| |
| @Override |
| public void doAfterAllAnalysed(AnalysisContext context) { |
| System.out.println("All data parsed!"); |
| } |
| } |
| |
| public static void main(String[] args) { |
| String fileName = "demo.xlsx"; |
| // Read file |
| FesodSheet.read(fileName, DemoData.class, new DemoDataListener()).sheet().doRead(); |
| } |
| ``` |
| |
| ### Write the spreadsheet |
| |
| Below is a simple example of creating a spreadsheet document: |
| |
| ```java |
| // Sample data class |
| public class DemoData { |
| @ExcelProperty("String Title") |
| private String string; |
| @ExcelProperty("Date Title") |
| private Date date; |
| @ExcelProperty("Number Title") |
| private Double doubleData; |
| @ExcelIgnore |
| private String ignore; |
| } |
| |
| // Prepare data to write |
| private static List<DemoData> data() { |
| List<DemoData> list = new ArrayList<>(); |
| 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; |
| } |
| |
| public static void main(String[] args) { |
| String fileName = "demo.xlsx"; |
| // Create a "Template" sheet and write data |
| FesodSheet.write(fileName, DemoData.class).sheet("Template").doWrite(data()); |
| } |
| ``` |