easyexcel 导出到web 样例代码
时间: 2024-01-20 19:04:38 浏览: 94
Sure, here is a sample code for exporting data to web using EasyExcel:
```java
// create a workbook
ExcelWriter writer = EasyExcel.write("output.xlsx").build();
// create a sheet
Sheet sheet = new Sheet(1, 0);
// set the column name
List<String> head = new ArrayList<>();
head.add("Name");
head.add("Age");
sheet.setHead(head);
// set the data
List<List<Object>> data = new ArrayList<>();
List<Object> row1 = new ArrayList<>();
row1.add("Alice");
row1.add(25);
data.add(row1);
List<Object> row2 = new ArrayList<>();
row2.add("Bob");
row2.add(30);
data.add(row2);
// write the data to the sheet
writer.write1(data, sheet);
// close the workbook
writer.finish();
```
Hope it helps!
相关问题
easyExcel导出本地excel模板代码
易Excel是一个帮助Java开发者快速进行复杂Excel操作的开源第三方库。要导出本地Excel模板代码,你需要先创建一个Excel模板,然后使用EasyExcel将数据写入Excel模板中。以下是一个示例代码:
String templateFileName = "template.xlsx";
String fileName = "output.xlsx";
OutputStream outputStream = new FileOutputStream(fileName);
ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate(templateFileName).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
List<YourData> dataList = getDataList();//在这个方法中你需要返回你要写入Excel的数据
excelWriter.write(dataList, writeSheet);
excelWriter.finish();
输出的Excel文件将基于你提供的本地Excel模板文件(template.xlsx)。希望这可以帮助你完成你的任务。
easyexcel导出可变列代码编写excel
以下是使用EasyExcel导出可变列的代码示例:
```java
@Test
public void testWriteExcel() {
String filename = "D:\\study\\excel\\user.xlsx";
// 创建ExcelWriter对象
ExcelWriter excelWriter = EasyExcel.write(filename).build();
// 创建Sheet对象
WriteSheet writeSheet = EasyExcel.writerSheet("用户信息").build();
// 定义表头
List<List<String>> head = new ArrayList<>();
List<String> headRow1 = new ArrayList<>();
headRow1.add("姓名");
headRow1.add("年龄");
head.add(headRow1);
// 定义数据
List<List<Object>> data = new ArrayList<>();
List<Object> rowData1 = new ArrayList<>();
rowData1.add("张三");
rowData1.add(20);
data.add(rowData1);
List<Object> rowData2 = new ArrayList<>();
rowData2.add("李四");
rowData2.add(25);
data.add(rowData2);
// 动态添加列
List<String> dynamicColumns = new ArrayList<>();
dynamicColumns.add("性别");
dynamicColumns.add("职业");
headRow1.addAll(dynamicColumns);
for (List<Object> rowData : data) {
rowData.add("男");
rowData.add("工程师");
}
// 写入表头和数据
excelWriter.write(head, writeSheet);
excelWriter.write(data, writeSheet);
// 关闭流
excelWriter.finish();
}
```
阅读全文