easyexcel 导出居中
时间: 2023-08-19 10:12:51 浏览: 149
要实现easyexcel导出的内容居中,可以使用EasyExcel提供的CellStyleStrategy接口来自定义样式策略。首先,你需要创建一个实现了CellStyleStrategy接口的类,然后在该类中重写customize方法,在该方法中设置单元格样式,使内容居中。
以下是一个示例代码:
```java
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.write.style.AbstractCellStyleStrategy;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
public class CenterCellStyleStrategy extends AbstractCellStyleStrategy {
@Override
protected void customize(Cell cell, CellStyle cellStyle) {
cellStyle.setAlignment(HorizontalAlignment.CENTER);
}
@Override
public void afterSheetCreate(Workbook workbook, String sheetName) {
// Do nothing
}
}
```
然后,在你的导出方法中注册这个样式策略:
```java
@PostMapping("/downloadBack")
public void downloadBack(HttpServletResponse response, String siteId, Integer content) throws TException, IOException {
List<Feedback> list = feedbackService.searchBack(siteId, content);
setResponse(response, "列表");
EasyExcel.write(response.getOutputStream(), ExcelFeedbackVO.class)
.sheet("列表")
.registerWriteHandler(new CenterCellStyleStrategy()) // 注册样式策略
.doWrite(list);
}
```
这样,导出的内容就会居中显示了。
#### 引用[.reference_title]
- *1* *2* *3* [EasyExcel导入导出样式、自适应列宽、自适应行高](https://blog.csdn.net/m0_73900214/article/details/130821280)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文