Hi nabilabolo,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
Namespaces
using System.Linq;
using System.Reflection;
using ofc = Microsoft.Office.Core;
using ppt = Microsoft.Office.Interop.PowerPoint;
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Slide(int[] checkbox)
{
if (checkbox != null)
{
NorthwindEntities entity = new NorthwindEntities();
DataTable dtCustomers = ToDataTable(entity.Customers.Take(13).ToList());
List<DataTable> dts = SplitDataTableToMultiple(dtCustomers, checkbox[0]);
ppt.Application pptApplication = new ppt.Application();
ppt.Presentation pptPresentation = pptApplication.Presentations.Add(ofc.MsoTriState.msoFalse);
ppt._Slide slide;
for (int i = 0; i < dts.Count; i++)
{
slide = pptPresentation.Slides.Add(i + 1, ppt.PpSlideLayout.ppLayoutBlank);
ppt.Shape oShape;
DataTable dt = dts[i];
int iRows = dt.Rows.Count + 1;
int iColumns = dt.Columns.Count;
oShape = slide.Shapes.AddTable(iRows, iColumns);
// Adding header.
for (int iColumn = 0; iColumn < dt.Columns.Count; iColumn++)
{
oShape.Table.Cell(1, iColumn + 1).Shape.TextFrame.TextRange.Text = Convert.ToString(dt.Columns[iColumn].ColumnName);
oShape.Table.Cell(1, iColumn + 1).Shape.TextFrame.TextRange.Font.Name = "Verdana";
oShape.Table.Cell(1, iColumn + 1).Shape.TextFrame.TextRange.Font.Size = 8;
}
// Adding rows.
for (int iRow = 0; iRow < dt.Rows.Count; iRow++)
{
for (int iColumn = 0; iColumn < dt.Columns.Count; iColumn++)
{
oShape.Table.Cell(iRow + 2, iColumn + 1).Shape.TextFrame.TextRange.Text = Convert.ToString(dt.Rows[iRow][iColumn]);
oShape.Table.Cell(iRow + 2, iColumn + 1).Shape.TextFrame.TextRange.Font.Name = "Verdana";
oShape.Table.Cell(iRow + 2, iColumn + 1).Shape.TextFrame.TextRange.Font.Size = 8;
}
}
}
pptPresentation.SaveAs(Server.MapPath("~/test.ppt"));
pptPresentation.Close();
pptApplication.Quit();
}
return File(Server.MapPath("~/test.ppt"), "application/vnd.ms-powerpoint");
}
private static List<DataTable> SplitDataTableToMultiple(DataTable originalTable, int batchSize)
{
List<DataTable> dts = new List<DataTable>();
DataTable dt = new DataTable();
dt = originalTable.Clone();
int j = 0;
int k = 1;
if (originalTable.Rows.Count <= batchSize)
{
dt.TableName = "Table_" + k;
dt = originalTable.Copy();
dts.Add(dt.Copy());
}
else
{
for (int i = 0; i < originalTable.Rows.Count; i++)
{
dt.NewRow();
dt.ImportRow(originalTable.Rows[i]);
if ((i + 1) == originalTable.Rows.Count)
{
dt.TableName = "Table_" + k;
dts.Add(dt.Copy());
dt.Rows.Clear();
k++;
}
else if (++j == batchSize)
{
dt.TableName = "Table_" + k;
dts.Add(dt.Copy());
dt.Rows.Clear();
k++;
j = 0;
}
}
}
return dts;
}
public DataTable ToDataTable<T>(List<T> items)
{
DataTable dataTable = new DataTable(typeof(T).Name);
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
dataTable.Columns.Add(prop.Name);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
return dataTable;
}
}
View
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@using (Html.BeginForm("Slide", "Home", FormMethod.Post))
{
<input type="checkbox" name="checkbox" value="1" /><span>1</span><br />
<input type="checkbox" name="checkbox" value="5" /><span>5</span><br />
<input type="checkbox" name="checkbox" value="10" /><span>10</span><br />
<input type="submit" value="Slide" />
}
</div>
</body>
</html>