开发者

To read/write from excel spreadsheet using C#

开发者 https://www.devze.com 2023-02-17 12:43 出处:网络
I need to make a program that writes some data to an excel spreadsheet. Something basic along the lines of First name, last name, phone number, e-mail per row with each category in its own column.

I need to make a program that writes some data to an excel spreadsheet. Something basic along the lines of First name, last name, phone number, e-mail per row with each category in its own column.

I don't even know where to start. If someone could tell me which assemblies to reference and maybe point me to a website or a开发者_高级运维 book that covers writing/reading data from an excel spreadsheet via a C# program that would be great.

Many thanks.


Add a reference to Microsoft.Office.Interop.Excel.

Assuming you have a repository of that data somewhere, and your model looks something like

class Contact
{
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }
}

you can import it into excel like this

Application excelapp = new Application();
excelapp.Visible = true;

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

worksheet.Cells[1, 1] = "First Name";
worksheet.Cells[1, 2] = "Last Name";
worksheet.Cells[1, 3] = "Email";
worksheet.Cells[1, 4] = "Phone Number";

int row = 1;

foreach (var contact in contacts)
{
    row++;

    worksheet.Cells[row, 1] = contact.Firstname;
    worksheet.Cells[row, 2] = contact.Lastname;
    worksheet.Cells[row, 3] = contact.Email;
    worksheet.Cells[row, 4] = contact.PhoneNumber;
}

excelapp.UserControl = true;

You can read more about the Excel interop library here: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel%28v=office.11%29.aspx


This particular feature is called "Excel Automation" in .NET where you can use C# to manipulate your spreadsheet.

A good starting point will be, http://support.microsoft.com/kb/302084#top

Regards, Andy.


Depending on the level of sophistication needed:

  • Write a comma-separated values (CSV) text file. Excel will open it, however you wont get any formatting.
  • Write an HTML table to file and name the file as filename.xls.
  • Write out an XML file in a format that Excel can open.
  • Call Excel directly and get it to build the spreadsheet. (See cherhan's answer)


Why don't you just create a csv file, saving it as xls. Is native excel a must?


As cherhan mentioned, Excel automation is the standard, Microsoft approach to programmatically creating spreadsheets. The downsides of this approach is that it used a COM interop layer and therefore requires Excel (and licence) for each server your code is running on.

I've not managed to find any great open source projects for doing this any better, however, can recommend GemBox's spreadsheet API's as a well-made commercial option (plus they have a basic free version, good for testing it). One major plus to this approach is that it's 100% managed code so makes deployment a bit neater.

The choice is probably down to if there's a budget you're willing to spend on a library!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号