设定excel导出时单元格的格式

一、需求

要求excel导出时,对应列里面的内容格式为日期,数值格式并有精度要求 ,如下图:

使用alibaba,easyexcel,默认的导出数据格式为文本,excel显示为常规,使用数据规范注解@NumberFormat一直不生效,无奈选择原生的apache.poi。

二、具体实现

java 复制代码
   public void writePoi(List<Export> exportList) {
        // 1.在内存中创建一个excel文件,excel2003之前版本使用HSSFWorkbook,之后版本使用XSSFWorkbook
        HSSFWorkbook workbook = new HSSFWorkbook();
//        XSSFWorkbook workbook = new XSSFWorkbook();
        // 2.创建工作簿
        HSSFSheet sheet = workbook.createSheet("已开具发票");
        //设置列宽
        sheet.setDefaultColumnWidth(15);
        // 3.创建标题行

        HSSFRow titlerRow = sheet.createRow(0);

        titlerRow.createCell(0).setCellValue("开票日期");
        titlerRow.createCell(1).setCellValue("发票号码");
        titlerRow.createCell(2).setCellValue("购方税号");
        titlerRow.createCell(3).setCellValue("购方名称");
        titlerRow.createCell(4).setCellValue("规格型号");
        titlerRow.createCell(5).setCellValue("计量单位");
        titlerRow.createCell(6).setCellValue("单价");
        titlerRow.createCell(7).setCellValue("数量");
        titlerRow.createCell(8).setCellValue("金额");
        titlerRow.createCell(9).setCellValue("税额");
        
        //4.设置需要的格式
        DataFormat dataFormat = workbook.createDataFormat();
        CellStyle dateStyle = workbook.createCellStyle();
        dateStyle.setDataFormat(dataFormat.getFormat("yyyy-m-d;@")); // 设置日期格式,注意日期格式规范之后不会直接显示单元格式为日期,但是使用筛选功能时可以按照日期范围进行筛选。
        dateStyle.setAlignment(HorizontalAlignment.CENTER); // 设置水平居中

        CellStyle doubleStyle = workbook.createCellStyle();
        doubleStyle.setDataFormat(dataFormat.getFormat("0.00_ ")); // 设置数值格式
        doubleStyle.setAlignment(HorizontalAlignment.CENTER); // 设置水平居中

        CellStyle floatStyle = workbook.createCellStyle();
        floatStyle.setDataFormat(dataFormat.getFormat("0.00000000_ ")); // 设置单价格式
        floatStyle.setAlignment(HorizontalAlignment.CENTER); // 设置水平居中

        // 5.遍历数据,创建数据行
        for (Export export : exportList) {
            // 获取最后一行的行号
            int lastRowNum = sheet.getLastRowNum();
            // 添加新行
            HSSFRow dataRow = sheet.createRow(lastRowNum + 1);
            HSSFCell hssfCell0 = dataRow.createCell(0);
            hssfCell0.setCellStyle(dateStyle);
            hssfCell0.setCellValue(export.getKprq());
            dataRow.createCell(1).setCellValue(export.getFphm());
            dataRow.createCell(2).setCellValue(export.getGfsh());
            dataRow.createCell(3).setCellValue(export.getGfmc());
            dataRow.createCell(4).setCellValue(export.getGgxh());
            dataRow.createCell(5).setCellValue(export.getJldw());
            HSSFCell hssfCell6 = dataRow.createCell(6);
            hssfCell6.setCellStyle(floatStyle);
            hssfCell6.setCellValue(export.getDj());

            HSSFCell hssfCell7 = dataRow.createCell(7);
            hssfCell7.setCellStyle(doubleStyle);
            hssfCell7.setCellValue(export.getSl());

            HSSFCell hssfCell8 =dataRow.createCell(8);
            hssfCell8.setCellStyle(doubleStyle);
            hssfCell8.setCellValue(export.getJe());

            HSSFCell hssfCell9 =dataRow.createCell(9);
            hssfCell9.setCellStyle(doubleStyle);
            hssfCell9.setCellValue(export.getSe());
        }
        // 设置自适应列宽
        for (int i = 0; i < sheet.getRow(0).getLastCellNum(); i++) {
            sheet.autoSizeColumn(i);
        }
        // 5.创建文件名
        String fileName = "已开发票明细导出_" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
        // 6.获取输出流对象
        OutputStream outputStream;
        try {
            outputStream = new FileOutputStream(excelFilePath + "\\" + fileName);
        // 7.写出文件,关闭流
            workbook.write(outputStream);
            workbook.close();
            outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();

        }
    }

注:数据规范所需的format规则可从excel中的格式设置看到,具体步骤为:

右键单击单元格,选择设置单元格格式,1选择你需要的格式,2设定好精度,

3直接点击自定义,即可看到format规范,注意数值格式的规范后面必须有一个空格,否则不生效,可以看到下图的下划线与光标之间是有一个空格的。

相关推荐
沉到海底去吧Go7 小时前
【行驶证识别成表格】批量OCR行驶证识别与Excel自动化处理系统,行驶证扫描件和照片图片识别后保存为Excel表格,基于QT和华为ocr识别的实现教程
自动化·ocr·excel·行驶证识别·行驶证识别表格·批量行驶证读取表格
Abigail_chow1 天前
EXCEL如何快速批量给两字姓名中间加空格
windows·microsoft·excel·学习方法·政务
xiaohezi2 天前
Rag chunk 之:Excel 文档解析
excel
weixin_472339462 天前
python批量解析提取word内容到excel
python·word·excel
2 天前
Unity与Excel表格交互热更方案
unity·游戏引擎·excel
金融小白数据分析之路2 天前
Excel高级函数使用FILTER、UNIQUE、INDEX
excel
未来之窗软件服务2 天前
Excel表格批量下载 CyberWin Excel Doenlaoder 智能编程-——玄武芯辰
excel·批量下载·仙盟创梦ide·东方仙盟
阿斯加德的IT3 天前
Power Automate: 从Excel 选择列,每200条生成一个CSV文件并保存在sharepoint文档库
低代码·excel
步达硬件3 天前
【转bin】EXCEL数据转bin
excel
wtsolutions3 天前
JSON to Excel 3.0.0 版本发布 - 从Excel插件到Web应用的转变
json·excel·json-to-excel·wtsolutions