Java带图片的excel数据导入

Posted 焚目圣僧渡众生

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java带图片的excel数据导入相关的知识,希望对你有一定的参考价值。

带图片的EXCEL数据导入

这里使用的是POI,所以这里使用的很杂,不过方便对它们的了解。模板下载与图片导出到excel都不一样。
下面会把对应连接贴上。
带入依赖:不要使用3.17的版本,直接上代码:

		<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>

接口:

@PostMapping("/import")
    public CommonResult importRoadsAndBridgesData(@RequestParam("file") MultipartFile[] files)
        MultipartFile file = files[0];
        String fileName = file.getOriginalFilename();
        // 上传文件为空
        if (StringUtils.isEmpty(fileName)) 
            throw new ServiceException("没有导入文件");
        
        // 上传文件名格式不正确
        if (fileName.lastIndexOf(".") != -1 && !".xlsx".equals(fileName.substring(fileName.lastIndexOf(".")))) 
            throw new ServiceException( "文件名格式不正确, 请使用后缀名为.xlsx的文件");
        
        urbanManageDataService.importRoadsAndBridgesData(file);
        return CommonResult.success();
    
@Override
    public void importRoadsAndBridgesData(MultipartFile file) 
        ExcelUtil<DetailsOfRoadMaintainDto> util = new ExcelUtil<>(DetailsOfRoadMaintainDto.class);
        List<DetailsOfRoadMaintainDto> detailsOfRoadDtos = null;
        try 
        //文件,sheet名称,标题占用行数
            detailsOfRoadDtos = util.importExcel(file, "附表1维护情况明细表", 3);
         catch (Exception e) 
            log.error("导入数据解析流失败:", e.getMessage());
            throw new ServiceException("导入数据文件解析失败!");
        
        //封装实体数据和文件上传
        List<DetailsOfRoadMaintainEntity> list = new ArrayList<>();
        //道路数据
        Map<String, DetailsOfRoadMaintainDto> dtoMap = dataConversion(detailsOfRoadDtos, 1, list);
        //数据保存,替换成你自己的
        if(!CollectionUtils.isEmpty(list))
            detailsOfRoadMaintainMapper.insertList(list);
        
    

//下面都是图片上传和数据存取,就不过多贴上了,主要看excel图片的获取方式
private Map<String, DetailsOfRoadMaintainDto> dataConversion(List<DetailsOfRoadMaintainDto> dtos, Integer type, List<DetailsOfRoadMaintainEntity> list)
        Map<String, DetailsOfRoadMaintainDto> dtoMap = new HashMap<>();
        if(!CollectionUtils.isEmpty(dtos))
            dtos.forEach(dto ->
                DetailsOfRoadMaintainEntity entity = new DetailsOfRoadMaintainEntity();
                //基础数据转换
                BeanUtils.copyProperties(dto, entity);
                entity.setId(IdUtil.simpleUUID());
                entity.setMaintainType(type);
                //上传的图片信息
                entity.setBeforeimg(uploadFile(dto.getBeforeimg()));
                entity.setCreateTime(new Date());
                //存入entity
                list.add(entity);
            );
        
        return dtoMap;
    
//这里就是已经获取到图片后的正常上传而已,获取到的图片信息为PictureData,转为MultipartFile进行的文件上传
private String uploadFile(PictureData pictureData)
        if(null != pictureData && pictureData.getData().length > 0)
            InputStream inputStream = new ByteArrayInputStream(pictureData.getData());
            SysFile sysFile = null;
            try 
                MultipartFile multipartFile = new MockMultipartFile(getFileName(pictureData), null, ContentType.IMAGE_JPEG.toString(), inputStream);
                sysFile = attachmentService.getUploadFileInfo(multipartFile);
             catch (IOException e) 
                log.error("字节流转文件失败:", e.getMessage());
                throw new ServiceException("字节流转文件失败!");
            
            if(Objects.isNull(sysFile) || StringUtils.isEmpty(sysFile.getUrl()))
                throw new ServiceException("未获取到上传文件的信息!");
            
            return sysFile.getUrl();
        
        return null;
    

上面都是一些图片上传和下面要获取到后的信息进行存储,主要看图片怎么从excel获取的,下面先看参数映射:

@Data
public class DetailsOfRoadMaintainDto implements Serializable 
    /**
     * 序号
     */
    @Excel(name = "序号")
    private String no;

	@Excel(name = "维护前照片", getPicture = true)
    private PictureData beforeimg;
