使用 C# 读取/写入 Excel 文件 (.xls/.xlsx)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10419071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using C# to read/write Excel files (.xls/.xlsx)
提问by jitsuin
How can I read and write Excel files in C#? I've already added the Excel Objects Library to my project, but I'm not getting a clear enough description of what needs to be done to access the files.
如何在 C# 中读写 Excel 文件?我已经将 Excel 对象库添加到我的项目中,但是我没有得到足够清楚的描述来访问这些文件需要做什么。
Please help me understand and, when explaining, please keep in mind I'm kind of new to this but I'm not a complete newbie. I study a lot, so I'm not totally ignorant.
请帮助我理解,并在解释时请记住,我对此有点陌生,但我不是一个完整的新手。我学习了很多,所以我并不完全无知。
回答by LosManos
If you are doing simple manipulation and can tie yourself to xlsx then you can look into manipulating the XML yourself. I have done it and found it to be faster than grokking the excel libs.
如果您正在进行简单的操作并且可以将自己与 xlsx 联系起来,那么您可以考虑自己操作 XML。我已经做到了,发现它比探索 excel 库更快。
There are also 3rd party libs that can be easier to use... and can be used on the server which MS's can't.
还有 3rd 方库可以更容易使用......并且可以在 MS 不能使用的服务器上使用。
回答by Paul Alan Taylor
I use NPOI for all my Excel needs.
我使用 NPOI 来满足我所有的 Excel 需求。
Comes with a solution of examples for many common Excel tasks.
附带许多常见 Excel 任务的示例解决方案。
回答by Baget
You can use Excel Automation (it is basically a COM Base stuff) e.g:
您可以使用 Excel 自动化(它基本上是一个 COM Base 的东西),例如:
Excel.Application xlApp ;
Excel.Workbook xlWorkBook ;
Excel.Worksheet xlWorkSheet ;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Open("1.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
回答by Brad Butner
I'm a big fan of using EPPlusto perform these types of actions. EPPlus is a library you can reference in your project and easily create/modify spreadsheets on a server. I use it for any project that requires an export function.
我非常喜欢使用EPPlus来执行这些类型的操作。EPPlus 是一个库,您可以在项目中引用并在服务器上轻松创建/修改电子表格。我将它用于任何需要导出功能的项目。
Here's a nice blog entry that shows how to use the library, though the library itself should come with some samples that explain how to use it.
这是一个很好的博客条目,展示了如何使用库,尽管库本身应该附带一些解释如何使用它的示例。
Third party libraries are a lot easier to use than Microsoft COM objects, in my opinion. I would suggest giving it a try.
在我看来,第三方库比 Microsoft COM 对象更容易使用。我建议尝试一下。
回答by Ramya Prakash Rout
**Reading the Excel File:**
string filePath = @"d:\MyExcel.xlsx";
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(filePath);
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Excel.Range xlRange = xlWorkSheet.UsedRange;
int totalRows = xlRange.Rows.Count;
int totalColumns = xlRange.Columns.Count;
string firstValue, secondValue;
for (int rowCount = 1; rowCount <= totalRows; rowCount++)
{
firstValue = Convert.ToString((xlRange.Cells[rowCount, 1] as Excel.Range).Text);
secondValue = Convert.ToString((xlRange.Cells[rowCount, 2] as Excel.Range).Text);
Console.WriteLine(firstValue + "\t" + secondValue);
}
xlWorkBook.Close();
xlApp.Quit();
**Writting the Excel File:**
Excel.Application xlApp = new Excel.Application();
object misValue = System.Reflection.Missing.Value;
Excel.Workbook xlWorkBook = xlApp.Workbooks.Add(misValue);
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "ID";
xlWorkSheet.Cells[1, 2] = "Name";
xlWorkSheet.Cells[2, 1] = "100";
xlWorkSheet.Cells[2, 2] = "John";
xlWorkSheet.Cells[3, 1] = "101";
xlWorkSheet.Cells[3, 2] = "Herry";
xlWorkBook.SaveAs(filePath, Excel.XlFileFormat.xlOpenXMLWorkbook, misValue, misValue, misValue, misValue,
Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close();
xlApp.Quit();

