Category Archives: Excel

Load a CSV file in Excel in C#

Loading a CSV file (comma-separated value file) into Excel isn’t too hard once you know the tricks. Using the same tricks you can load a file that uses any delimiter, not just a comma. First at design time, add a … Continue reading

Posted in Excel, files, Office | Tagged , , , , , , , , , , , , | 5 Comments

Make an Excel chart in C#

This example uses the following code to create an Excel chart in a new workbook. private void btnCreateChart_Click( object sender, EventArgs e) { // Get the Excel application object. Excel.Application excel_app = new Excel.ApplicationClass(); // Make Excel visible (optional). excel_app.Visible … Continue reading

Posted in Excel, interoperability, Office | Tagged , , , , , , , , , , | Leave a comment

Add and remove pictures in an Excel worksheet

This is another brief foray into VBA programming. When you click the Insert Image button, the following VBA code executes. ‘ Insert a picture at the selection. Sub Button1_Click() Dim filters As String Dim filename As Variant ‘ Define the … Continue reading

Posted in Excel, Office, VBA | Tagged , , , , , , , , , | Leave a comment

Read Excel data in C#

To read Excel data, you can open the Excel application and use it as a server to manipulate Excel workbooks. First open the Add References dialog. On the COM tab, select “Microsoft Excel 14.0 Object Library” (or whatever version you … Continue reading

Posted in Excel, Office | Tagged , , , , , , , , , | 3 Comments

Write Excel data in C#

This example shows how to write Excel data into a workbook. You can do that by opening the Excel application and use it as a server to manipulate Excel workbooks. First open the Add References dialog. On the COM tab, … Continue reading

Posted in Excel, Office | Tagged , , , , , , , , , | 2 Comments

Use VBA code to pick random cells from the currently selected cells in an Excel workbook

This example shows how you can use VBA code to pick random cells in an Excel workbook. Sooner or later, many programmers need to write code to control another application such as the Microsoft Office applications, either for their own … Continue reading

Posted in Excel, VBA | Tagged , , , , , , , , , , | Leave a comment

Convert between Excel column numbers and names in C#

Excel uses both numbers and names to identify columns. A = 1, B = 2, …, Z = 26, AZ = 27, …, XFD = 16,384. This example converts between these Excel-style column numbers and names. It’s a little trickier … Continue reading

Posted in algorithms, Excel, Office | Tagged , , , , , , , , , | 1 Comment

Make a C# DLL and use it from Excel VBA code

This example shows how to make a C# DLL and then use it from VBA code running in Excel. A similar procedure should work for other Office applications. Note that some of the steps may require modification and significant fiddling … Continue reading

Posted in Excel, interoperability, Office, VBA | 10 Comments

Use VBA macros to add and remove hyperlinks to external PowerPoint and Word files in C#

My book Essential Algorithms: A Practical Approach to Computer Algorithms has associated instructor materials that include lesson plans and PowerPoint presentations. There’s also a summary Excel workbook. In the workbook, column C contains links to lesson plans (Word documents) and … Continue reading

Posted in Excel, Office, PowerPoint, Word | Leave a comment

Use VBA to randomize blocks of cells in an Excel workbook in C#

This is another quick excursion into VBA programming. This example uses a VBA macro to let you randomize cells in an Excel workbook. If you select a contiguous block of cells and click the Randomize button, the following macro executes. … Continue reading

Posted in Excel, Office, VBA | Leave a comment