public interface ExcelHandlerAdapter

    /**
     * 格式化
     * 
     * @param value 单元格数据值
     * @param args excel注解args参数组
     *
     * @return 处理后的值
     */
    Object format(Object value, String[] args);

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Excels

    Excel[] value();

package com.goktech.common.core.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.math.BigDecimal;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;

/**
 * 自定义导出Excel数据注解
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Excel

    /**
     * 导出时在excel中排序
     */
    public int sort() default Integer.MAX_VALUE;

    /**
     * 导出到Excel中的名字.
     */
    public String name() default "";

    /**
     * 日期格式, 如: yyyy-MM-dd
     */
    public String dateFormat() default "";

    /**
     * 读取内容转表达式 (如: 0=男,1=女,2=未知)
     */
    public String readConverterExp() default "";

    /**
     * 读取图片 true=是,false=否)
     */
    public boolean getPicture() default false;

    /**
     * 分隔符,读取字符串组内容
     */
    public String separator() default ",";

    /**
     * BigDecimal 精度 默认:-1(默认不开启BigDecimal格式化)
     */
    public int scale() default -1;

    /**
     * BigDecimal 舍入规则 默认:BigDecimal.ROUND_HALF_EVEN
     */
    public int roundingMode() default BigDecimal.ROUND_HALF_EVEN;

    /**
     * 导出时在excel中每个列的高度 单位为字符
     */
    public double height() default 14;

    /**
     * 导出时在excel中每个列的宽 单位为字符
     */
    public double width() default 16;

    /**
     * 文字后缀,如% 90 变成90%
     */
    public String suffix() default "";

    /**
     * 当值为空时,字段的默认值
     */
    public String defaultValue() default "";

    /**
     * 提示信息
     */
    public String prompt() default "";

    /**
     * 设置只能选择不能输入的列内容.
     */
    public String[] combo() default ;

    /**
     * 是否导出数据,应对需求:有时我们需要导出一份模板,这是标题需要但内容需要用户手工填写.
     */
    public boolean isExport() default true;

    /**
     * 另一个类中的属性名称,支持多级获取,以小数点隔开
     */
    public String targetAttr() default "";

    /**
     * 是否自动统计数据,在最后追加一行统计数据总和
     */
    public boolean isStatistics() default false;

    /**
     * 导出类型(0数字 1字符串)
     */
    public ColumnType cellType() default ColumnType.STRING;

    /**
     * 导出字体颜色
     */
    public IndexedColors color() default IndexedColors.BLACK;

    /**
     * 导出字段对齐方式
     */
    public HorizontalAlignment align() default HorizontalAlignment.CENTER;

    /**
     * 自定义数据处理器
     */
    public Class<?> handler() default ExcelHandlerAdapter.class;

    /**
     * 自定义数据处理器参数
     */
    public String[] args() default ;

    /**
     * 字段类型(0:导出导入;1:仅导出;2:仅导入)
     */
    Type type() default Type.ALL;

    public enum Type
    
        ALL(0), EXPORT(1), IMPORT(2);
        private final int value;

        Type(int value)
        
            this.value = value;
        

        public int value()
        
            return this.value;
        
    

    public enum ColumnType
    
        NUMERIC(0), STRING(1), IMAGE(2);
        private final int value;

        ColumnType(int value)
        
            this.value = value;
        

        public int value()
        
            return this.value;
        
    

导入的工具类:

package com.goktech.common.core.utils.poi;

import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.rmi.ServerException;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.common.core.annotation.Excel;
import com.common.core.annotation.Excels;
import com.common.core.exception.ServiceException;
import com.common.core.text.Convert;
import com.common.core.utils.DateUtils;
import com.common.core.utils.StringUtils;
import com.common.core.utils.file.ImageUtils;
import com.common.core.utils.reflect.ReflectUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.RegExUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.goktech.common.core.utils.file.FileTypeUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * Excel相关处理
 *
 * @author ruoyi
 */
