JAVA利用POI解析Excel图片,并按照标签号分类导入文件夹

来源:互联网 发布:上海巨人网络 编辑:程序博客网 时间:2024/06/06 17:46

这里提供一个导出excel表中图片数据的小工具

用户需求:XX环保公司需要从XX网站上导出带有标签号的图片,并整理成文件夹的形式,根据之前的估算人工操作需要3个月的时间,并且这可是个非人类的工作,一天几个小时重复的进行新建文件夹,CTRL-C,CTRL-V的工作,这是会要命的,本着人道主义救死扶伤的精神,特意写了个小程序来进行救赎

import org.apache.poi.POIXMLDocumentPart;import org.apache.poi.xssf.usermodel.*;import javax.swing.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.*;/** * Created by SunMing on 2016/9/4. */public class Test {    private JPanel mainPanel;    private JButton but;    private JTextField textFieldExcelPath;    private JTextField textFieldFilePath;    public static void main(String[] args){        JFrame frame = new JFrame("ExcelPictureOutPutTool");        frame.setContentPane(new Test().mainPanel);        frame.setDefaultCloseOperation(3);        frame.pack();        frame.setSize(500,200);        frame.setVisible(true);    }    /**         * Created by SunMing on 2016/9/4.         * 获取一个表内所有的图片,并根据标签号进行图片的分类存储         */    public static void getSheetPictrues07(XSSFSheet sheet, XSSFWorkbook workbook, String Path) throws IOException {        for (POIXMLDocumentPart dr : sheet.getRelations()) {            if (dr instanceof XSSFDrawing) {                XSSFDrawing drawing = (XSSFDrawing) dr;                java.util.List<XSSFShape> shapes = drawing.getShapes();                for (XSSFShape shape : shapes) {                    XSSFPicture pic = (XSSFPicture) shape;                    XSSFClientAnchor anchor = pic.getPreferredSize();                    org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker ctMarker = anchor.getFrom();                    String fileName = sheet.getRow(ctMarker.getRow()-2).getCell(3).toString().substring(5);                    String pictureName = "unhandle.png";                    String savePic = Path + fileName;                    File file = new File(savePic);                    if(!file.exists()){                        file.mkdir();                        pictureName = "handle.png";                    }                    String savePath = savePic + "\\" + pictureName;                    FileOutputStream fos = new FileOutputStream(savePath);                    XSSFPictureData data = pic.getPictureData();                    fos.write(data.getData());                }            }        }    }    public Test(){        but.addActionListener(new ActionListener() {            @Override            public void actionPerformed(ActionEvent e) {                String ExcelPath = textFieldExcelPath.getText();                String FilePath = textFieldFilePath.getText();                File file = new File(ExcelPath);                try {                    FileInputStream fis = new FileInputStream(file);                    XSSFWorkbook workbook = new XSSFWorkbook(fis);                    XSSFSheet sheet = workbook.getSheetAt(0);                    File fileRoot = new File(FilePath);                    if(!fileRoot.exists()){                        fileRoot.mkdir();                    }                    getSheetPictrues07(sheet, workbook,FilePath);                } catch (FileNotFoundException e1) {                    e1.printStackTrace();                } catch (IOException e1) {                    e1.printStackTrace();                }            }        });    }}
1 0