我在c#中使用了一些办公自动化功能来更改powerpoint中的幻灯片。
由于一些窗口更新(还没有找到(,所包含excel中更改的图表数据将不会保存。
保存的powerpoint文件似乎很好,但当试图编辑图表数据时,会更改为旧值。
有人有同样的行为吗?
我的代码:
using System;
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 Microsoft.Office.Core;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Excel = Microsoft.Office.Interop.Excel;
namespace PowerpointAutomation
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "pptx|*.pptx";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("changing chart data");
string m_filename = openFileDialog1.FileName;
string fullPath = System.IO.Path.GetFullPath(m_filename);
string fileName = System.IO.Path.GetFileNameWithoutExtension(m_filename);
PowerPoint.Application objApp = new PowerPoint.Application();
PowerPoint.Presentations objPresSet = objApp.Presentations;
PowerPoint._Presentation objPres = objPresSet.Open(m_filename, MsoTriState./*msoTrue*/msoFalse, MsoTriState./*msoTrue*/msoFalse, MsoTriState.msoFalse);
PowerPoint.Slides objSlides = objPres.Slides;
foreach (PowerPoint._Slide objSlide in objSlides)
{
PowerPoint.Shapes objShapes = objSlide.Shapes;
foreach (PowerPoint.Shape objShape in objShapes)
{
switch (objShape.Type)
{
case MsoShapeType.msoChart:
if (objShape.HasChart == MsoTriState.msoTrue)
{
PowerPoint.Chart mychart = objShape.Chart;
PowerPoint.ChartData mydata = mychart.ChartData;
mydata.Activate();
((Excel.Workbook)mydata.Workbook).Application.Visible = false;
Excel.Workbook mywkb = mydata.Workbook as Excel.Workbook;
Excel._Worksheet mysheet = (Excel._Worksheet)mywkb.Worksheets[1];
Excel.Range usedRange = mysheet.UsedRange;
string address = usedRange.Address;
usedRange.Clear();
//
Excel::Range newCellsRange = mysheet.Cells;
int axisCategories = 10;
int serieCount = 1;
int i = 0;
for (i = 1; i <= axisCategories; i++)
{
newCellsRange.set_Item(1, i + 1, string.Format("cat{0}", i - 1));
}
for (i = 1; i <= serieCount; i++)
{
newCellsRange.set_Item(i + 1, 1, string.Format("serie{0}", i));
}
Random random = new Random(15);
for (int r = 1; r <= serieCount; r++)
{
for (int c = 1; c <= axisCategories; c++)
{
newCellsRange.set_Item(r + 1, c + 1, random.Next(10, 1000));
}
}
usedRange = mysheet.UsedRange;
string sAddress = "=Sheet1!";
sAddress += usedRange.Address;
mychart.SetSourceData(sAddress);
//just to make sure that is changed
string excelFileSaved = fullPath.Replace(fileName+".pptx", "changedexcel.xlsx");
mywkb.SaveCopyAs(@excelFileSaved);
mywkb.RefreshAll();
mychart.Refresh();
}
break;
default:
break;
}
}
}
string newFileName = fileName + "_NEW";
string newFullPath = fullPath.Replace(fileName, newFileName);
objPres.SaveAs(@newFullPath, PowerPoint.PpSaveAsFileType.ppSaveAsDefault, MsoTriState.msoTrue);
MessageBox.Show("saved");
objPres.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(objPres);
objPres = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(objApp);
objApp = null;
}
}
}
}
除了"mirco"之外,似乎没有人能使用Microsoft.Office.Interop.PowerPoint。问题已解决