poi 導出圖片及數據到excel

Maven

 <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
 </dependency>

java代碼

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelUtil {

    public void exportXSSFExcel() throws IOException {
        File file = new File("d:/test/rows.xlsx");
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream fileOutput = new FileOutputStream(file);
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet();
        XSSFRow row = sheet.createRow(10);
        File photoFile = new File("C:\\Users\\guang.wang\\Desktop\\PlantMI\\1.jpg");
        if (photoFile.exists()) {
            BufferedImage bufferedImage = ImageIO.read(photoFile);
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "jpg", byteArrayOut);
            byte[] data = byteArrayOut.toByteArray();
            XSSFDrawing drawingPatriarch = sheet.createDrawingPatriarch();
            XSSFClientAnchor anchor = new XSSFClientAnchor(480, 30, 700, 250, (short) 0, 1, (short) 10, 20);
            drawingPatriarch.createPicture(anchor, workbook.addPicture(data, XSSFWorkbook.PICTURE_TYPE_JPEG));
            sheet.setColumnWidth((short) 500, (short) 500);
            row.setHeight((short) 500);
        }
        XSSFCell cell;
        for (int i = 0; i <10 ; i++) {
            row = sheet.createRow(i+20);
            for (int j = 0; j < 10; j++) {
               cell= row.createCell(j);
               cell.setCellValue("行"+i+",列"+j);
            }
        }
        for (int i = 1; i < 10; i++) {
                sheet.autoSizeColumn(i);
        }
        CellRangeAddress region = new CellRangeAddress(21, 21, 0, 3);
        sheet.addMergedRegion(region);
        row=sheet.getRow(21);

        row.getCell(0).setCellValue("123123211321331");

        // 處理中文不能自動調整列寬的問題
        this.setXSSFSizeColumn(sheet, 10);
        fileOutput.flush();
        workbook.write(fileOutput);
        fileOutput.close();
    }
    // 自適應寬度(中文支持)
    private void setXSSFSizeColumn(XSSFSheet sheet, int size) {
        for (int columnNum = 0; columnNum < size; columnNum++) {
            int columnWidth = sheet.getColumnWidth(columnNum) / 256;
            for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
                XSSFRow currentRow;
                //當前行未被使用過
                if (sheet.getRow(rowNum) == null) {
                    currentRow = sheet.createRow(rowNum);
                } else {
                    currentRow = sheet.getRow(rowNum);
                }
                if (currentRow.getCell(columnNum) != null) {
                    XSSFCell currentCell = currentRow.getCell(columnNum);
                    if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
                        int length = currentCell.getStringCellValue().getBytes().length;
                        if (columnWidth < length) {
                            columnWidth = length;
                        }
                    }
                }
            }
            columnWidth = columnWidth * 256;
            sheet.setColumnWidth(columnNum, columnWidth >= 65280 ? 6000 : columnWidth);
        }
    }

    public void exportHSSFExcel() throws IOException {
        File file = new File("d:/test/rows.xlsx");
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream fileOutput = new FileOutputStream(file);
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        HSSFRow row = sheet.createRow(10);
        File photoFile = new File("C:\\Users\\guang.wang\\Desktop\\PlantMI\\1.jpg");
        if (photoFile.exists()) {
            BufferedImage bufferedImage = ImageIO.read(photoFile);
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "jpg", byteArrayOut);
            byte[] data = byteArrayOut.toByteArray();
            HSSFPatriarch drawingPatriarch = sheet.createDrawingPatriarch();
            HSSFClientAnchor anchor = new HSSFClientAnchor(480, 30, 700, 250, (short) 0, 1, (short) 10, 19);
            drawingPatriarch.createPicture(anchor, workbook.addPicture(data, HSSFWorkbook.PICTURE_TYPE_JPEG));
            sheet.setColumnWidth((short) 500, (short) 500);
            row.setHeight((short) 500);
        }
        HSSFCell cell;
        for (int i = 0; i <10 ; i++) {
            row = sheet.createRow(i+20);
            for (int j = 0; j < 10; j++) {
                cell= row.createCell(j);
                cell.setCellValue("行"+i+",列"+j);
            }
        }
        for (int i = 1; i < 10; i++) {
            sheet.autoSizeColumn(i);
        }
        CellRangeAddress region = new CellRangeAddress(21, 21, 0, 3);
        sheet.addMergedRegion(region);
        row=sheet.getRow(21);

        row.getCell(0).setCellValue("123123211321331");

        // 處理中文不能自動調整列寬的問題
        this.setHSSFSizeColumn(sheet, 10);
        fileOutput.flush();
        workbook.write(fileOutput);
        fileOutput.close();
    }
    // 自適應寬度(中文支持)
    private void setHSSFSizeColumn(HSSFSheet sheet, int size) {
        for (int columnNum = 0; columnNum < size; columnNum++) {
            int columnWidth = sheet.getColumnWidth(columnNum) / 256;
            for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
                HSSFRow currentRow;
                //當前行未被使用過
                if (sheet.getRow(rowNum) == null) {
                    currentRow = sheet.createRow(rowNum);
                } else {
                    currentRow = sheet.getRow(rowNum);
                }
                if (currentRow.getCell(columnNum) != null) {
                    HSSFCell currentCell = currentRow.getCell(columnNum);
                    if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
                        int length = currentCell.getStringCellValue().getBytes().length;
                        if (columnWidth < length) {
                            columnWidth = length;
                        }
                    }
                }
            }
            columnWidth = columnWidth * 256;
            sheet.setColumnWidth(columnNum, columnWidth >= 65280 ? 6000 : columnWidth);
        }
    }

    public static void main(String[] args) throws IOException {
        ExcelUtil excelUtil=new ExcelUtil();
        excelUtil.exportXSSFExcel();
//        excelUtil.exportHSSFExcel();
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章