Hello,
I am a newbie to C#.
What I am trying to do is write my C# data (e.g. integers & strings) into in Excel
How do I do this?
thanks
J
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Raaj KumarRank #7,836 · 233 reputationPosted Feb 18, 2010, 6:26 PM EST
using Microsoft.Office.Interop.Excel;
List<Employee> employeeList = new List<Employee>();
employeeList.Add(new Employee() { ID = 1, FirstName = "Test1", LastName = "TestLast", Address = "TestAddress1", Sex = "Male" });
employeeList.Add(new Employee() { ID = 2, FirstName = "Test2", LastName = "TestLast", Address = "TestAddress2", Sex = "FeMale" });
employeeList.Add(new Employee() { ID = 3, FirstName = "Test3", LastName = "TestLast", Address = "TestAddress3", Sex = "Male" });
employeeList.Add(new Employee() { ID = 4, FirstName = "Test4", LastName = "TestLast", Address = "TestAddress4", Sex = "FeMale" });
employeeList.Add(new Employee() { ID = 5, FirstName = "Test5", LastName = "TestLast", Address = "TestAddress5", Sex = "Male" });
Application xlApp = new Application();
Workbook wb = xlApp.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws = (Worksheet)xlApp.ActiveSheet;
int row=1,column=1;
foreach (Employee employee in employeeList)
{
ws.Cells[row, column] = employee.ID;
ws.Cells[row, column++] = employee.FirstName;
ws.Cells[row, column++] = employee.LastName;
ws.Cells[row, column++] = employee.Address;
ws.Cells[row, column++] = employee.Sex;
column = 1;
row++;
}
xlApp.Visible = true;
xlApp.UserControl = true;
Lalit MRank #1,186 · 1.5K reputationPosted Feb 18, 2010, 6:35 PM EST
j l68 reputationPosted Feb 19, 2010, 3:28 AM EST
I get some errors though.
Error #1: "Employee namespace can not be found"
How do I correct this error?
thanks
J
Raaj KumarRank #7,836 · 233 reputationPosted Feb 19, 2010, 3:53 AM EST
Just create a class like this and try
public class Employee
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Sex { get; set; }
}
Regards,
Raaj
j l68 reputationPosted Feb 19, 2010, 3:57 AM EST
How does that integrate into your 1st post?
J
Raaj KumarRank #7,836 · 233 reputationPosted Feb 19, 2010, 4:16 AM EST
Here i am attaching the code. Please have a look at this. Let me explain the code.
1. Create a Employee Class and add few properties to that(ID,FirstName....)
2. In Main method, create a list of type employees and add few employee type objects.
3. Now create an instance of Workbook and worksheet. assign these list values to the worksheet. You can also try using arraylist.
Hope this will help you.
Regards,
Raaj
j l68 reputationPosted Feb 19, 2010, 4:30 AM EST
I was able to compile your code...but I had to add references to "Microsoft Excel Library" and "Microsoft.Csharp.dll".
What path/directory is the Excel spreadsheet that is generated?
Also, I will need to call and save my data to an existing spreadsheet in the path "C:\ART\Input\WSCatalogLookupSS.xlsx"
How does this integrate to the code you sent?
thanks
J
Raaj KumarRank #7,836 · 233 reputationPosted Feb 19, 2010, 5:14 AM EST
Yes you need to add the reference which I had mentioned previously. Since you are using a COM object you need to add the reference. Add this code at the end.
//save the workbook
wb.SaveAs(@"c:\sample", XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
wb.Close(true, Type.Missing, Type.Missing);//close
xlApp.Quit();//exit
Regards,
Raaj
j l68 reputationPosted Feb 19, 2010, 5:24 AM EST
I am running your code....should it be in a Unit Test Project?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
namespace ExportToExcel
{
public class Employee
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Sex { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Employee> employeeList = new List<Employee>();
employeeList.Add(new Employee() { ID = 1, FirstName = "Test1", LastName = "TestLast", Address = "TestAddress1", Sex = "Male" });
employeeList.Add(new Employee() { ID = 2, FirstName = "Test2", LastName = "TestLast", Address = "TestAddress2", Sex = "FeMale" });
employeeList.Add(new Employee() { ID = 3, FirstName = "Test3", LastName = "TestLast", Address = "TestAddress3", Sex = "Male" });
employeeList.Add(new Employee() { ID = 4, FirstName = "Test4", LastName = "TestLast", Address = "TestAddress4", Sex = "FeMale" });
employeeList.Add(new Employee() { ID = 5, FirstName = "Test5", LastName = "TestLast", Address = "TestAddress5", Sex = "Male" });
Application xlApp = new Application();
Workbook wb = xlApp.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws = (Worksheet)xlApp.ActiveSheet;
int row=1,column=1;
foreach (Employee employee in employeeList)
{
ws.Cells[row, column] = employee.ID;
ws.Cells[row, column++] = employee.FirstName;
ws.Cells[row, column++] = employee.LastName;
ws.Cells[row, column++] = employee.Address;
ws.Cells[row, column++] = employee.Sex;
column = 1;
row++;
}
xlApp.Visible = true;
xlApp.UserControl = true;
//save the workbook
wb.SaveAs(@"c:\sample.xlsx", XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
wb.Close(true, Type.Missing, Type.Missing);//close
xlApp.Quit();//exit
}
}
}
Raaj KumarRank #7,836 · 233 reputationPosted Feb 19, 2010, 5:33 AM EST
And Remove these 2 line
xlApp.Visible = true;
xlApp.UserControl = true;
j l68 reputationPosted Feb 19, 2010, 5:36 AM EST
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
namespace ExportToExcel
{
public class Employee
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Sex { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Employee> employeeList = new List<Employee>();
employeeList.Add(new Employee() { ID = 1, FirstName = "Test1", LastName = "TestLast", Address = "TestAddress1", Sex = "Male" });
employeeList.Add(new Employee() { ID = 2, FirstName = "Test2", LastName = "TestLast", Address = "TestAddress2", Sex = "FeMale" });
employeeList.Add(new Employee() { ID = 3, FirstName = "Test3", LastName = "TestLast", Address = "TestAddress3", Sex = "Male" });
employeeList.Add(new Employee() { ID = 4, FirstName = "Test4", LastName = "TestLast", Address = "TestAddress4", Sex = "FeMale" });
employeeList.Add(new Employee() { ID = 5, FirstName = "Test5", LastName = "TestLast", Address = "TestAddress5", Sex = "Male" });
Application xlApp = new Application();
Workbook wb = xlApp.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws = (Worksheet)xlApp.ActiveSheet;
int row=1,column=1;
foreach (Employee employee in employeeList)
{
ws.Cells[row, column] = employee.ID;
ws.Cells[row, column++] = employee.FirstName;
ws.Cells[row, column++] = employee.LastName;
ws.Cells[row, column++] = employee.Address;
ws.Cells[row, column++] = employee.Sex;
column = 1;
row++;
}
// xlApp.Visible = true;
// xlApp.UserControl = true;
//save the workbook
wb.SaveAs(@"c:\sample", XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
wb.Close(true, Type.Missing, Type.Missing);//close
xlApp.Quit();//exit
}
}
}
Raaj KumarRank #7,836 · 233 reputationPosted Feb 19, 2010, 6:10 AM EST
j l68 reputationPosted Feb 19, 2010, 6:17 AM EST
J
Raaj KumarRank #7,836 · 233 reputationPosted Feb 19, 2010, 6:19 AM EST
j l68 reputationPosted Feb 19, 2010, 6:31 AM EST
(your console APP does work though)
But I need the Test Project view to work for me.
Do you know how to do this?
J
Raaj KumarRank #7,836 · 233 reputationPosted Feb 19, 2010, 6:43 AM EST
j l68 reputationPosted Feb 19, 2010, 6:54 AM EST
Send to Excel = csResult.Customers[0].AccountNumber;
Send to Excel = csResult.Customers[0].CustomerID;
Send to Excel = csResult.Customers[0].FullName;
Send to Excel = csResult.Customers[0].IsProspect;
I want to put the AccountNumber,CustomerID,FullName and IsProspect into cells within Excel....what is the C# code to put these values into Excel?
j l68 reputationPosted Feb 19, 2010, 11:45 AM EST
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/ef11a193-54f3-407b-9374-9f5770fd9fd7
J