We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
package org.springblade.modules.api.utils; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.write.builder.ExcelWriterBuilder; import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.fill.FillConfig; import com.alibaba.excel.write.metadata.fill.FillWrapper; import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; import com.alibaba.fastjson.JSON; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.springblade.modules.api.entity.LedgerObserve; import org.springblade.modules.api.vo.LedgerObserveVO; import org.springframework.core.io.ClassPathResource; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; import java.util.HashMap; import java.util.List; import java.util.Map; public class ExcelExporterUtil { /** * @param response 响应 * @param templateFilePath 模版路径 * @param inputFileName 导出文件名 * @param List 数据列表 * @param map 单个对象 * @param sheetAt 第几个sheet * @param row 第几行开始 * @throws Exception */ public static void exportExcel(HttpServletResponse response, String templateFilePath, String inputFileName, List<?> List, Map<String, Object> map, Integer sheetAt, Integer row) throws Exception { response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 String fileNamePath = URLEncoder.encode(inputFileName, "UTF-8"); response.setHeader("Content-disposition", "attachment;filename=" + fileNamePath + ".xlsx"); //内容样式 WriteCellStyle contentWriteCellStyle = ExcelMergeUtil.getContentWriteCellStyleTaiZhang(); HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(null, contentWriteCellStyle); int rowHeight = getFourthRowHeightFromTemplate(templateFilePath, sheetAt, row); InputStream is = getInputStream(templateFilePath); ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(is) .registerWriteHandler(new CustomRowHeightHandler(rowHeight)) .registerWriteHandler(horizontalCellStyleStrategy) .excelType(ExcelTypeEnum.XLS) .build(); WriteSheet writeSheet = EasyExcel.writerSheet().build(); // 先填充map中的变量 excelWriter.fill(map, writeSheet); // 填充列表数据 FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build(); excelWriter.fill(List, fillConfig, writeSheet); excelWriter.finish(); } public static int getFourthRowHeightFromTemplate(String templateFilePath, Integer sheetAt, Integer row) throws Exception { InputStream is = getInputStream(templateFilePath); Workbook workbook = WorkbookFactory.create(is); Sheet sheet = workbook.getSheetAt(sheetAt); // 第一个工作表 Row fourthRow = sheet.getRow(row); // 第四行(索引从0开始) int rowHeight = fourthRow.getHeight(); workbook.close(); return rowHeight; } public static InputStream getInputStream(String templateFilePath) throws Exception { ClassPathResource res = new ClassPathResource(templateFilePath); InputStream is = res.getInputStream(); return is; } }
/** * 内容样式 * * @return */ public static WriteCellStyle getContentWriteCellStyleTaiZhang() { //内容策略 WriteCellStyle contentWriteCellStyle = new WriteCellStyle(); // 必须设置字体(避免NPE) WriteFont font = new WriteFont(); font.setFontName("宋体"); font.setFontHeightInPoints((short) 14); font.setColor(IndexedColors.BLACK.getIndex()); contentWriteCellStyle.setWriteFont(font); // 关键:字体不能为null //设置 水平居中 contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); //自动换行 contentWriteCellStyle.setWrapped(true); //垂直居中 contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //设置左边框 contentWriteCellStyle.setBorderLeft(BorderStyle.THIN); //设置右边框 contentWriteCellStyle.setBorderRight(BorderStyle.THIN); //设置上边框 contentWriteCellStyle.setBorderTop(BorderStyle.THIN); //设置下边框 contentWriteCellStyle.setBorderBottom(BorderStyle.THIN); return contentWriteCellStyle; }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
异常代码
策略代码
The text was updated successfully, but these errors were encountered: