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

最新文章

  1. oracle数据库_实例_用户_表空间之间的关系(转)
  2. 《JAVA 从入门到精通》 - 正式走向JAVA项目开发的路
  3. 【JAVA】 @override报错的解决方法
  4. VC++ 学习笔记(序):神一样的语言
  5. Android菜单详解(四)——使用上下文菜单ContextMenu
  6. CSS对浏览器的兼容性常见处理方式小结
  7. win7 php 配置多个网站
  8. FZU 1919 -- K-way Merging sort(记忆化搜索)
  9. Hadoop2.x 体系结构和源码编译
  10. vue中click阻止事件冒泡,防止触发另一个事件
  11. Spring通知方法错误
  12. svn 的add 和 commit
  13. translate和position的比较
  14. kettle 启动spoon一闪而过
  15. 微软职位内部推荐-Senior SW Engineer for Application Ecosystem
  16. ASCII编码和Unicode编码的区别
  17. placement new
  18. 数据库 proc编程三
  19. gatsbyjs 使用
  20. TDS开启log TDS开启SSL

热门文章

  1. 一、100ASK_IMX6ULL嵌入式裸板学习_LED实验(上)
  2. docker 安装mongodb
  3. ClickHouse 使用
  4. U盘启动安装 Centos 出错记录(Reached target Basic System)
  5. H3C交换机基本操作
  6. 解决Google翻译不能用的问题
  7. Raid磁盘阵列更换磁盘时另一块盘离线恢复案例(v7000存储数据恢复)
  8. 请求浏览器重新加载数据/返回前端Json 数据
  9. android9.0之后wifi热点原生接口开发示例
  10. 在Mac的哪里可以找到bashrc文件