简体   繁体   English

使用C#从Excel电子表格中读取/写入

[英]To read/write from excel spreadsheet using C#

I need to make a program that writes some data to an excel spreadsheet. 我需要制作一个程序,将一些数据写入excel电子表格。 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. 如果有人可以告诉我参考哪些程序集,并且可能指向一个网站或一本书,其中包括通过C#程序从excel电子表格中写入/读取数据,这将是很棒的。

Many thanks. 非常感谢。

Add a reference to Microsoft.Office.Interop.Excel. 添加对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 你可以像这样将它导入excel

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 您可以在此处阅读有关Excel互操作库的更多信息: 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. 这个特殊功能在.NET中称为“Excel Automation”,您可以使用C#来操作电子表格。

A good starting point will be, http://support.microsoft.com/kb/302084#top 一个很好的起点是http://support.microsoft.com/kb/302084#top

Regards, Andy. 问候,安迪。

Depending on the level of sophistication needed: 取决于所需的复杂程度:

  • Write a comma-separated values (CSV) text file. 写一个逗号分隔值(CSV)文本文件。 Excel will open it, however you wont get any formatting. Excel将打开它,但你不会得到任何格式。
  • Write an HTML table to file and name the file as filename.xls. 将HTML表写入文件并将文件命名为filename.xls。
  • Write out an XML file in a format that Excel can open . 以Excel可以打开格式写出XML文件。
  • Call Excel directly and get it to build the spreadsheet. 直接调用Excel并使其构建电子表格。 (See cherhan's answer) (见cherhan的回答)

Why don't you just create a csv file, saving it as xls. 为什么不创建一个csv文件,将其保存为xls。 Is native excel a must? 原生优秀是必须的吗?

As cherhan mentioned, Excel automation is the standard, Microsoft approach to programmatically creating spreadsheets. 正如cherhan所提到的,Excel自动化是Microsoft以编程方式创建电子表格的标准方法。 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. 这种方法的缺点是它使用了COM互操作层,因此需要为运行代码的每个服务器提供Excel(和许可证)。

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). 我没有找到任何更好的开源项目来做这件事,但是,可以推荐GemBox的电子表格API作为一个制作精良的商业选项(加上他们有一个基本的免费版本 ,很适合测试它)。 One major plus to this approach is that it's 100% managed code so makes deployment a bit neater. 这种方法的一个主要优点是它是100%托管代码,因此部署更加整洁。

The choice is probably down to if there's a budget you're willing to spend on a library! 选择可能取决于你是否愿意花在图书馆上的预算!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM