forked from Excel-DNA/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ribbon: Add access to the Excel COM object model
- Loading branch information
Showing
5 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using ExcelDna.Integration; | ||
using Microsoft.Office.Interop.Excel; | ||
|
||
namespace Ribbon | ||
{ | ||
public class DataWriter | ||
{ | ||
public static void WriteData() | ||
{ | ||
Application xlApp = (Application)ExcelDnaUtil.Application; | ||
|
||
Workbook wb = xlApp.ActiveWorkbook; | ||
if (wb == null) | ||
return; | ||
|
||
Worksheet ws = wb.Worksheets.Add(Type: XlSheetType.xlWorksheet); | ||
ws.Range["A1"].Value = "Date"; | ||
ws.Range["B1"].Value = "Value"; | ||
|
||
Range headerRow = ws.Range["A1", "B1"]; | ||
headerRow.Font.Size = 12; | ||
headerRow.Font.Bold = true; | ||
|
||
// Generally it's faster to write an array to a range | ||
var values = new object[100, 2]; | ||
var startDate = new DateTime(2007, 1, 1); | ||
var rand = new Random(); | ||
for (int i = 0; i < 100; i++) | ||
{ | ||
values[i, 0] = startDate.AddDays(i); | ||
values[i, 1] = rand.NextDouble(); | ||
} | ||
|
||
ws.Range["A2"].Resize[100, 2].Value = values; | ||
ws.Columns["A:A"].EntireColumn.AutoFit(); | ||
|
||
// Add a chart | ||
Range dataRange= ws.Range["A1:B101"]; | ||
dataRange.Select(); | ||
ws.Shapes.AddChart(XlChartType.xlLineMarkers).Select(); | ||
xlApp.ActiveChart.SetSourceData(Source: dataRange); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters