Booksman
Booksman

Reputation: 1623

how to do this in C# a VBA statment

I have to name the excell sheets myself with the patient name which i have in the input file. rather than the sheet1 sheet2 etc.

in VBA:

Set wsPatient =ThisWorkbook.Worksheets.Add
wsPatient.Name = "ThePatientName"

Upvotes: 0

Views: 131

Answers (2)

David Heffernan
David Heffernan

Reputation: 613461

Like this:

Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp != null)
{
    xlApp.Visible = true;
    Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
    Worksheet wsPatient = (Worksheet)wb.Worksheets.Add(missing, missing, missing, missing);
    wsPatient.Name = "ThePatientName";
}

See this MSDN article for details of the references you need to add and the using statements.

Once you get past the boiler-plate, the C# code will be pretty much identical to what you are used to with VBA.

Upvotes: 4

Eder
Eder

Reputation: 1884

I don't think that you can do the same in C# as you did in VBA, VBA (Visual Basic for Applications) provides built in functions to integrate the programming language (VBA) with Excel, However there are lots of libraries that you can use to work with Excel in C#

  1. EPPlus
  2. COM Objects (Excel.Application) Example

Upvotes: 2

Related Questions