Powerpoint chart data not saved(未保存PowerPoint图表数据)
本文介绍了未保存PowerPoint图表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在C#中使用了一些办公自动化功能来更改PowerPoint中的幻灯片。
由于某些Windows更新(尚未找到),因此不会保存所包含的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。 问题已解决
这篇关于未保存PowerPoint图表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:未保存PowerPoint图表数据
猜你喜欢
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01