public class ExcelUtil<T>

    private static final Logger log = LoggerFactory.getLogger(ExcelUtil.class);

    public static final String FORMULA_REGEX_STR = "=|-|\\\\+|@";

    public static final String[] FORMULA_STR =  "=", "-", "+", "@" ;

    /**
     * Excel sheet最大行数,默认65536
     */
    public static final int SHEET_SIZE = 65536;

    /**
     * 工作表名称
     */
    private String sheetName;

    /**
     * 导出类型(EXPORT:导出数据;IMPORT:导入模板)
     */
    private Excel.Type type;

    /**
     * 工作薄对象
     */
    private Workbook wb;

    /**
     * 工作表对象
     */
    private Sheet sheet;

    /**
     * 样式列表
     */
    private Map<String, CellStyle> styles;

    /**
     * 导入导出数据列表
     */
    private List<T> list;

    /**
     * 注解列表
     */
    private List<Object[]> fields;

    /**
     * 当前行号
     */
    private int rownum;

    /**
     * 标题
     */
    private String title;

    /**
     * 最大高度
     */
    private short maxHeight;

    /**
     * 统计列表
     */
    
function excel_down(){
            //导入谁就去查谁
            $data=Db::name(‘order_xueyou‘)->select();

            // 导出Exl
            // import("./PHPExcel");
            // import("./PHPExcel/Worksheet/Drawing");
            // import("./PHPExcel/Writer/Excel2007");
            include("./PHPExcel/PHPExcel.php");
            include("./PHPExcel/Drawing.php");
    
            $objPHPExcel = new PHPExcel();
             
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
         
            $objActSheet = $objPHPExcel->getActiveSheet();
             
            // 水平居中(位置很重要,建议在最初始位置)
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘A‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘B‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘C‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘D‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘E‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘F‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘G‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘H‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘I‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘J‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘K‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘L‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘M‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘N‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘O‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘P‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘Q‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘R‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘S‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘T‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘U‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘V‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘W‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘X‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘Y‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
             
            $objActSheet->setCellValue(‘A1‘, ‘id‘);
            $objActSheet->setCellValue(‘B1‘, ‘openid‘);
            $objActSheet->setCellValue(‘C1‘, ‘nickName‘);
            $objActSheet->setCellValue(‘D1‘, ‘buyer‘);
            $objActSheet->setCellValue(‘E1‘, ‘ordertime‘);
            $objActSheet->setCellValue(‘F1‘, ‘orderdate‘);
            $objActSheet->setCellValue(‘G1‘, ‘address‘);
            $objActSheet->setCellValue(‘H1‘, ‘name‘);
            $objActSheet->setCellValue(‘I1‘, ‘goodid‘);
            $objActSheet->setCellValue(‘J1‘, ‘img‘);
            $objActSheet->setCellValue(‘K1‘, ‘oneprice‘);
            $objActSheet->setCellValue(‘L1‘, ‘num‘);
            $objActSheet->setCellValue(‘M1‘, ‘color‘);
            $objActSheet->setCellValue(‘N1‘, ‘sex‘);
            $objActSheet->setCellValue(‘O1‘, ‘size‘);
            $objActSheet->setCellValue(‘P1‘, ‘buynum‘);
            $objActSheet->setCellValue(‘Q1‘, ‘buy‘);
            $objActSheet->setCellValue(‘R1‘, ‘price‘);
            $objActSheet->setCellValue(‘S1‘, ‘status‘);
            $objActSheet->setCellValue(‘T1‘, ‘formkorea‘);
            $objActSheet->setCellValue(‘U1‘, ‘saddress‘);
            $objActSheet->setCellValue(‘V1‘, ‘remark‘);
            $objActSheet->setCellValue(‘W1‘, ‘formchina‘);
            $objActSheet->setCellValue(‘X1‘, ‘subremark‘);
            $objActSheet->setCellValue(‘Y1‘, ‘ddh‘);
            // 设置个表格宽度
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘A‘)->setWidth(16);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘B‘)->setWidth(15);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘C‘)->setWidth(15);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘D‘)->setWidth(20);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘E‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘F‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘G‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘H‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘I‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘J‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘K‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘L‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘M‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘N‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘O‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘P‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘Q‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘R‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘S‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘T‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘U‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘V‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘W‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘X‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘Y‘)->setWidth(12);
             
            // 垂直居中
             $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘A‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘B‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘C‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘D‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘E‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘F‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘G‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘H‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘I‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘J‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘K‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘L‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘M‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘N‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘O‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘P‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘Q‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘R‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘S‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘T‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘U‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘V‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘W‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘X‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle(‘Y‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
             
            foreach($data as $k=>$v){
                $k +=2;
                $objActSheet->setCellValue(‘A‘.$k, $v[‘id‘]);   
                $objActSheet->setCellValue(‘B‘.$k, $v[‘openid‘]);   
                $objActSheet->setCellValue(‘C‘.$k, $v[‘nickName‘]);   
                $objActSheet->setCellValue(‘D‘.$k, $v[‘buyer‘]);   
                $objActSheet->setCellValue(‘E‘.$k, $v[‘ordertime‘]);   
                $objActSheet->setCellValue(‘F‘.$k, $v[‘orderdate‘]);   
                $objActSheet->setCellValue(‘G‘.$k, $v[‘address‘]);   
                $objActSheet->setCellValue(‘H‘.$k, $v[‘name‘]);   
                $objActSheet->setCellValue(‘I‘.$k, $v[‘goodid‘]);   
                     
                 //获取到图片信息   
                $img = Db::name(‘order_xueyou‘)->where(‘id = ‘.$v[‘id‘])->field(‘img‘)->find();
                 //dump($img);
                // die();
                // 图片生成
                $objDrawing[$k] = new PHPExcel_Worksheet_Drawing();
                $objDrawing[$k]->setPath(‘.‘.$img[‘img‘]);//这里拼接 . 是因为要在根目录下获取
                // 设置宽度高度
                $objDrawing[$k]->setHeight(80);//照片高度
                $objDrawing[$k]->setWidth(80); //照片宽度
                /*设置图片要插入的单元格*/
                $objDrawing[$k]->setCoordinates(‘J‘.$k);
                // 图片偏移距离
                $objDrawing[$k]->setOffsetX(12);
                $objDrawing[$k]->setOffsetY(12);
                $objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());
                 
                // 表格内容
                $objActSheet->setCellValue(‘K‘.$k, $v[‘oneprice‘]);   
                $objActSheet->setCellValue(‘L‘.$k, $v[‘num‘]);   
                $objActSheet->setCellValue(‘M‘.$k, $v[‘color‘]);
                $objActSheet->setCellValue(‘N‘.$k, $v[‘sex‘]);
                $objActSheet->setCellValue(‘O‘.$k, $v[‘size‘]);
                $objActSheet->setCellValue(‘P‘.$k, $v[‘buynum‘]);
                $objActSheet->setCellValue(‘Q‘.$k, $v[‘buy‘]);
                $objActSheet->setCellValue(‘R‘.$k, $v[‘price‘]);
                $objActSheet->setCellValue(‘S‘.$k, $v[‘status‘]);
                $objActSheet->setCellValue(‘T‘.$k, $v[‘formkorea‘]);
                $objActSheet->setCellValue(‘U‘.$k, $v[‘saddress‘]);
                $objActSheet->setCellValue(‘V‘.$k, $v[‘remark‘]);
                $objActSheet->setCellValue(‘W‘.$k, $v[‘formchina‘]);
                $objActSheet->setCellValue(‘X‘.$k, $v[‘subremark‘]);
                $objActSheet->setCellValue(‘Y‘.$k, $v[‘ddh‘]);
                     
                // 表格高度
                $objActSheet->getRowDimension($k)->setRowHeight(80);
                 
            }
             
            $fileName = ‘报价表‘;
            $date = date("Y-m-d",time());
            $fileName .= "_{$date}.xls";
            $fileName = iconv("utf-8", "gb2312", $fileName);
            //重命名表
            // $objPHPExcel->getActiveSheet()->setTitle(‘test‘);
            //设置活动单指数到第一个表,所以Excel打开这是第一个表
            $objPHPExcel->setActiveSheetIndex(0);
            header(‘Content-Type: application/vnd.ms-excel‘);
            header("Content-Disposition: attachment;filename="$fileName"");
            header(‘Cache-Control: max-age=0‘);
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel5‘);
            $objWriter->save(‘php://output‘); //文件通过浏览器下载
            // END   
    }

 

以上是关于Java带图片的excel数据导入的主要内容,如果未能解决你的问题,请参考以下文章

java类 图片导入到excel 模糊 就是图片被盖上了一层红色

用Java向Excel中导入图片出现的问题

java excel中有从数据库查到的数据 下面需要在一个excel中插入图片应该怎么做

java中怎么让导入的excle表中每一条数据对应文件夹中对应的图片

jsp导入和导出excel的代码(jsp连接excel的方法)

java 批量导入图片到excel