Epplusを使って「図形の描画」と「列の非表示」を個別での実装は上手くいくのですが、
図形の描画をしている箇所と非表示する列の位置が重なる場合、
図形の幅や位置が変わってしまいます。
どうしたらいいでしょうか?
図形の右端(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(); } } } }

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/09/05 14:00