開發環境
- jdk 1.8
- Maven 3.6
- SpringBoot 2.1.4.RELEASE
- aspose-cells 8.5.2
- Idea
或
先看效果
模板:
導出后效果:
引入jar包
pom.xml
如遇到jar無法下載的情況,可自行下載到本地,然后手動添加到項目中
<dependencies>
<!-- 你的其他jar -->
<!-- aspose -->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>8.5.2</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>http://repository.aspose.com/repo/</url>
</repository>
</repositories>
校驗許可證
Aspose默認引入的是評估版(未指定許可證),此版本可使用全部功能,但是有以下兩個限制:
- 運行程序時,只能打開100個Excel文件。如果您的應用程序超過此數量,將引發異常。
- 帶有評估水印的工作表
如果你的應用場景不能接受以上兩點限制,可在官網購買許可證,然后通過校驗許可證解除限制;如果你可以接受,請忽略此操作;
官網提供多種校驗方式,此處只列舉其中一種將License配置在項目中
在resources包下添加license.xml
創建AsposeUtils工具類,添加方法校驗方法
package com.wayne.common.utils;
import com.aspose.cells.License;
import java.io.InputStream;
/**
* Aspose工具類
* @author Wayne
* @date 2019/6/10
*/
public class AsposeUtils {
/**
* 校驗Aspose的License
*/
private static Boolean checkLicense() {
try {
InputStream license = AsposeUtils.class.getClassLoader().getResourceAsStream("license.xml");
License aposeLic = new License();
aposeLic.setLicense(license);
} catch (Exception e) {
e.printStackTrace();
}
return License.isLicenseSet();
}
}
導出方法
此處列舉為較簡單的單個sheet,且不分頁導出,更多使用方式請參考官網文檔
package com.wayne.common.utils;
import com.aspose.cells.License;
import com.aspose.cells.Workbook;
import com.aspose.cells.WorkbookDesigner;
import java.io.InputStream;
import java.util.List;
/**
* Aspose工具類
* @author Wayne
* @date 2019/6/10
*/
public class AsposeUtils {
/**
* @param head 單個對象,將對象作為此參數傳入。如沒有,傳入null
* @param list 多個對象時,將對象作為此參數傳入。如沒有,傳入null
* @param templateName 模板所在的位置,如:E:/template/studentTemplate.xlsx
* @param resultFilePath 生成后的文件所存放的文件夾,如:E:/data/
* @return 生成后的文件路徑及文件名,如:E:/data/student.xlsx
*/
public static <H> String exportExcelByAsposeWithTemplate(H head, List list, String templateName, String resultFilePath) {
// 校驗許可證
if(!checkLicense()) {
return null;
}
// 生成后文件名
String resultFile = resultFilePath + System.currentTimeMillis() + ".xlsx";
try {
// 加載模板
Workbook wb = new Workbook(templateName);
// 加載設計器
WorkbookDesigner designer = new WorkbookDesigner();
designer.setWorkbook(wb);
// 單個對象和集合區分(在模板中定義方式不同)
if(null != head) {
designer.setDataSource("Head", head);
}
if(null != list) {
designer.setDataSource("List", list);
}
designer.process();
wb.save(resultFile);
wb.dispose();
} catch (Exception e) {
e.printStackTrace();
}
return resultFile;
}
/**
* 校驗Aspose的License
*/
private static Boolean checkLicense() {
try {
InputStream license = AsposeUtils.class.getClassLoader().getResourceAsStream("license.xml");
License aposeLic = new License();
aposeLic.setLicense(license);
} catch (Exception e) {
e.printStackTrace();
}
return License.isLicenseSet();
}
}
測試結果
@Test
public void exportExcelByAsposeWithTemplateTestCase() {
String templateName = "E:/Temp/Template.xlsx";
String resultFilePath = "E:/Temp/";
// 單個對象測試
UserOne userOne = new UserOne();
userOne.setId(1);
userOne.setUsername("Tom");
userOne.setPassword("123123");
// 集合測試
List<UserTwo> lists = new ArrayList<>();
for(int i = 0; i < 10; i++) {
UserTwo temp = new UserTwo();
temp.setId(i*10);
temp.setUsername(String.valueOf((i*100)));
lists.add(temp);
}
String resultFileName = AsposeUtils.exportExcelByAsposeWithTemplate(userOne, lists, templateName, resultFilePath);
assert null != resultFileName;
}
運行測試用例,綠了~~~
占位符
常規占位 (¬_¬)…