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);
}
}
}

用java读取Excel并依据模板图生成对应的图片的更多相关文章

  1. Java读取Excel文件的几种方法

    Java读取 Excel 文件的常用开源免费方法有以下几种: 1. JDBC-ODBC Excel Driver 2. jxl.jar 3. jcom.jar 4. poi.jar 简单介绍: 百度文 ...

  2. Java读取excel表格

    Java读取excel表格 一般都是用poi技术去读取excel表格的,但是这个技术又是什么呢 什么是Apache POI? Apache POI是一种流行的API,它允许程序员使用Java程序创建, ...

  3. java读取excel文件的代码

    如下内容段是关于java读取excel文件的内容,应该能对各朋友有所用途. package com.zsmj.utilit; import java.io.FileInputStream;import ...

  4. Java读取Excel数据

    Java读取Excel数据,解析文本并格式化输出 Java读取Excel数据,解析文本并格式化输出 Java读取Excel数据,解析文本并格式化输出 下图是excel文件的路径和文件名 下图是exce ...

  5. 关于解决java读取excel文件遇空行抛空指针的问题 !

    关于解决java读取excel文件遇空行抛空指针的问题 ! package exceRead; import java.io.File; import java.io.FileInputStream; ...

  6. 第四篇:java读取Excel简单模板

    场景:对于经常需要导入Excel模板或数据来解析后加以应用的,使用频率非常之高,做了一个比较稳定的版本,体现在这些地方工具:org.apache.poi使用前必须了解这些:1.要解析,那肯定先判断是不 ...

  7. java 读取Excel文件并数据持久化方法Demo

    import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util ...

  8. java读取excel文件数据

    package com.smp.server.Ctrl; import java.io.File;import java.io.FileInputStream;import java.io.FileN ...

  9. JAVA读取EXCEL文件异常Unable to recognize OLE stream

    异常: jxl.read.biff.BiffException: Unable to recognize OLE stream at jxl.read.biff.CompoundFile.<in ...

  10. java 读取excel 正常 xls

    package com.sun.test; import java.io.BufferedInputStream;import java.io.File;import java.io.FileInpu ...

随机推荐

  1. Spring框架1--IoC

    Spring框架之IoC Spring的后处理器 待补充~ BeanFactoryPostProcessor BeanPostProcessor Bean的生命周期 具体可见图解:点击这里 补充: A ...

  2. Linux常用命令-文件处理命令一

    命令格式:         命令 [-选项] [参数]           例如: ls -la /etc 说明:         选项--通常是功能         参数--通常是操作对象     ...

  3. Lombok Requires Annotation Processing

    当你打开一个项目启动的时候报这样的错误 这种错误 打开这个设置

  4. 关于flex

    flex 是 flex-grow.flex-shrink.flex-basis 的缩写. flex 的默认值是以上三个属性值的组合.假设以上三个属性同样取默认值,则 flex 的默认值是 0 1 au ...

  5. Linux程序设计

    2.6 shell的语法 2.6.1  变量 参数变量 实验:使用参数和环境变量 #!/bin/sh salutation="Hello" echo $salutation ech ...

  6. VMware VirtualCenter Servere服务不能启动

    故障现象现象:VMware VirtualCenter Servere服务不能启动 解决办法:进入%programdata%\VMware\VMwareVirtualCenter\Logs目录下面. ...

  7. OpenLayer——模拟运动轨迹

    模拟在人地图上移动,动态绘制行动轨迹的功能,附带一个跟随的气泡弹窗. <!DOCTYPE html> <html lang="en"> <head&g ...

  8. ls access.log.?.gz

    因为日志文件每天都会打包, 所以昨天的问题可能就在今天的access.log/error.log文件里找不到了.如何找出个位数的log文件呢? 这里就有两种不同的匹配符号, *匹配多个, ?匹配一个, ...

  9. got multiple values for keyword argument 'to_fields'

    django.db.models.fields.related.ForeignObject.__init__() got multiple values for keyword argument 't ...

  10. 浏览器中通过js获取用户语言环境方法

    用户语言环境存在navigator对象中,不同浏览器分别通过如下方法获取用户浏览器语言.操作系统语言.   IE6 IE7 IE8 Firefox Chrome Safari Opera naviga ...