在我的应用程序的某个地方,我需要创建一个 powerpoint 幻灯片并用 DB 值填充它。我可以使用本地模板创建幻灯片。但我不知道如何用 DB 值填充它。我在网上搜索并找到了这个 ,但它并没有帮助我。没有要填充的图表或图形。它只是基于 DB 值的简单文本。
有人可以在这里帮助我吗?
问问题
5175 次
1 回答
5
以下代码允许您创建新幻灯片、选择它并向表格添加值。您现在只需要阅读数据库并填写它。
int newaddslidecounter = 0; // checking how many new slides have to be inserted in order to split the table
int startmultiplicator=1; // used in order to return current table row that must be pasted in new table
int amountrowsshown =7; // amount of shown rows per table
int fontsize = 12;
// set position of new table/chart
int topsize = 100;
int leftsize = 100;
int widthsize = 150;
int heightsize = 150;
Boolean alreadycreated = false; // checks if a table has already been created
//Resizing all tables
//and insert new Excel table instead while importing data from Excel table
//used for visualization of data
foreach (PowerPoint.Slide slide in presentation.Slides)
{
slide.Select();
//loop through all shapes in slide
foreach (PowerPoint.Shape pptshape in slide.Shapes)
{
//Console.WriteLine("Shape type : " + pptshape.Type.ToString());
//check if current shape is a table
//in order to do the manipulation
if (pptshape.Type.ToString().Equals("msoTable"))
{
for (int i = 1; i <= pptshape.Table.Rows.Count; i++)
{
for (int j = 1; j <= pptshape.Table.Columns.Count; j++)
{
/// checks if the module of the current row and the amount of maximum
/// shown rows to be visible in table is the same
// Console.WriteLine("row= " + i + " col= " + j);
if ((i % amountrowsshown) == 0 && alreadycreated == false)
{
PowerPoint.Table objTable = null;
/// Add new slide if table requires more than one slide
if (newaddslidecounter == 0)
{
/// Add new table object in the slide
objTable = presentation.Slides[slide.SlideIndex].Shapes.AddTable(amountrowsshown, pptshape.Table.Columns.Count, topsize, leftsize, slide.Master.Width - widthsize, slide.Master.Height - heightsize).Table;
}
else
{
presentation.Slides.Add(slide.SlideIndex + newaddslidecounter, NetOffice.PowerPointApi.Enums.PpSlideLayout.ppLayoutClipArtAndVerticalText);
/// Add new table object in the slide
objTable = presentation.Slides[slide.SlideIndex + newaddslidecounter].Shapes.AddTable(amountrowsshown, pptshape.Table.Columns.Count, topsize, leftsize, slide.Master.Width - widthsize, slide.Master.Height - heightsize).Table;
}
int incrementer = 1;
/// Run through the created new table and paste all the previous table values
for(int k=startmultiplicator;k<=i;k++)
{
for(int l=1;l<=pptshape.Table.Columns.Count;l++)
{
objTable.Cell(incrementer,l).Shape.TextFrame.TextRange.Text=pptshape.Table.Cell(k,l).Shape.TextFrame.TextRange.Text;
objTable.Cell(incrementer, l).Shape.TextFrame.TextRange.Font.Size = fontsize;
}
incrementer++;
}
startmultiplicator += amountrowsshown;
alreadycreated = true;
newaddslidecounter++;
}
}
alreadycreated = false;
}
/// In case is still some rows left which haven't been considered
/// we copy those in a new table
//Console.WriteLine("Create new table before leaving");
// Console.WriteLine(startmultiplicator);
if (startmultiplicator <= pptshape.Table.Rows.Count)
{
PowerPoint.Table objTable = null;
if (startmultiplicator < amountrowsshown)
{
/// Add new table object in the slide
objTable = presentation.Slides[slide.SlideIndex].Shapes.AddTable(amountrowsshown, pptshape.Table.Columns.Count, topsize, leftsize, slide.Master.Width - widthsize, slide.Master.Height - heightsize).Table;
}
else
{
/// Don't add new slide for last rows
presentation.Slides.Add(slide.SlideIndex + newaddslidecounter, NetOffice.PowerPointApi.Enums.PpSlideLayout.ppLayoutClipArtAndVerticalText);
// Add new table object in the slide
objTable = presentation.Slides[slide.SlideIndex + newaddslidecounter].Shapes.AddTable(amountrowsshown, pptshape.Table.Columns.Count, topsize, leftsize, slide.Master.Width - widthsize, slide.Master.Height - heightsize).Table;
}
int incrementer = 1;
/// Run through the created new table and paste all the previous table values
for (int k = startmultiplicator; k <= pptshape.Table.Rows.Count; k++)
{
for (int l = 1; l <= pptshape.Table.Columns.Count; l++)
{
objTable.Cell(incrementer, l).Shape.TextFrame.TextRange.Text = pptshape.Table.Cell(k, l).Shape.TextFrame.TextRange.Text;
objTable.Cell(incrementer, l).Shape.TextFrame.TextRange.Font.Size = fontsize;
}
incrementer++;
}
objTable.Application.Top = topsize;
objTable.Application.Left = leftsize;
objTable.Application.Width = slide.Master.Width - widthsize;
objTable.Application.Height = slide.Master.Height - heightsize;
}
pptshape.Delete();
}
}
newaddslidecounter=0;
startmultiplicator=1;
}
**编辑:
用于从 DB 读取数据的代码源 在 C# 中从 SQL 数据库中读取值
using (SqlConnection connection = new SqlConnection(connectionString)
using (SqlCommand command = new SqlCommand("select * from Requests where Complete = 0", connection))
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(reader["Username"].ToString());
Console.WriteLine(reader["Item"].ToString());
Console.WriteLine(reader["Amount"].ToString());
Console.WriteLine(reader["Complete"].ToString());
}
}
}
连接到数据库,示例可以在这里找到:
try
{
SqlConnection thisConnection = new SqlConnection(@"Network Library=DBMSSOCN;Data Source=192.168.0.100,1433;database=Northwind;User id=Paladine;Password=;");
thisConnection.Open();
SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText = "SELECT CustomerID, CompanyName FROM Customers";
SqlDataReader thisReader = thisCommand.ExecuteReader();
while (thisReader.Read())
{
Console.WriteLine("\t{0}\t{1}", thisReader["CustomerID"], thisReader["CompanyName"]);
}
thisReader.Close();
thisConnection.Close();
}
catch (SqlException e)
{
Console.WriteLine(e.Message);
}
于 2013-07-08T07:06:25.427 回答