質問するログイン新規登録
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Q&A

解決済

2回答

7671閲覧

Epplus 図形の描画について

f_tonakai

総合スコア15

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

0グッド

0クリップ

投稿2019/09/03 13:58

編集2019/09/05 14:02

0

0

Epplusを使って「図形の描画」と「列の非表示」を個別での実装は上手くいくのですが、
図形の描画をしている箇所と非表示する列の位置が重なる場合、
図形の幅や位置が変わってしまいます。
どうしたらいいでしょうか?

イメージ説明

「M列」~「Q列」を非表示にすると
イメージ説明

図形の右端(S列とT列)の位置を固定にして描画をしたいのですが、
図形のサイズが非表示に伴い変わってしまいます。

// 出力ファイルの準備(実行ファイルと同じフォルダに出力される) FileInfo newFile = new FileInfo("result.xlsx"); if (newFile.Exists) { newFile.Delete(); newFile = new FileInfo("result.xlsx"); } // Excelファイルの作成 using (ExcelPackage package = new ExcelPackage(newFile)) { // ワークシートを1枚追加 ExcelWorksheet sheet = package.Workbook.Worksheets.Add("シート名"); // セル幅 for (int i=1; i<200; i++) { sheet.Column(i).Width = 3; } ExcelShape shape = sheet.Drawings.AddShape("Description", eShapeStyle.RoundRect); shape.SetPosition(1, 0, 10, 0); shape.SetSize(200, 60); shape.Text = "あいうえおかきくけこさ"; shape.Fill.Style = eFillStyle.SolidFill; shape.Fill.Color = Color.White; shape.Fill.Transparancy = 20; shape.Border.Fill.Style = eFillStyle.SolidFill; shape.Border.LineStyle = eLineStyle.Solid; shape.Border.Width = 2; shape.Border.Fill.Color = Color.Black; shape.Border.LineCap = eLineCap.Round; shape.TextAnchoring = eTextAnchoringType.Top; shape.TextVertical = eTextVerticalType.Horizontal; shape.TextAlignment = eTextAlignment.Center; shape.TextAnchoringControl = false; // 列非表示 for (int i=13; i<18; i++) { sheet.Column(i).Hidden = true; } // 保存 package.Save(); }

自己解決したソース

using System; using System.Diagnostics; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Drawing; using OfficeOpenXml; using OfficeOpenXml.Drawing; using OfficeOpenXml; namespace EppluseTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void Button1_Click(object sender, EventArgs e) { string filePath1 = @"C:\Debug\SampleBook.xlsx"; string filePath2 = @"C:\Debug\SampleBook_a.xlsx"; File.Delete(filePath2); File.Copy(filePath1, filePath2); FileInfo excelFile = new FileInfo(filePath2); using (var inputFile = new ExcelPackage(excelFile)) { var sheet = inputFile.Workbook.Worksheets["work001"]; ExcelShape shape = sheet.Drawings.AddShape("test001", eShapeStyle.RoundRect); shape.SetPosition(0, 7, 37, 10); shape.SetSize(500, 30); shape.Text = "あいうえおかきくけこ"; shape.Fill.Style = eFillStyle.SolidFill; shape.Fill.Color = Color.White; shape.Fill.Transparancy = 20; shape.Border.Fill.Style = eFillStyle.SolidFill; shape.Border.LineStyle = eLineStyle.Solid; shape.Border.Width = 2; shape.Border.Fill.Color = Color.Black; shape.Border.LineCap = eLineCap.Round; shape.TextAnchoring = eTextAnchoringType.Center; shape.TextVertical = eTextVerticalType.Horizontal; shape.TextAlignment = eTextAlignment.Center; shape.TextAnchoringControl = false; sheet = inputFile.Workbook.Worksheets["work002"]; sheet.Column(45).Hidden = true; shape = sheet.Drawings.AddShape("test002", eShapeStyle.RoundRect); shape.SetPosition(0, 7, 36, 10); shape.SetSize(500, 30); shape.Text = "あいうえおかきくけこ"; shape.Fill.Style = eFillStyle.SolidFill; shape.Fill.Color = Color.White; shape.Fill.Transparancy = 20; shape.Border.Fill.Style = eFillStyle.SolidFill; shape.Border.LineStyle = eLineStyle.Solid; shape.Border.Width = 2; shape.Border.Fill.Color = Color.Red; shape.Border.LineCap = eLineCap.Round; shape.TextAnchoring = eTextAnchoringType.Center; shape.TextVertical = eTextVerticalType.Horizontal; shape.TextAlignment = eTextAlignment.Center; shape.TextAnchoringControl = false; sheet = inputFile.Workbook.Worksheets["work003"]; sheet.Column(45).Hidden = true; sheet.Column(44).Hidden = true; shape = sheet.Drawings.AddShape("test003", eShapeStyle.RoundRect); shape.SetPosition(0, 7, 35, 10); shape.SetSize(500, 30); shape.Text = "あいうえおかきくけこ"; shape.Fill.Style = eFillStyle.SolidFill; shape.Fill.Color = Color.White; shape.Fill.Transparancy = 20; shape.Border.Fill.Style = eFillStyle.SolidFill; shape.Border.LineStyle = eLineStyle.Solid; shape.Border.Width = 2; shape.Border.Fill.Color = Color.Green; shape.Border.LineCap = eLineCap.Round; shape.TextAnchoring = eTextAnchoringType.Center; shape.TextVertical = eTextVerticalType.Horizontal; shape.TextAlignment = eTextAlignment.Center; shape.TextAnchoringControl = false; sheet = inputFile.Workbook.Worksheets["work004"]; sheet.Column(45).Hidden = true; sheet.Column(44).Hidden = true; sheet.Column(43).Hidden = true; shape = sheet.Drawings.AddShape("test004", eShapeStyle.RoundRect); shape.SetPosition(0, 7, 34, 10); shape.SetSize(500, 30); shape.Text = "あいうえおかきくけこ"; shape.Fill.Style = eFillStyle.SolidFill; shape.Fill.Color = Color.White; shape.Fill.Transparancy = 20; shape.Border.Fill.Style = eFillStyle.SolidFill; shape.Border.LineStyle = eLineStyle.Solid; shape.Border.Width = 2; shape.Border.Fill.Color = Color.Magenta; shape.Border.LineCap = eLineCap.Round; shape.TextAnchoring = eTextAnchoringType.Center; shape.TextVertical = eTextVerticalType.Horizontal; shape.TextAlignment = eTextAlignment.Center; shape.TextAnchoringControl = false; inputFile.Save(); Process.Start(filePath2); Close(); } } } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

c#

1 shape.SetPosition(1, 0, 10, 0); 2 shape.SetSize(200, 60); 3

SetPositionが図形の左上基準なので、難しいですね。
列を隠す分だけSetPositionもずらすことぐらいになるのかなあ。

c#

1 //列非表示 2 var count = 0; 3 for (int i = 13; i < 18; i++) 4 { 5 sheet.Column(i).Hidden = true; 6 count++; 7 } 8 shape.SetPosition(1, 0, 10 - count, 0); 9

投稿2019/09/04 08:55

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

f_tonakai

2019/09/05 14:00

ありがとうございます。 上手くいきました。
guest

0

処理順番として列を非表示にしてから図形描画するのはダメなのですか?

投稿2019/09/04 00:08

YAmaGNZ

総合スコア10679

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.30%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問