0

I'm teaching myself to use Visual C# 2010 and I don't know what data structure to use to store 3 columns of data from an Excel worksheet.

Column A contains person's names (string), Column B contains person's age (integer), and Column C contains person's phone number (string). How do I store this into a data structure in C#?

In C++ I remember creating a vector that could hold a bunch of different data types...How do I do the equivalent in C#? I've read that vectors are not used in C#. Should I be using an array or list? Thanks.

MrPatterns
  • 4,184
  • 27
  • 65
  • 85

3 Answers3

2

The concept of a C++ vector is roughly equivalent in functionality to the generic List<T> class in C#.

As SLaks said, the best solution, given you know the types and that each set of three columns is a "row" in the table, is to create a simple class that holds your Column A, B, and C, then create a list of those:

public class ExcelData
{
   public string PersonName {get;set;}
   public int Age {get;set;}
   public string PhoneNumber {get;set;}
}

public List<ExcelData> fromExcel = new List<ExcelData>();

fromExcel.Add(new ExcelData
                  {
                     PersonName = "Joe Smith", 
                     Age = 34, 
                     PhoneNumber = "(123) 456-7890"
                  });

If you're using .NET 4 (which, since you're using VS 2010, you should be), there's a class Tuple, which has several generic overloads and a static helper to create them:

public List<Tuple<string, int, string>> fromExcel = new List<Tuple<string, int, string>>();

...

fromExcel.Add(Tuple.Create("Joe Smith", 34, "(123) 456-7890"));

The upside is a built-in, flexible class; the downside is that a Tuple is very general-purpose and so its column names are similarly general-purpose; Item1, Item2, etc.

KeithS
  • 70,210
  • 21
  • 112
  • 164
0

You can create your own class

public class Person
{
    public string name {get; set;}
    public int age {get; set;}
    public string phone {get; set;}
}

And use an ArrayList or List of this class. You can also learn how to read Excel data here: How to read data of an Excel file using C#?

Community
  • 1
  • 1
mittmemo
  • 2,062
  • 3
  • 20
  • 27
  • Ok SLaks. Can you please explain to a noob why? – MrPatterns Aug 27 '12 at 19:40
  • ArrayList is an old (.NET 1.0) collection, from before .NET supported generics. As a result, everything you put in and get back out is treated as the base System.Object, and you have to cast the elements to the type they're supposed to be. Complicating that, because you can insert any object, you can insert objects of varying types, making casting them when you get them back out very risky. All in all, ArrayList is obsolete, error-prone, and there are few if any remaining uses for it that are considered "good coding" and wouldn't be better served by the generic `List`. – KeithS Aug 28 '12 at 15:52
  • @KeithS What if you need direct access? And don't make any casting errors? It's not difficult to do. Are there any performance issues or are they just error-prone? – mittmemo Aug 28 '12 at 16:17
  • Well, there can be some performance issues with boxing/unboxing of value types stored in ArrayLists. You are not guaranteed "direct access" in that situation either. Not making casting errors may not be difficult, but if you're saying you've never made a coding error in any software you've ever written in your entire life, whether compile-time or runtime, I call BS. Generics give you compile-time checking of type safety in an object that can work with multiple contained types; that is their single greatest power, and you would be wise to make use of it. – KeithS Aug 28 '12 at 16:47
0

What you'll need to do to accomplish this is read up a bit on the library commands available for such actions. I included some that i used below for styling, and manually setting box contents etc. There are several ways to do what you want. Also Vectors still exist in C#. I recommend storing the information in a vector or array and then looping through that vector or array to populate your excel sheet. ( composite formatting would most likely be easiest)

Hope that this helps! Cheers!

//using Microsoft.Office.Interop.Excel;

/*
                //WRITE DATA FROM TABLES TO AN EXCEL SHEET
                Application excelapp = new Application();


                excelapp.Visible = true;
                //Range range = excelapp.Application.get_Range("A1", "C1"); 

                _Workbook workbook = (_Workbook)(excelapp.Workbooks.Add(Type.Missing));
                _Worksheet worksheet = (_Worksheet)workbook.ActiveSheet;


                worksheet.Cells[1, 1].Style.Font.Bold = true;

                worksheet.Cells[1, 1] = "Brochure Fares";
                worksheet.Cells[1, 2] = "Cruise Only Fares";
                worksheet.Cells[1, 3] = "Port, Security & Handling";

                //worksheet.Cells.Style.Font.Bold = false;

                //worksheet.Cells[1,1].Style.Selection = "Check Cell";
                //worksheet.Range("B1").Selection = "Check Cell"; 
                worksheet.Cells[1, 1].ColumnWidth = 20;
                worksheet.Cells[1, 2].ColumnWidth = 20;
                worksheet.Cells[1, 3].ColumnWidth = 25;

                //worksheet.get_Range("A1", "D1").VerticalAlignment.Center = true; 
                //worksheet.get_Range("A1", "C1").VerticalAlignment = ; 
                //worksheet.Cells[1,1].VerticalAlignment.Center = true;


                //worksheet.Cells[1, 1].mergeandcenter = true;
                //worksheet.Cells[1, 1].wraptext = true; 






                excelapp.UserControl = true;


                workbook.SaveAs(@"TestSheet");
                workbook.Close();
                excelapp.Visible = false; 
                */
newITguy
  • 155
  • 1
  • 11
  • The Vector type in C# is not a collection; it represents a 2D geometric vector. The OP should use `List` for a collection of variable length that allows addition and removal of elements and roughly constant access time. – KeithS Aug 28 '12 at 16:12