LoginSignup
higuma4466
@higuma4466

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

ExcelをPowerPointに転記

解決したいこと

Excelの内容をPowerPointに転記したいです。
コピー&ペーストではなく、RPA等を使って自動化したいと考えています。

ざっくりとした質問で申し訳ございませんが、上記に似たことをした人もご経験を教えていただけますと幸いです。
よろしくお願いいたします。

0

2Answer

RPA: Robotic Process Automationの略で「ロボットによる業務の自動化」を表してるので、選択した RPA で、コピペを実行させる、ってのが RPA を使った自動化だと思いますよ。

PowerPoint の自動生成 で検索した結果を参考にしてみては?

1

Comments

  1. @higuma4466

    Questioner
    ありがとうございます!

最も効果的な方法は、まずExcelワークシートを画像に変換することです。
Java

import com.spire.xls.*;

public class ExcelToImage {
    public static void main(String[] args){

        // Workbookインスタンスを作成する
        Workbook workbook = new Workbook();
        // Excelサンプルドキュメントをロードする
        workbook.loadFromFile("sample.xlsx");

        //最初のワークシートを取得する
        Worksheet sheet = workbook.getWorksheets().get(0);

        //ワークシートをイメージに変換する
        sheet.saveToImage("output/SheetToImage.png");
    }
}

Excelを画像に変換する
その後、画像をエクセルに挿入します。
Java

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.geom.Rectangle2D;

public class AddImageToSlide {
    public static void main(String []args) throws Exception {
        //Presentationクラスのインスタンスを初期化する
        Presentation presentation = new Presentation();
        //PowerPointドキュメントをロードする
        presentation.loadFromFile("Input.pptx");

        //最初のスライドを取得する
        ISlide slide = presentation.getSlides().get(0);

        //スライドに画像を追加する
        String imageFile = "image.png";
        Rectangle2D.Double rect1 = new Rectangle2D.Double(presentation.getSlideSize().getSize().getWidth() / 2 - 280, 140, 120, 120);
        IEmbedImage image = slide.getShapes().appendEmbedImage(ShapeType.RECTANGLE, imageFile, rect1);
        image.getLine().setFillType(FillFormatType.NONE);

        //結果ドキュメントを保存する
        presentation.saveToFile("AddImageToSlide.pptx", FileFormat.PPTX_2013);
    }
}

PowerPointに画像を挿入する
まずExcelワークシートのデータをエクスポートすることもできます。
Java

import com.spire.data.table.DataTable;
import com.spire.xls.*;

public class dataExport {
    public static void main(String[] args) {
        //XLSXドキュメントを開く
        Workbook workbook = new Workbook();
        workbook.loadFromFile("data/DataExport.xlsx");
        //1つ目のワークシートを取得する
        Worksheet sheet = workbook.getWorksheets().get(0);
        //dataTableへの書き出し
        DataTable dataTable= sheet.exportDataTable();
        int rows = dataTable.getRows().size();
        int columns = dataTable.getColumns().size();
        for (int i=0; i<rows;i++)
        {
            for (int j=0; j<columns;j++)
            {
                //出力する
                System.out.println(dataTable.getRows().get(i).getString(j));
            }
        }
    }
}

その後、データを使ってスライドに表を作成します。
Java

import com.spire.presentation.*;

public class AddTable {
    public static void main(String[] args) throws Exception {
        //Presentationのインスタンスを作成する
        Presentation presentation = new Presentation();

        Double[] widths = new Double[]{100d, 100d, 150d, 100d, 100d};
        Double[] heights = new Double[]{15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d};

        //テーブルを追加する 
        ITable table = presentation.getSlides().get(0).getShapes().appendTable((float) presentation.getSlideSize().getSize().getWidth() / 2 - 275, 90, widths, heights);
        String[][] dataStr = new String[][]
                {
                        {"Name", "Capital", "Continent", "Area", "Population"},
                        {"Venezuela", "Caracas", "South America", "912047", "19700000"},
                        {"Bolivia", "La Paz", "South America", "1098575", "7300000"},
                        {"Brazil", "Brasilia", "South America", "8511196", "150400000"},
                        {"Canada", "Ottawa", "North America", "9976147", "26500000"},
                        {"Chile", "Santiago", "South America", "756943", "13200000"},
                        {"Colombia", "Bagota", "South America", "1138907", "33000000"},
                        {"Cuba", "Havana", "North America", "114524", "10600000"},
                        {"Ecuador", "Quito", "South America", "455502", "10600000"},
                        {"Paraguay", "Asuncion", "South America", "406576", "4660000"},
                        {"Peru", "Lima", "South America", "1285215", "21600000"},
                        {"Jamaica", "Kingston", "North America", "11424", "2500000"},
                        {"Mexico", "Mexico City", "North America", "1967180", "88600000"}
                };
        //テーブルにデータを追加する
        for (int i = 0; i < 13; i++) {
            for (int j = 0; j < 5; j++) {
                //テーブルをデータで埋める
                table.get(j, i).getTextFrame().setText(dataStr[i][j]);

                //フォントを設定する
                table.get(j, i).getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setLatinFont(new TextFont("Arial Narrow"));
            }
        }
        //テーブルの1行目のアライメントを「中央」に設定する
        for (int i = 0; i < 5; i++) {
            table.get(i, 0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.CENTER);
        }
        //テーブルのスタイルを設定する
        table.setStylePreset(TableStylePreset.LIGHT_STYLE_3_ACCENT_1);

        //ドキュメントを保存する
        presentation.saveToFile("AddTable.pptx", FileFormat.PPTX_2013);
    }
}

PowerPointで表を作成する
これをやると、表の書式がおかしくなることがあります。

上記の方法では、 Spire.XLS for JavaSpire.Presentation for Java を使用する必要があります。

上記の方法とコードを使って、自動化プログラムを作成することができます。

1

Comments

  1. @higuma4466

    Questioner
    大変参考になりました!
    ありがとうございます!

Your answer might help someone💌