Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion fesod-examples/fesod-sheet-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,27 @@
<name>Fesod Sheet Examples</name>

<dependencies>

<dependency>
<groupId>org.apache.fesod</groupId>
<artifactId>fesod-sheet</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback-classic.version}</version>
</dependency>

<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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.
*/

package org.apache.fesod.sheet.examples.advanced;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.fesod.sheet.FesodSheet;
import org.apache.fesod.sheet.context.AnalysisContext;
import org.apache.fesod.sheet.examples.advanced.converter.CustomStringStringConverter;
import org.apache.fesod.sheet.examples.util.ExampleFileUtil;
import org.apache.fesod.sheet.examples.write.data.DemoData;
import org.apache.fesod.sheet.read.listener.ReadListener;

/**
* Example demonstrating how to use custom converters for specialized data transformations.
*/
@Slf4j
public class CustomConverterExample {

public static void main(String[] args) {
String fileName = ExampleFileUtil.getTempPath("customConverter" + System.currentTimeMillis() + ".xlsx");
customConverterWrite(fileName);
customConverterRead(fileName);
}

/**
* Write with a custom converter registered for the operation.
*/
public static void customConverterWrite(String fileName) {
FesodSheet.write(fileName, DemoData.class)
.registerConverter(new CustomStringStringConverter())
.sheet("CustomConverter")
.doWrite(data());
log.info("Successfully wrote file with custom converter: {}", fileName);
}

/**
* Read with a custom converter registered for the operation.
*/
public static void customConverterRead(String fileName) {
FesodSheet.read(fileName, DemoData.class, new ReadListener<DemoData>() {
@Override
public void invoke(DemoData data, AnalysisContext context) {
log.info("Read data with custom converter: {}", data);
}

@Override
public void doAfterAllAnalysed(AnalysisContext context) {
log.info("Custom converter read completed");
}
})
.registerConverter(new CustomStringStringConverter())
.sheet()
.doRead();
}

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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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.
*/

package org.apache.fesod.sheet.examples.advanced;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.fesod.sheet.ExcelWriter;
import org.apache.fesod.sheet.FesodSheet;
import org.apache.fesod.sheet.examples.util.ExampleFileUtil;
import org.apache.fesod.sheet.examples.write.data.DemoData;
import org.apache.fesod.sheet.util.FileUtils;
import org.apache.fesod.sheet.write.handler.WorkbookWriteHandler;
import org.apache.fesod.sheet.write.handler.context.WorkbookWriteHandlerContext;
import org.apache.fesod.sheet.write.metadata.WriteSheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

/**
* Example demonstrating how to handle large files and compress temporary files.
*/
@Slf4j
public class LargeFileWriteExample {

public static void main(String[] args) {
compressedTemporaryFile();
}

/**
* Compress temporary files to save disk space.
*/
public static void compressedTemporaryFile() {
log.info("Temporary XML files are stored at: {}", FileUtils.getPoiFilesPath());
String fileName = ExampleFileUtil.getPath() + "largeFile" + System.currentTimeMillis() + ".xlsx";

try (ExcelWriter excelWriter = FesodSheet.write(fileName, DemoData.class)
.registerWriteHandler(new WorkbookWriteHandler() {
@Override
public void afterWorkbookCreate(WorkbookWriteHandlerContext context) {
Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
if (workbook instanceof SXSSFWorkbook) {
// Enable temporary file compression.
((SXSSFWorkbook) workbook).setCompressTempFiles(true);
}
}
})
.build()) {
WriteSheet writeSheet = FesodSheet.writerSheet("Template").build();
// Write 100,000 rows in batches.
for (int i = 0; i < 1000; i++) {
excelWriter.write(data(), writeSheet);
}
}
log.info("Successfully wrote large file: {}", fileName);
}

private static List<DemoData> data() {
List<DemoData> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
DemoData data = new DemoData();
data.setString("String" + i);
data.setDate(new Date());
data.setDoubleData(0.56);
list.add(data);
}
return list;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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.
*/

package org.apache.fesod.sheet.examples.advanced;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.fesod.sheet.FesodSheet;
import org.apache.fesod.sheet.context.AnalysisContext;
import org.apache.fesod.sheet.examples.util.ExampleFileUtil;
import org.apache.fesod.sheet.examples.write.data.DemoData;
import org.apache.fesod.sheet.read.listener.ReadListener;

/**
* Example demonstrating how to write and read password-protected Excel files.
*/
@Slf4j
public class PasswordProtectionExample {

public static void main(String[] args) {
String fileName = ExampleFileUtil.getTempPath("password" + System.currentTimeMillis() + ".xlsx");
String password = "password123";

log.info("Starting password protection example...");
passwordWrite(fileName, password);
passwordRead(fileName, password);
}

/**
* Write a password-protected Excel file.
*/
public static void passwordWrite(String fileName, String password) {
FesodSheet.write(fileName)
.password(password)
.head(DemoData.class)
.sheet("PasswordSheet")
.doWrite(data());
log.info("Successfully wrote password-protected file: {}", fileName);
}

/**
* Read a password-protected Excel file.
*/
public static void passwordRead(String fileName, String password) {
FesodSheet.read(fileName, DemoData.class, new ReadListener<DemoData>() {
@Override
public void invoke(DemoData data, AnalysisContext context) {
log.info("Read password-protected data: {}", data);
}

@Override
public void doAfterAllAnalysed(AnalysisContext context) {
log.info("Password-protected file read completed");
}
})
.password(password)
.sheet()
.doRead();
}

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@
* under the License.
*/

package org.apache.fesod.sheet.demo.read;
package org.apache.fesod.sheet.examples.advanced.converter;

import org.apache.fesod.sheet.converters.Converter;
import org.apache.fesod.sheet.converters.ReadConverterContext;
import org.apache.fesod.sheet.converters.WriteConverterContext;
import org.apache.fesod.sheet.enums.CellDataTypeEnum;
import org.apache.fesod.sheet.metadata.GlobalConfiguration;
import org.apache.fesod.sheet.metadata.data.ReadCellData;
import org.apache.fesod.sheet.metadata.data.WriteCellData;
import org.apache.fesod.sheet.metadata.property.ExcelContentProperty;

/**
* String and string converter
*
*
* Custom String to String converter.
*/
public class CustomStringStringConverter implements Converter<String> {

Expand All @@ -42,26 +41,15 @@ public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}

/**
* This method is called when reading data from Excel.
*
* @param context The context containing the cell data read from Excel.
* @return The converted Java data.
*/
@Override
public String convertToJavaData(ReadConverterContext<?> context) {
return "Custom:" + context.getReadCellData().getStringValue();
public String convertToJavaData(
ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return "Custom:" + cellData.getStringValue();
}

/**
* This method is called when writing data to Excel.
* (This is not relevant in this context and can be ignored.)
*
* @param context The context containing the Java data to be written.
* @return The converted Excel data.
*/
@Override
public WriteCellData<?> convertToExcelData(WriteConverterContext<String> context) {
return new WriteCellData<>(context.getValue());
public WriteCellData<?> convertToExcelData(
String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return new WriteCellData<>("Custom:" + value);
}
}
Loading
Loading