用数据库值填充powerpoint演示文稿
本文关键字:文稿 powerpoint 填充 数据库 | 更新日期: 2023-09-27 18:21:38
在我的应用程序中的某个地方,我需要创建一个powerpoint幻灯片,并用DB值填充它。我可以使用本地模板创建幻灯片。但我不知道如何用DB值填充它。我在网上搜索了一下,发现了这个,但它对我没有帮助。没有我想要填充的图表。它只是基于DB值的简单文本
有人能帮我吗?
下面的代码允许您创建一个新幻灯片,选择它并向表中添加值。您现在只需要读取数据库并填写即可。
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;
}
**编辑:
从数据库读取数据的代码源从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());
}
}
}
连接到数据库,示例可以在这里找到:
http://www.daniweb.com/software-development/csharp/threads/21636/how-do-you-connect-to-a-sql-database-using-c
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);
}