Tạo Bảng Bằng VSTO và Aspose.Slides cho .NET

Creating Tables

VSTO 2008 Example

Các bước sau sẽ thêm một bảng vào slide Microsoft PowerPoint bằng VSTO:

  1. Tạo một bản thuyết trình.
  2. Thêm một slide trống vào bản thuyết trình.
  3. Thêm một bảng 15 x 15 vào slide.
  4. Thêm văn bản vào mỗi ô của bảng với kích thước phông chữ 10.
  5. Lưu bản thuyết trình vào đĩa.
//Tạo một bản thuyết trình
PowerPoint.Presentation pres = Globals.ThisAddIn.Application
              .Presentations.Add(Microsoft.Office.Core.MsoTriState.msoFalse);
//Thêm một slide trống
PowerPoint.Slide sld = pres.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutBlank);

//Thêm một bảng 15 x 15
PowerPoint.Shape shp = sld.Shapes.AddTable(15, 15, 10, 10, pres.PageSetup.SlideWidth - 20, 300);
PowerPoint.Table tbl = shp.Table;
int i = -1;
int j = -1;

//Lặp qua tất cả các hàng
foreach (PowerPoint.Row row in tbl.Rows)
{
    i = i + 1;
    j = -1;

    //Lặp qua tất cả các ô trong hàng
    foreach (PowerPoint.Cell cell in row.Cells)
    {
        j = j + 1;
        //Lấy khung văn bản của mỗi ô
        PowerPoint.TextFrame tf = cell.Shape.TextFrame;
        //Thêm một số văn bản
        tf.TextRange.Text = "T" + i.ToString() + j.ToString();
        //Đặt kích thước phông chữ của văn bản là 10
        tf.TextRange.Paragraphs(0, tf.TextRange.Text.Length).Font.Size = 10;
    }
}

//Lưu bản thuyết trình vào đĩa
pres.SaveAs("d:\\tblVSTO.ppt",
      PowerPoint.PpSaveAsFileType.ppSaveAsPresentation,
      Microsoft.Office.Core.MsoTriState.msoFalse);

Aspose.Slides for .NET Example

Các bước sau sẽ thêm một bảng vào slide Microsoft PowerPoint bằng Aspose.Slides:

  1. Tạo một bản thuyết trình.
  2. Thêm một bảng 15 x 15 vào slide đầu tiên.
  3. Thêm văn bản vào mỗi ô của bảng với kích thước phông chữ 10.
  4. Ghi bản thuyết trình ra đĩa.
Presentation pres = new Presentation();

//Truy cập slide đầu tiên
ISlide sld = pres.Slides[0];

//Xác định các cột với độ rộng và các hàng với chiều cao
double[] dblCols = { 50, 50, 50 };
double[] dblRows = { 50, 30, 30, 30, 30 };

//Thêm một bảng
Aspose.Slides.ITable tbl = sld.Shapes.AddTable(50, 50, dblCols, dblRows);

//Đặt định dạng viền cho mỗi ô
foreach (IRow row in tbl.Rows)
{
	foreach (ICell cell in row)
	{

		//Lấy khung văn bản của mỗi ô
		ITextFrame tf = cell.TextFrame;
		//Thêm một số văn bản
		tf.Text = "T" + cell.FirstRowIndex.ToString() + cell.FirstColumnIndex.ToString();
		//Đặt kích thước phông chữ là 10
		tf.Paragraphs[0].Portions[0].PortionFormat.FontHeight = 10;
		tf.Paragraphs[0].ParagraphFormat.Bullet.Type = BulletType.None;
	}
}

//Ghi bản thuyết trình vào đĩa
pres.Save("C:\\data\\tblSLD.ppt", SaveFormat.Ppt);