![](/img/trans.png)
[英]C# copy and paste datagridview to Powerpoint slide with formatting
[英]C# WindowsForm Add DatagridView Content into PowerPoint's slide
我正在使用 .NET 4.7.2、Windowsform。
我有一個 datagridview,我設法生成了一個 powerpoint 文件 pptx。 我制作了第一張 ppt 幻燈片,我想將 datagridview 內容添加到第二張 ppt 幻燈片中,因為我需要選擇更改 PPt 幻燈片中的數據。
Microsoft.Office.Interop.PowerPoint.Application pptApp = new Microsoft.Office.Interop.PowerPoint.Application();
pptApp.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
Microsoft.Office.Interop.PowerPoint.Slides slides;
Microsoft.Office.Interop.PowerPoint._Slide slide;
Microsoft.Office.Interop.PowerPoint._Slide slide2;
Microsoft.Office.Interop.PowerPoint.TextRange objText;
// Create File
Presentation pptPresentation = pptApp.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoTrue);
CustomLayout customLayout = pptPresentation.SlideMaster.CustomLayouts[PpSlideLayout.ppLayoutText];
// new Slide
slides = pptPresentation.Slides;
slide = slides.AddSlide(1, customLayout);
slide2 = slides.AddSlide(1, customLayout);
// title
objText = slide.Shapes[1].TextFrame.TextRange;
objText.Text = "Bonds Screner Report";
objText.Font.Name = "Haboro Contrast Ext Light";
objText.Font.Size = 32;
Shape shape1 = slide.Shapes[2];
slide.Shapes.AddPicture("C:\\mylogo.png", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, shape1.Left, shape1.Top, shape1.Width, shape1.Height);
slide.NotesPage.Shapes[2].TextFrame.TextRange.Text = "Disclaimer";
dataGridViewBonds.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
dataGridViewBonds.SelectAll();
DataObject obj = dataGridViewBonds.GetClipboardContent();
Clipboard.SetDataObject(obj, true);
Shape shapegrid = slide2.Shapes[2];
我知道我現在還不是很遠,但我想念一些東西。 任何幫助,將不勝感激 !
我熟悉 Excel 互操作,並多次使用它,很可能對互操作的笨拙方式感到麻木。 出於多種原因,使用 PowerPoint 互操作可能會非常令人沮喪,但是,我覺得最大的問題是缺乏文檔以及不同 MS 版本之間的差異。
此外,我尋找了第三方 PowerPoint 庫,“Aspose”看起來是唯一的選擇,不幸的是它不是“免費”選項。 我會假設有一個免費的第三方選項,但我只是沒有找對地方……或者可能有一種完全不同的方式來使用 XML 來做到這一點。 我確信我正在向合唱團布道。
因此,我能夠整理的內容可能對您有用。 首先,查看您當前發布的代碼,您需要將“復制”的網格單元格放入幻燈片中缺少一個部分……
slide.Shapes.Paste();
這會將網格中“復制”的單元格粘貼到幻燈片中的“未格式化”表格中。 如果網格AllowUserToAddRows
設置為true
,則除了“新行”之外,如果它顯示在網格中,這將復制“行標題”。 如果這種“未格式化的粘貼”適合您,那么您就可以開始了。
如果你更喜歡至少有一個最小格式的表格,而忽略行標題和最后一個空行......在幻燈片中簡單地“創建”一個我們想要的大小以及正確數量的行和列的新Table
可能更容易. 當然,這可能需要更多的工作,但是,無論如何“如果”您希望表格格式化,使用粘貼將需要這樣做。
該方法(如下)需要一個電源點_Slide
和一個DataGridView
。 代碼根據給定網格中的行數和列數在幻燈片中“創建”一個新Table
。 通過這種方法,表格將使用演示文稿中的默認“表格樣式”進行“格式化”。 因此,這可以通過簡單地“創建”表格而不是“粘貼”表格來為您提供所需的格式。
我曾嘗試在 powerpoint 中“應用”現有的“表格樣式”之一,但是,我看到的示例使用了類似……
table.ApplyStyle("{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}");
它使用 GUID id 來標識要使用的“哪種”樣式。 我不知道為什么 MS 決定采用這種 GUID 方法……這超出了我的范圍,它適用於“某些”樣式但不是全部。
此外,更常識性的解決方案顯示出類似......
table.StylePreset = TableStylePreset.MediumStyle2Accent2;
不幸的是,使用我的 2019 版 Office PowerPoint,此屬性不存在。 我已經放棄了對此的進一步研究,因為它似乎與版本有關。 很煩人!
鑒於此,如果我們根據需要單獨格式化單元格可能會更容易。 無論如何,我們都需要將網格中的單元格文本添加到單個單元格中,因此我們還可以同時格式化單個單元格。 同樣,我相信有更好的方法,但是,我找不到。
在InsertTableIntoSlide(_Slide slide, DataGridView dgv)
方法下方InsertTableIntoSlide(_Slide slide, DataGridView dgv)
將幻燈片和網格作為參數。 它將使用給定網格中的數據向幻燈片添加一個表格。 下面是一個簡短的代碼跟蹤。
首先進行檢查以獲取網格中的總行數(不包括標題) totRows
。 如果網格AllowUsersToAddRows
為true
,則總行數變量減 1 以忽略此新行。 接下來,網格中的列數設置為變量totCols
。 左上角的 X 和 Y 點被定義為topLeftX
和topLeftY
以在幻燈片中定位表格以及表格的width
和height
。
添加注意:使用AllowUserToAddRows
屬性來確定行數……可能無法按上述方式工作,並且會“錯過”最后一行……“如果” AllowUserToAddRows
為true
(默認)並且網格是綁定到數據源的數據不允許添加新行。 在這種情況下,您不想減少totRows
變量。
接下來,使用前面的變量將“表格”“形狀”添加到幻燈片中,以定義基本表格尺寸。 接下來是兩個循環。 第一個循環將標題單元格添加到表中的第一行。 然后第二個循環將來自網格中的單元格的數據添加到幻燈片中的表格單元格中。
注釋掉的代碼僅作為示例,以便您對單個單元格進行一些特定的格式化。 這在我的情況下是不需要的,因為“默認”表格樣式接近我想要的格式。
另外,請注意“ForeColor”是單元格/形狀的“背景”顏色。 奇怪的!
我希望這會有所幫助,並再次對不得不使用 PowerPoint 互操作性表示更多的同情......我不能。
private void InsertTableIntoSlide(_Slide slide, DataGridView dgv) {
try {
int totRows;
if (dgv.AllowUserToAddRows) {
totRows = dgv.Rows.Count - 1;
}
else {
totRows = dgv.Rows.Count;
}
int totCols = dgv.Columns.Count;
int topLeftX = 10;
int topLeftY = 10;
int width = 400;
int height = 100;
// add extra row for header row
Shape shape = slide.Shapes.AddTable(totRows + 1, totCols, topLeftX, topLeftY, width, height);
Table table = shape.Table;
for (int i = 0; i < dgv.Columns.Count; i++) {
table.Cell(1, i+1).Shape.TextFrame.TextRange.Text = dgv.Columns[i].HeaderText;
//table.Cell(1, i+1).Shape.Fill.ForeColor.RGB = ColorTranslator.ToOle(Color.Blue);
//table.Cell(1, i+1).Shape.TextFrame.TextRange.Font.Bold = Microsoft.Office.Core.MsoTriState.msoTrue;
//table.Cell(1, i+1).Shape.TextFrame.TextRange.Font.Color.RGB = ColorTranslator.ToOle(Color.White);
}
int curRow = 2;
for (int i = 0; i < totRows; i++) {
for (int j = 0; j < totCols; j++) {
if (dgv.Rows[i].Cells[j].Value != null) {
table.Cell(curRow, j + 1).Shape.TextFrame.TextRange.Text = dgv.Rows[i].Cells[j].Value.ToString();
//table.Cell(curRow, j + 1).Shape.Fill.ForeColor.RGB = ColorTranslator.ToOle(Color.LightGreen);
//table.Cell(curRow, j + 1).Shape.TextFrame.TextRange.Font.Bold = Microsoft.Office.Core.MsoTriState.msoTrue;
//table.Cell(curRow, j + 1).Shape.TextFrame.TextRange.Font.Color.RGB = ColorTranslator.ToOle(Color.Black);
}
}
curRow++;
}
}
catch (Exception ex) {
MessageBox.Show("Error: " + ex.Message);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.