1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
using xlNS = Microsoft.Office.Interop.Excel;
using pptNS = Microsoft.Office.Interop.PowerPoint;
....
public class ExcelToPowerpoint {
pptNS.ApplicationClass powerpointApplication = null;
pptNS.Presentation pptPresentation = null;
pptNS.Slide pptSlide = null;
pptNS.ShapeRange shapeRange = null;
xlNS.ApplicationClass excelApplication = null;
xlNS.Workbook excelWorkBook = null;
xlNS.Worksheet targetSheet = null;
xlNS.ChartObjects chartObjects = null;
xlNS.ChartObject existingChartObject = null;
xlNS.Range targetRange = null;
object misValue = System.Reflection.Missing.Value;
string paramPresentationPath = "C:\\Path\\to\\your\\ppt";
string paramWorkbookPath = "C:\\Path\\to\\your\\xls";
object paramMissing = Type.Missing;
powerpointApplication = new pptNS.ApplicationClass();
// Create an instance Excel.
excelApplication = new xlNS.ApplicationClass();
// Open the Excel workbook containing the worksheet with the chart
// data.
excelWorkBook = excelApplication.Workbooks.Open(paramWorkbookPath,
paramMissing, paramMissing, paramMissing,
paramMissing, paramMissing, paramMissing,
paramMissing, paramMissing, paramMissing,
paramMissing, paramMissing, paramMissing,
paramMissing, paramMissing);
// Get the worksheet that contains the chart.
targetSheet =
(xlNS.Worksheet)(excelWorkBook.Worksheets["Graphique1"]);
// Modify a data on a cell
Form1 f = new Form1();
f.AddData(targetSheet, targetRange, 5, 2, "0.80", "B5", "B5", "0.0%");
// Get the ChartObjects collection for the sheet.
chartObjects =
(xlNS.ChartObjects)(targetSheet.ChartObjects(paramMissing));
// Get the chart to copy.
existingChartObject =
(xlNS.ChartObject)(chartObjects.Item("Chart 4"));
// Create a PowerPoint presentation.
pptPresentation = powerpointApplication.Presentations.Add(
Microsoft.Office.Core.MsoTriState.msoTrue);
// Add a blank slide to the presentation.
pptSlide =
pptPresentation.Slides.Add(1, pptNS.PpSlideLayout.ppLayoutBlank);
// Copy the chart from the Excel worksheet to the clipboard.
existingChartObject.Copy();
// Paste the chart into the PowerPoint presentation.
shapeRange = pptSlide.Shapes.Paste();
// Position the chart on the slide.
shapeRange.Left = 60;
shapeRange.Top = 100;
// Save the excel
//excelWorkBook.SaveAs(paramWorkbookPath, xlNS.XlFileFormat.xlWorkbookDefault, misValue, misValue, misValue, misValue, xlNS.XlSaveAsAccessMode.xlExclusive, xlNS.XlSaveConflictResolution.xlOtherSessionChanges, misValue, misValue, misValue, misValue);
excelWorkBook.Save();
excelWorkBook.Close(false, paramMissing, paramMissing);
System.Diagnostics.Debug.Write("je suis passe par la18\n");
// Save the presentation.
pptPresentation.SaveAs(paramPresentationPath,
pptNS.PpSaveAsFileType.ppSaveAsOpenXMLPresentation,
Microsoft.Office.Core.MsoTriState.msoTrue);
pptPresentation.Close();
// Method for modify a data on my excel File
public void AddData(xlNS.Worksheet targetSheet, xlNS.Range targetRange, int row, int col, string data, string cell1, string cell2, string format)
{
System.Diagnostics.Debug.Write("Get Buck in Here\n");
targetSheet.Cells[row, col] = data;
System.Diagnostics.Debug.Write("Get Buck in Here2\n");
targetRange = targetSheet.get_Range(cell1, cell2);
System.Diagnostics.Debug.Write("Get Buck in Here3\n");
targetRange.NumberFormat = format;
System.Diagnostics.Debug.Write("Get Buck in Here4\n");
}
} |
Partager