├── .gitattributes
├── .gitignore
├── MSExcelAutomation.sln
├── MSExcelAutomation
├── App.config
├── MSExcelAutomation.csproj
├── MSExcelAutomationWnd.Designer.cs
├── MSExcelAutomationWnd.cs
├── MSExcelAutomationWnd.resx
├── Program.cs
└── Properties
│ ├── AssemblyInfo.cs
│ ├── Resources.Designer.cs
│ ├── Resources.resx
│ ├── Settings.Designer.cs
│ └── Settings.settings
└── README.md
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.userosscache
8 | *.sln.docstates
9 |
10 | # User-specific files (MonoDevelop/Xamarin Studio)
11 | *.userprefs
12 |
13 | # Build results
14 | [Dd]ebug/
15 | [Dd]ebugPublic/
16 | [Rr]elease/
17 | [Rr]eleases/
18 | x64/
19 | x86/
20 | bld/
21 | [Bb]in/
22 | [Oo]bj/
23 | [Ll]og/
24 |
25 | # Visual Studio 2015 cache/options directory
26 | .vs/
27 | # Uncomment if you have tasks that create the project's static files in wwwroot
28 | #wwwroot/
29 |
30 | # MSTest test Results
31 | [Tt]est[Rr]esult*/
32 | [Bb]uild[Ll]og.*
33 |
34 | # NUNIT
35 | *.VisualState.xml
36 | TestResult.xml
37 |
38 | # Build Results of an ATL Project
39 | [Dd]ebugPS/
40 | [Rr]eleasePS/
41 | dlldata.c
42 |
43 | # DNX
44 | project.lock.json
45 | artifacts/
46 |
47 | *_i.c
48 | *_p.c
49 | *_i.h
50 | *.ilk
51 | *.meta
52 | *.obj
53 | *.pch
54 | *.pdb
55 | *.pgc
56 | *.pgd
57 | *.rsp
58 | *.sbr
59 | *.tlb
60 | *.tli
61 | *.tlh
62 | *.tmp
63 | *.tmp_proj
64 | *.log
65 | *.vspscc
66 | *.vssscc
67 | .builds
68 | *.pidb
69 | *.svclog
70 | *.scc
71 |
72 | # Chutzpah Test files
73 | _Chutzpah*
74 |
75 | # Visual C++ cache files
76 | ipch/
77 | *.aps
78 | *.ncb
79 | *.opendb
80 | *.opensdf
81 | *.sdf
82 | *.cachefile
83 | *.VC.db
84 | *.VC.VC.opendb
85 |
86 | # Visual Studio profiler
87 | *.psess
88 | *.vsp
89 | *.vspx
90 | *.sap
91 |
92 | # TFS 2012 Local Workspace
93 | $tf/
94 |
95 | # Guidance Automation Toolkit
96 | *.gpState
97 |
98 | # ReSharper is a .NET coding add-in
99 | _ReSharper*/
100 | *.[Rr]e[Ss]harper
101 | *.DotSettings.user
102 |
103 | # JustCode is a .NET coding add-in
104 | .JustCode
105 |
106 | # TeamCity is a build add-in
107 | _TeamCity*
108 |
109 | # DotCover is a Code Coverage Tool
110 | *.dotCover
111 |
112 | # NCrunch
113 | _NCrunch_*
114 | .*crunch*.local.xml
115 | nCrunchTemp_*
116 |
117 | # MightyMoose
118 | *.mm.*
119 | AutoTest.Net/
120 |
121 | # Web workbench (sass)
122 | .sass-cache/
123 |
124 | # Installshield output folder
125 | [Ee]xpress/
126 |
127 | # DocProject is a documentation generator add-in
128 | DocProject/buildhelp/
129 | DocProject/Help/*.HxT
130 | DocProject/Help/*.HxC
131 | DocProject/Help/*.hhc
132 | DocProject/Help/*.hhk
133 | DocProject/Help/*.hhp
134 | DocProject/Help/Html2
135 | DocProject/Help/html
136 |
137 | # Click-Once directory
138 | publish/
139 |
140 | # Publish Web Output
141 | *.[Pp]ublish.xml
142 | *.azurePubxml
143 | # TODO: Comment the next line if you want to checkin your web deploy settings
144 | # but database connection strings (with potential passwords) will be unencrypted
145 | *.pubxml
146 | *.publishproj
147 |
148 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
149 | # checkin your Azure Web App publish settings, but sensitive information contained
150 | # in these scripts will be unencrypted
151 | PublishScripts/
152 |
153 | # NuGet Packages
154 | *.nupkg
155 | # The packages folder can be ignored because of Package Restore
156 | **/packages/*
157 | # except build/, which is used as an MSBuild target.
158 | !**/packages/build/
159 | # Uncomment if necessary however generally it will be regenerated when needed
160 | #!**/packages/repositories.config
161 | # NuGet v3's project.json files produces more ignoreable files
162 | *.nuget.props
163 | *.nuget.targets
164 |
165 | # Microsoft Azure Build Output
166 | csx/
167 | *.build.csdef
168 |
169 | # Microsoft Azure Emulator
170 | ecf/
171 | rcf/
172 |
173 | # Windows Store app package directories and files
174 | AppPackages/
175 | BundleArtifacts/
176 | Package.StoreAssociation.xml
177 | _pkginfo.txt
178 |
179 | # Visual Studio cache files
180 | # files ending in .cache can be ignored
181 | *.[Cc]ache
182 | # but keep track of directories ending in .cache
183 | !*.[Cc]ache/
184 |
185 | # Others
186 | ClientBin/
187 | ~$*
188 | *~
189 | *.dbmdl
190 | *.dbproj.schemaview
191 | *.pfx
192 | *.publishsettings
193 | node_modules/
194 | orleans.codegen.cs
195 |
196 | # Since there are multiple workflows, uncomment next line to ignore bower_components
197 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
198 | #bower_components/
199 |
200 | # RIA/Silverlight projects
201 | Generated_Code/
202 |
203 | # Backup & report files from converting an old project file
204 | # to a newer Visual Studio version. Backup files are not needed,
205 | # because we have git ;-)
206 | _UpgradeReport_Files/
207 | Backup*/
208 | UpgradeLog*.XML
209 | UpgradeLog*.htm
210 |
211 | # SQL Server files
212 | *.mdf
213 | *.ldf
214 |
215 | # Business Intelligence projects
216 | *.rdl.data
217 | *.bim.layout
218 | *.bim_*.settings
219 |
220 | # Microsoft Fakes
221 | FakesAssemblies/
222 |
223 | # GhostDoc plugin setting file
224 | *.GhostDoc.xml
225 |
226 | # Node.js Tools for Visual Studio
227 | .ntvs_analysis.dat
228 |
229 | # Visual Studio 6 build log
230 | *.plg
231 |
232 | # Visual Studio 6 workspace options file
233 | *.opt
234 |
235 | # Visual Studio LightSwitch build output
236 | **/*.HTMLClient/GeneratedArtifacts
237 | **/*.DesktopClient/GeneratedArtifacts
238 | **/*.DesktopClient/ModelManifest.xml
239 | **/*.Server/GeneratedArtifacts
240 | **/*.Server/ModelManifest.xml
241 | _Pvt_Extensions
242 |
243 | # Paket dependency manager
244 | .paket/paket.exe
245 | paket-files/
246 |
247 | # FAKE - F# Make
248 | .fake/
249 |
250 | # JetBrains Rider
251 | .idea/
252 | *.sln.iml
253 |
--------------------------------------------------------------------------------
/MSExcelAutomation.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 14
4 | VisualStudioVersion = 14.0.25420.1
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MSExcelAutomation", "MSExcelAutomation\MSExcelAutomation.csproj", "{277AAE6D-33D7-4537-9E91-FE95E0B3B3CC}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {277AAE6D-33D7-4537-9E91-FE95E0B3B3CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {277AAE6D-33D7-4537-9E91-FE95E0B3B3CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {277AAE6D-33D7-4537-9E91-FE95E0B3B3CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {277AAE6D-33D7-4537-9E91-FE95E0B3B3CC}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | EndGlobal
23 |
--------------------------------------------------------------------------------
/MSExcelAutomation/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/MSExcelAutomation/MSExcelAutomation.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {277AAE6D-33D7-4537-9E91-FE95E0B3B3CC}
8 | WinExe
9 | Properties
10 | MSExcelAutomation
11 | MSExcelAutomation
12 | v4.5.2
13 | 512
14 | true
15 | publish\
16 | true
17 | Disk
18 | false
19 | Foreground
20 | 7
21 | Days
22 | false
23 | false
24 | true
25 | 0
26 | 1.0.0.%2a
27 | false
28 | false
29 | true
30 |
31 |
32 | AnyCPU
33 | true
34 | full
35 | false
36 | bin\Debug\
37 | DEBUG;TRACE
38 | prompt
39 | 4
40 |
41 |
42 | AnyCPU
43 | pdbonly
44 | true
45 | bin\Release\
46 | TRACE
47 | prompt
48 | 4
49 |
50 |
51 |
52 | True
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 | Form
69 |
70 |
71 | MSExcelAutomationWnd.cs
72 |
73 |
74 |
75 |
76 | MSExcelAutomationWnd.cs
77 |
78 |
79 | ResXFileCodeGenerator
80 | Resources.Designer.cs
81 | Designer
82 |
83 |
84 | True
85 | Resources.resx
86 |
87 |
88 | SettingsSingleFileGenerator
89 | Settings.Designer.cs
90 |
91 |
92 | True
93 | Settings.settings
94 | True
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 | False
103 | Microsoft .NET Framework 4.5.2 %28x86 and x64%29
104 | true
105 |
106 |
107 | False
108 | .NET Framework 3.5 SP1
109 | false
110 |
111 |
112 |
113 |
120 |
--------------------------------------------------------------------------------
/MSExcelAutomation/MSExcelAutomationWnd.Designer.cs:
--------------------------------------------------------------------------------
1 | namespace MSExcelAutomation
2 | {
3 | partial class MSExcelAutomationWnd
4 | {
5 | ///
6 | /// Required designer variable.
7 | ///
8 | private System.ComponentModel.IContainer components = null;
9 |
10 | ///
11 | /// Clean up any resources being used.
12 | ///
13 | /// true if managed resources should be disposed; otherwise, false.
14 | protected override void Dispose(bool disposing)
15 | {
16 | if (disposing && (components != null))
17 | {
18 | components.Dispose();
19 | }
20 | base.Dispose(disposing);
21 | }
22 |
23 | #region Windows Form Designer generated code
24 |
25 | ///
26 | /// Required method for Designer support - do not modify
27 | /// the contents of this method with the code editor.
28 | ///
29 | private void InitializeComponent()
30 | {
31 | System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MSExcelAutomationWnd));
32 | this.automateExcelSpreadsheet = new System.Windows.Forms.Button();
33 | this.closeMainWnd = new System.Windows.Forms.Button();
34 | this.SuspendLayout();
35 | //
36 | // automateExcelSpreadsheet
37 | //
38 | this.automateExcelSpreadsheet.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
39 | this.automateExcelSpreadsheet.Cursor = System.Windows.Forms.Cursors.Hand;
40 | this.automateExcelSpreadsheet.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
41 | this.automateExcelSpreadsheet.Location = new System.Drawing.Point(305, 317);
42 | this.automateExcelSpreadsheet.Margin = new System.Windows.Forms.Padding(4);
43 | this.automateExcelSpreadsheet.Name = "automateExcelSpreadsheet";
44 | this.automateExcelSpreadsheet.Size = new System.Drawing.Size(179, 32);
45 | this.automateExcelSpreadsheet.TabIndex = 0;
46 | this.automateExcelSpreadsheet.Text = "Automate Excel";
47 | this.automateExcelSpreadsheet.UseVisualStyleBackColor = true;
48 | this.automateExcelSpreadsheet.Click += new System.EventHandler(this.automateExcelSpreadsheet_Click);
49 | //
50 | // closeMainWnd
51 | //
52 | this.closeMainWnd.BackColor = System.Drawing.Color.Transparent;
53 | this.closeMainWnd.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("closeMainWnd.BackgroundImage")));
54 | this.closeMainWnd.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
55 | this.closeMainWnd.Cursor = System.Windows.Forms.Cursors.Hand;
56 | this.closeMainWnd.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
57 | this.closeMainWnd.Location = new System.Drawing.Point(716, -2);
58 | this.closeMainWnd.Name = "closeMainWnd";
59 | this.closeMainWnd.Size = new System.Drawing.Size(57, 44);
60 | this.closeMainWnd.TabIndex = 1;
61 | this.closeMainWnd.UseVisualStyleBackColor = false;
62 | this.closeMainWnd.Click += new System.EventHandler(this.closeMainWnd_Click);
63 | //
64 | // MSExcelAutomationWnd
65 | //
66 | this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
67 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
68 | this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
69 | this.ClientSize = new System.Drawing.Size(772, 411);
70 | this.Controls.Add(this.closeMainWnd);
71 | this.Controls.Add(this.automateExcelSpreadsheet);
72 | this.Font = new System.Drawing.Font("Verdana", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
73 | this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
74 | this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
75 | this.Margin = new System.Windows.Forms.Padding(4);
76 | this.Name = "MSExcelAutomationWnd";
77 | this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
78 | this.Text = "MSExcelAutomation";
79 | this.ResumeLayout(false);
80 |
81 | }
82 |
83 | #endregion
84 |
85 | private System.Windows.Forms.Button automateExcelSpreadsheet;
86 | private System.Windows.Forms.Button closeMainWnd;
87 | }
88 | }
89 |
90 |
--------------------------------------------------------------------------------
/MSExcelAutomation/MSExcelAutomationWnd.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Windows.Forms;
3 | using System.Reflection;
4 | using Excel = Microsoft.Office.Interop.Excel;
5 |
6 | namespace MSExcelAutomation
7 | {
8 | public partial class MSExcelAutomationWnd : Form
9 | {
10 |
11 | private System.Globalization.CultureInfo oldCI;
12 |
13 | private Excel.Application oXL;
14 | private Excel._Workbook oWB;
15 | private Excel._Worksheet oSheet;
16 |
17 | public MSExcelAutomationWnd()
18 | {
19 | InitializeComponent();
20 | SetNewCurrentCulture();
21 | }
22 |
23 | private void automateExcelSpreadsheet_Click(object sender, EventArgs e)
24 | {
25 | Excel.Range oRng;
26 |
27 | try
28 | {
29 | oXL = new Excel.Application();
30 | oXL.Visible = true;
31 |
32 | oWB = oXL.Workbooks.Add(Missing.Value);
33 | oWB.Title = "Test";
34 | oSheet = (Excel._Worksheet)oWB.ActiveSheet;
35 | oSheet.Name = "Test";
36 |
37 | oSheet.Cells[1, 1] = "First Name";
38 | oSheet.Cells[1, 2] = "Last Name";
39 | oSheet.Cells[1, 3] = "E-mail";
40 | oSheet.Cells[1, 4] = "Salary";
41 |
42 | oSheet.get_Range("A1", "D1").Font.Bold = true;
43 | oSheet.get_Range("A1", "D1").RowHeight = 37;
44 | oSheet.get_Range("A1", "D1").VerticalAlignment =
45 | Excel.XlVAlign.xlVAlignCenter;
46 |
47 | string[,] saNames = new string[5, 2];
48 |
49 | saNames[0, 0] = "John";
50 | saNames[0, 1] = "Smith";
51 | saNames[1, 0] = "Tom";
52 | saNames[1, 1] = "Brown";
53 | saNames[2, 0] = "Sue";
54 | saNames[2, 1] = "Thomas";
55 | saNames[3, 0] = "Jane";
56 | saNames[3, 1] = "Jones";
57 | saNames[4, 0] = "Adams";
58 | saNames[4, 1] = "Johnson";
59 |
60 | oSheet.get_Range("A2", "B6").Value2 = saNames;
61 |
62 | oRng = oSheet.get_Range("C2", "C6");
63 | oRng.Formula = "=A2 & \".\" & B2 & \"@\" & \"gmail.com\"";
64 |
65 | oRng = oSheet.get_Range("D2", "D6");
66 | oRng.Formula = "=RAND()*10000";
67 | oRng.NumberFormat = "$0.00";
68 |
69 | oRng = oSheet.get_Range("A1", "D1");
70 | oRng.EntireColumn.AutoFit();
71 |
72 | DisplayQuarterlySales(oSheet);
73 |
74 | oXL.Visible = true;
75 | oXL.UserControl = true;
76 | }
77 | catch (Exception exception)
78 | {
79 | string errorMessage = "Error :";
80 |
81 | errorMessage = string.Concat(errorMessage, exception.Message);
82 | errorMessage = string.Concat(errorMessage, " Line: ");
83 | errorMessage = string.Concat(errorMessage, exception.Source);
84 |
85 | MessageBox.Show(errorMessage, "Error");
86 | }
87 | }
88 |
89 | private void DisplayQuarterlySales(Excel._Worksheet oWS)
90 | {
91 | Excel._Workbook oWB;
92 | Excel.Series oSeries;
93 | Excel.Range oResizeRange;
94 | Excel._Chart oChart;
95 | string sMsg;
96 | int iNumQtrs;
97 |
98 | for (iNumQtrs = 4; iNumQtrs >= 2; iNumQtrs--)
99 | {
100 | sMsg = "Enter sales data for ";
101 | sMsg = string.Concat(sMsg, iNumQtrs);
102 | sMsg = string.Concat(sMsg, " quarter(s)?");
103 |
104 | DialogResult iRet = MessageBox.Show(sMsg, "Quarterly Sales?",
105 | MessageBoxButtons.YesNo);
106 |
107 | if (iRet == DialogResult.Yes)
108 | break;
109 | }
110 |
111 | sMsg = "Displaying data for ";
112 | sMsg = string.Concat(sMsg, iNumQtrs);
113 | sMsg = string.Concat(sMsg, " quarter(s).");
114 |
115 | MessageBox.Show(sMsg, "Quarterly Sales");
116 |
117 | oResizeRange = oWS.get_Range("E1", "E1").get_Resize(Missing.Value, iNumQtrs);
118 | oResizeRange.Formula = "=\"Q\" & COLUMN()-4 & CHAR(10) & \"Sales\"";
119 |
120 | oResizeRange.Orientation = 38;
121 | oResizeRange.WrapText = true;
122 |
123 | oResizeRange.Interior.ColorIndex = 36;
124 |
125 | oResizeRange = oWS.get_Range("E2", "E6").get_Resize(Missing.Value, iNumQtrs);
126 | oResizeRange.Formula = "=RAND()*100";
127 | oResizeRange.NumberFormat = "$0.00";
128 |
129 | oResizeRange = oWS.get_Range("E1", "E6").get_Resize(Missing.Value, iNumQtrs);
130 | oResizeRange.Borders.Weight = Excel.XlBorderWeight.xlThin;
131 |
132 | oResizeRange = oWS.get_Range("E8", "E8").get_Resize(Missing.Value, iNumQtrs);
133 | oResizeRange.Formula = "=SUM(E2:E6)";
134 | oResizeRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeBottom).LineStyle =
135 | Excel.XlLineStyle.xlDouble;
136 | oResizeRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeBottom).Weight =
137 | Excel.XlBorderWeight.xlThick;
138 |
139 | oWB = (Excel._Workbook)oWS.Parent;
140 | oChart = (Excel._Chart)oWB.Charts.Add(Missing.Value, Missing.Value,
141 | Missing.Value, Missing.Value);
142 |
143 | oResizeRange = oWS.get_Range("E2:E6").get_Resize(Missing.Value, iNumQtrs);
144 |
145 | oChart.SetSourceData(oResizeRange, Excel.XlRowCol.xlColumns);
146 | oChart.ChartType = Excel.XlChartType.xlColumnClustered;
147 |
148 | oSeries = (Excel.Series)oChart.SeriesCollection(1);
149 | oSeries.XValues = oWS.get_Range("A2", "A6");
150 |
151 | for (int iRet = 1; iRet <= iNumQtrs; iRet++)
152 | {
153 | oSeries = (Excel.Series)oChart.SeriesCollection(iRet);
154 | string seriesName;
155 | seriesName = "=\"Q";
156 | seriesName = string.Concat(seriesName, iRet);
157 | seriesName = string.Concat(seriesName, "\"");
158 | oSeries.Name = seriesName;
159 | }
160 |
161 | oChart.Location(Excel.XlChartLocation.xlLocationAsObject, oWS.Name);
162 |
163 | oResizeRange = (Excel.Range)oWS.Rows.get_Item(10, Missing.Value);
164 | oWS.Shapes.Item("Chart 1").Top = (float)oResizeRange.Top;
165 | oResizeRange = (Excel.Range)oWS.Columns.get_Item(2, Missing.Value);
166 | oWS.Shapes.Item("Chart 1").Left = (float)oResizeRange.Left;
167 | }
168 |
169 | private void closeMainWnd_Click(object sender, EventArgs e)
170 | {
171 | try
172 | {
173 | oWB.Close(false, Missing.Value, Missing.Value);
174 | oXL.Quit();
175 |
176 | releaseObject(oSheet);
177 | releaseObject(oWB);
178 | releaseObject(oXL);
179 | }
180 | catch (Exception exception)
181 | {
182 |
183 | }
184 | finally
185 | {
186 | ResetCurrentCulture();
187 | ActiveForm.Close();
188 | }
189 | }
190 |
191 | private void releaseObject(object obj)
192 | {
193 | try
194 | {
195 | System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
196 | obj = null;
197 | }
198 | catch (Exception exception)
199 | {
200 | obj = null;
201 | MessageBox.Show("Exception occured while releasing object " + exception.ToString());
202 | }
203 | finally
204 | {
205 | GC.Collect();
206 | }
207 | }
208 |
209 | void SetNewCurrentCulture()
210 | {
211 | oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
212 | System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
213 | }
214 |
215 | void ResetCurrentCulture()
216 | {
217 | System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
218 | }
219 | }
220 | }
221 |
--------------------------------------------------------------------------------
/MSExcelAutomation/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading.Tasks;
5 | using System.Windows.Forms;
6 |
7 | namespace MSExcelAutomation
8 | {
9 | static class Program
10 | {
11 | ///
12 | /// The main entry point for the application.
13 | ///
14 | [STAThread]
15 | static void Main()
16 | {
17 | Application.EnableVisualStyles();
18 | Application.SetCompatibleTextRenderingDefault(false);
19 | Application.Run(new MSExcelAutomationWnd());
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/MSExcelAutomation/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("MSExcelAutomation")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("MSExcelAutomation")]
13 | [assembly: AssemblyCopyright("Copyright © 2016")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("277aae6d-33d7-4537-9e91-fe95e0b3b3cc")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/MSExcelAutomation/Properties/Resources.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.42000
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace MSExcelAutomation.Properties
12 | {
13 |
14 |
15 | ///
16 | /// A strongly-typed resource class, for looking up localized strings, etc.
17 | ///
18 | // This class was auto-generated by the StronglyTypedResourceBuilder
19 | // class via a tool like ResGen or Visual Studio.
20 | // To add or remove a member, edit your .ResX file then rerun ResGen
21 | // with the /str option, or rebuild your VS project.
22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
25 | internal class Resources
26 | {
27 |
28 | private static global::System.Resources.ResourceManager resourceMan;
29 |
30 | private static global::System.Globalization.CultureInfo resourceCulture;
31 |
32 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
33 | internal Resources()
34 | {
35 | }
36 |
37 | ///
38 | /// Returns the cached ResourceManager instance used by this class.
39 | ///
40 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
41 | internal static global::System.Resources.ResourceManager ResourceManager
42 | {
43 | get
44 | {
45 | if ((resourceMan == null))
46 | {
47 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("MSExcelAutomation.Properties.Resources", typeof(Resources).Assembly);
48 | resourceMan = temp;
49 | }
50 | return resourceMan;
51 | }
52 | }
53 |
54 | ///
55 | /// Overrides the current thread's CurrentUICulture property for all
56 | /// resource lookups using this strongly typed resource class.
57 | ///
58 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
59 | internal static global::System.Globalization.CultureInfo Culture
60 | {
61 | get
62 | {
63 | return resourceCulture;
64 | }
65 | set
66 | {
67 | resourceCulture = value;
68 | }
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/MSExcelAutomation/Properties/Resources.resx:
--------------------------------------------------------------------------------
1 |
2 |
3 |
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 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 | text/microsoft-resx
107 |
108 |
109 | 2.0
110 |
111 |
112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
113 |
114 |
115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
116 |
117 |
--------------------------------------------------------------------------------
/MSExcelAutomation/Properties/Settings.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.42000
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace MSExcelAutomation.Properties
12 | {
13 |
14 |
15 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
16 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
17 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
18 | {
19 |
20 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
21 |
22 | public static Settings Default
23 | {
24 | get
25 | {
26 | return defaultInstance;
27 | }
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/MSExcelAutomation/Properties/Settings.settings:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # MSExcelAutomation
2 | Automate the creation/editing of a Microsoft Excel 2016 spreadsheet based on a given model.
3 |
--------------------------------------------------------------------------------