| | |
| | | |
| | | import com.zy.acs.framework.common.Cools; |
| | | import io.swagger.annotations.ApiModelProperty; |
| | | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
| | | 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.xssf.usermodel.XSSFWorkbook; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | |
| | | public static void build(Workbook workbook, HttpServletResponse response) { |
| | | response.reset(); |
| | | Http.cors(response); |
| | | response.setContentType("application/octet-stream; charset=utf-8"); |
| | | |
| | | response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8"); |
| | | |
| | | try { |
| | | response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("export", "UTF-8")); |
| | | response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("export.xlsx", "UTF-8")); |
| | | workbook.write(response.getOutputStream()); |
| | | workbook.close(); |
| | | |
| | | } catch (IOException ignore) {} |
| | | } |
| | | |
| | | public static <T> Workbook create(List<T> list, Class<T> clz) { |
| | | HSSFWorkbook workbook = new HSSFWorkbook(); |
| | | XSSFWorkbook workbook = new XSSFWorkbook(); |
| | | Sheet sheet = workbook.createSheet(clz.getSimpleName()); |
| | | |
| | | Row header = sheet.createRow(0); |
| | | Field[] fields = Cools.getAllFields(clz); |
| | | |
| | | int headerIdx = 0; |
| | | for (Field field : fields) { |
| | | if (Modifier.isFinal(field.getModifiers()) |
| | |
| | | || Modifier.isTransient(field.getModifiers())) { |
| | | continue; |
| | | } |
| | | |
| | | String memo = "Undefined"; |
| | | if (field.isAnnotationPresent(ApiModelProperty.class)) { |
| | | memo = field.getAnnotation(ApiModelProperty.class).value(); |
| | |
| | | } |
| | | } |
| | | |
| | | for (int i = 0; i <= fields.length; i++) { |
| | | for (int i = 0; i <= headerIdx; i++) { |
| | | sheet.autoSizeColumn(i); |
| | | } |
| | | |