用POI导出Excel表格及对单元格简单操作

此篇博客介绍了如何使用e-iceblue/spire.xls.free库在Java中创建、编辑Excel表格,包括添加边框、单元格居中、设置字体、合并单元格、调整行高列宽、实现单元格自动换行等功能。通过实例展示了如何将评估数据填充到模板并个性化样式输出。
摘要由CSDN通过智能技术生成

功能有
1.添加边框线
2.单元格垂直居中显示
3.设置字体
4.合并单元格(newSheet.setRowHeight(row1,32);)
5.设置行高,列宽
6.单元格自动换行

导入依赖是

   <dependency>
       <groupId>e-iceblue</groupId>
     <artifactId>spire.xls.free</artifactId>
     <version>2.2.0</version>
   </dependency>
package com.rongyungov.socins.api.utils;

import com.rongyungov.socins.api.vo.EvaluationExportVo;
import com.rongyungov.socins.api.vo.ExportVo;
import com.spire.xls.*;

import java.util.List;

public class EvaluationExportExcel {
    public static String evaluationExport(String filePath, ExportVo exportVo){
        //创建Workbook对象
        Workbook wb = new Workbook();
        wb.loadFromFile(filePath+"个人总评表模板.xlsx");

        //声明Workbook变量
        Workbook newWb = new Workbook();
        newWb.setVersion(wb.getVersion());
        Worksheet newSheet = newWb.getWorksheets().get("sheet1");

        //复制
        Worksheet orderSheet = wb.getWorksheets().get("sheet1");
        newSheet.copyFrom(orderSheet);


        //设置单元格式
        CellStyle style = newWb.getStyles().addStyle("Data Style");
        style.getFont().setSize(14);
        style.getFont().setFontName("宋体");
        style.getFont().isBold(true);
        style.setVerticalAlignment(VerticalAlignType.Center);

        newSheet.setValue(1,1,exportVo.getTitle());
        newSheet.setValue(4,2,exportVo.getUserName());
        newSheet.setValue(4,4,exportVo.getLevel());
        newSheet.setValue(5,2,exportVo.getCorporateName());
        List<EvaluationExportVo> exportVoList = exportVo.getExportVoList();
        int row=7;
        for (EvaluationExportVo evaluationExportVo : exportVoList){
            newSheet.setValue(row,1,evaluationExportVo.getReviewType());
            newSheet.setValue(row,2,evaluationExportVo.getUnits());
            newSheet.setValue(row,3,evaluationExportVo.getEvaluationGradeTitle());
            newSheet.setValue(row,4,evaluationExportVo.getReason());
            newSheet.getCellRange(row,1).setStyle(style);
            row++;
        }
        CellRange cellRange = newSheet.getCellRange(7, 1, newSheet.getLastRow(), newSheet.getLastColumn());


        //边框线
        cellRange.getBorders().setLineStyle(LineStyleType.Thin);
        cellRange.getBorders().getByBordersLineType(BordersLineType.DiagonalDown).setLineStyle(LineStyleType.None);
        cellRange.getBorders().getByBordersLineType(BordersLineType.DiagonalUp).setLineStyle(LineStyleType.None);

        //让单元格水平居中
        cellRange.getCellStyle().setVerticalAlignment(VerticalAlignType.Center);
        cellRange.getCellStyle().setHorizontalAlignment(HorizontalAlignType.Center);
        //单元格自动换行
        cellRange.getCellStyle().setWrapText(true);
        //将列宽设为18
        for (int i = 7; i < row; i++) {
//            newSheet.setRowHeight(i,30);
            CellStyle cellStyle = newSheet.getCellRange(7,4, newSheet.getLastRow(), newSheet.getLastColumn()).getCellStyle();

            cellStyle.getExcelFont().setSize(12);
            cellStyle.getExcelFont().setFontName("宋体");
            cellStyle.setHorizontalAlignment(HorizontalAlignType.Center);
            cellStyle.setVerticalAlignment(VerticalAlignType.Center);
        }

        String file = filePath+exportVo.getTitle()+".xlsx";
        String path = "/excel/"+exportVo.getTitle()+".xlsx";
        newWb.saveToFile(file, ExcelVersion.Version2016);
        return path;
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值