2013-07-08 54 views
1

在我的应用程序中的某处,我需要创建幻灯片幻灯片并使用数据库值填充它。我可以使用本地模板创建幻灯片。但我不知道如何用DB值填充它。我搜索了网页,发现this,但它没有帮助我。没有图表或图表我想填充。这只是基于DB值的简单文本。
有人可以帮我吗?使用数据库值填充幻灯片演示文稿

+0

你给的链接可以帮助你创建一个表,你在找什么?您只需返回数据库值和每行,在Powerpoint中创建一个新表并填充它。 – mike27015

回答

5

下面的代码允许您创建一个新的幻灯片,选择它并将值添加到表中。你只需要现在阅读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; 
      } 

**编辑:

代码源从数据库中读取数据 Reading values from SQL database in C#

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); 
} 
+1

感谢分享代码,但有许多未定义的变量。 – Nitish

+0

我相应地更改了我的代码。你现在应该看到更多关于不同变量的信息。我之所以这么做是因为允许在PowerPoint演示文稿中创建多个表格,也就是说,如果您的数据库中的行数为100,那么您会将它们分成不同的表格,并且如果您想限制要显示的列数量。此外,我还将连接的信息添加到数据库并读取数据,并选择要使用的行并创建与列大小相关的表。 – mike27015

+0

您是否终于找到了我提交的代码的解决方案或完美的方法? – mike27015

相关问题