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