ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

用java读取Excel并依据模板图生成对应的图片

2022-01-24 14:00:08  阅读:135  来源: 互联网

标签:java String Excel cell new import null 模板


package test;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
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.imageio.ImageIO;

public class Testpoi {

    public static void main(String[] args) {
        String a = "E:\\Desktop files\\工作文档\\pic\\photo.png";
        String b = "E:\\Desktop files\\工作文档\\test\\";
        String c = null;
        String d = null;
        String e = null;

        Workbook wb =null;
        Sheet sheet = null;
        Row row = null;
        List<Map<String,String>> list = null;
//        String cellData = null;
        String filePath = "E:\\Desktop files\\工作文档\\wps\\a.xlsx";
//        String columns[] = {"name","names","code"};
        wb = readExcel(filePath);
        if(wb != null){
            //用来存放表中数据
            list = new ArrayList<Map<String,String>>();
            //获取第一个sheet
            sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            row = sheet.getRow(0);
            //获取最大列数
            int colnum = row.getPhysicalNumberOfCells();
//            DecimalFormat df = new DecimalFormat("0.00");
//            String whatYouWant = df.format(1234567890.666);
            for (int i = 1; i<rownum; i++) {
//                Map<String,String> map = new LinkedHashMap<String,String>();
                row = sheet.getRow(i);
                c = (String) getCellFormatValue(row.getCell(0));
                d = (String) getCellFormatValue(row.getCell(1));
                e = (String) getCellFormatValue(row.getCell(2));
                DecimalFormat df = new DecimalFormat("0");
                String code = df.format(Double.parseDouble(e));
                ImgYin(c,d,code,a,b+code+".png");
//                if(row !=null){
//                    for (int j=0;j<colnum;j++){
//                        cellData = (String) getCellFormatValue(row.getCell(j));
//                        System.out.println(cellData);
////                        map.put(columns[j], cellData);
//                    }
//                }else{
//                    break;
//                }
//                list.add(map);
            }
        }
        //遍历解析出来的list
//        for (Map<String,String> map : list) {
//            for (Entry<String,String> entry : map.entrySet()) {
//
//                System.out.print(entry.getKey()+entry.getValue()+"...");
//            }
//            System.out.println();
//        }
//        ImgYin("1111111","2222222","33333333",a,b+"1.png");
    }
    //读取excel
    public static Workbook readExcel(String filePath){
        Workbook wb = null;
        if(filePath==null){
            return null;
        }
        String extString = filePath.substring(filePath.lastIndexOf("."));
        InputStream is = null;
        try {
            is = new FileInputStream(filePath);
            if(".xls".equals(extString)){
                return wb = new HSSFWorkbook(is);
            }else if(".xlsx".equals(extString)){
                return wb = new XSSFWorkbook(is);
            }else{
                return wb = null;
            }
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return wb;
    }
    public static Object getCellFormatValue(Cell cell){
        Object cellValue = null;
        if(cell!=null){
            //判断cell类型
            switch(cell.getCellType()){
            case Cell.CELL_TYPE_NUMERIC:{
                cellValue = String.valueOf(cell.getNumericCellValue());
                break;
            }
            case Cell.CELL_TYPE_FORMULA:{
                //判断cell是否为日期格式
                if(DateUtil.isCellDateFormatted(cell)){
                    //转换为日期格式YYYY-mm-dd
                    cellValue = cell.getDateCellValue();
                }else{
                    //数字
                    cellValue = String.valueOf(cell.getNumericCellValue());
                }
                break;
            }
            case Cell.CELL_TYPE_STRING:{
                cellValue = cell.getRichStringCellValue().getString();
                break;
            }
            default:
                cellValue = "";
            }
        }else{
            cellValue = "";
        }
        return cellValue;
    }
    public static void ImgYin(String s1, String s2,String s3, String ImgName,String outName){
        try{
            File file = new File(ImgName);
            Image src = ImageIO.read(file);
            int wideth=src.getWidth(null);
            int height=src.getHeight(null);
            BufferedImage image=new BufferedImage(wideth,height,BufferedImage.TYPE_INT_RGB);
            Graphics g=image.createGraphics();
            g.drawImage(src,0,0,wideth,height,null);
            //设置字体颜色
            g.setColor(Color.BLACK);
            //size字体大小
            g.setFont(new Font("宋体",Font.PLAIN,50));
            //wideth控制字体距离右侧边缘距离  height控制字体距离底部距离 1831   2569
            g.drawString(s1,wideth-1500,height-1250);
            g.drawString(s2,wideth-1450,height-970);
            g.drawString(s3,wideth-1150,height-970);
            g.dispose();
            FileOutputStream out=new FileOutputStream(outName);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            encoder.encode(image);
            out.close();
        }
        catch(Exception e){
            System.out.println(e);
        }
    }
}
View Code

 

标签:java,String,Excel,cell,new,import,null,模板
来源: https://www.cnblogs.com/ysfvf/p/15839114.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有