Skip to content
New issue

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

通过自定义单元格的方式为货币类型后,excel必须双击该单元格才会显示货币符号 #188

Open
SinceNovember opened this issue Jan 10, 2025 · 0 comments

Comments

@SinceNovember
Copy link

版本

1.0.0

问题描述

想通过自定义单元格的方式将单元格的数字类型变为货币类型,但excel下载之后单元格左上角有绿色的标志,必须双击该单元格才会显示货币符号
image

public class TestClass {

    public static void writeMultipleSheets(String fileName, List<SheetModel> sheetModels) {
        HttpServletResponse response =
            ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getResponse();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-disposition",
            "attachment; filename=" + UriUtils.encode(fileName, "UTF-8"));
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");

        try (ExcelWriter writer = FastExcel.write(response.getOutputStream()).build()) {
            int sheetNo = 0;
            for (SheetModel sheetModel : sheetModels) {
                ExcelWriterSheetBuilder excelWriterSheetBuilder =
                    FastExcel.writerSheet(sheetNo++, sheetModel.getSheetName());
                excelWriterSheetBuilder
                    .registerConverter(new FastExcelUtils.CommerceCellConverter());
                writer.write(sheetModel.getDataList(), excelWriterSheetBuilder.build());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

     static class CommerceCellConverter implements Converter<TestClass.CommerceCell> {
        private static final String DEFAULT_MONEY_FORMAT = "_ [$$] #,##0.00_ ;_ [$$] -#,##0.00_ ;_ [$$] #,##0.00_ ;_ @_ ";

        @Override
        public Class<?> supportJavaTypeKey() {
            return TestClass.CommerceCell.class;
        }

        @Override
        public WriteCellData<?> convertToExcelData(TestClass.CommerceCell value,
                                                   ExcelContentProperty contentProperty,
                                                   GlobalConfiguration globalConfiguration) {
            if (value == null) {
                return new WriteCellData<>("");
            }
            WriteCellData<Object> cellData = new WriteCellData<>(CellDataTypeEnum.STRING, value.getValue().toString());
            WorkBookUtil.fillDataFormat(cellData, DEFAULT_MONEY_FORMAT, "");
            return cellData;
        }
    }

    @Data
    @ToString
    @AllArgsConstructor
    @NoArgsConstructor
    public static class SheetModel {

        private String sheetName;

        private List<List<Object>> dataList;

        private List<String> headers;

    }

    @Data
    private static class CommerceCell {

        /**
         * 单元格内容
         */
        private Object value;

        /**
         * 如果是货币类型,传入货币的符号例如$
         */
        private String moneySymbol;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant