1、安装引用ExcelLibrary
通过NuGet获取ExcelLibrary和手动引用
1)使用Nuget管理控制台
将ExcelLibrary集成到项目中的最简单方法是使用NuGet。您可以通过打开包管理器控制台(PM)并键入以下语句来安装ExcelLibrary:
Install-Package ExcelLibrary
2)使用Nuget图形管理器
使用Nuget的界面的管理器搜索"ExcelLibrary"=> 找到点出点击"安装"。
相关文档:VS(Visual Studio)中Nuget的使用
手动下载:https://code.google.com/archive/p/excellibrary/downloads
FileStream fileStream = new FileStream(@"c:\wonhero.xls", FileMode.Open);Workbook workbook = Workbook.Load(fileStream);//也可以直接传个文件名//https://www.wonhero.com//Workbook workbook = Workbook.Load(@"c:\wonhero.xls");Worksheet worksheet = workbook.Worksheets[0];for (int i = 0; i <= worksheet.Cells.LastRowIndex; i++){ for (int j = 0; j <= worksheet.Cells.LastColIndex; j++) { Console.Write(worksheet.Cells[i, j].Value); if (j < worksheet.Cells.LastColIndex) Console.Write(", "); } Console.WriteLine();}fileStream.Close();
Liststudents = GetInput();List onlineStudentsSortedByResult = students .Where(student => student.StudentType == "Online") .OrderByDescending(student => student.CalculateResult()) .ToList();// creating new workbook and sheetconst string pathToOutputFile = "../../output/outputFile.xls";Workbook workbook = new Workbook();Worksheet worksheet = new Worksheet("Online Students");// adding all the studentsfor (int i = 0; i < onlineStudentsSortedByResult.Count; i++){ worksheet.Cells[i, 0] = new Cell(onlineStudentsSortedByResult[i].ID); worksheet.Cells[i, 1] = new Cell(onlineStudentsSortedByResult[i].FirstName); worksheet.Cells[i, 2] = new Cell(onlineStudentsSortedByResult[i].LastName); worksheet.Cells[i, 3] = new Cell(onlineStudentsSortedByResult[i].Email); worksheet.Cells[i, 4] = new Cell(onlineStudentsSortedByResult[i].Gender); worksheet.Cells[i, 5] = new Cell(onlineStudentsSortedByResult[i].StudentType); worksheet.Cells[i, 6] = new Cell(onlineStudentsSortedByResult[i].ExamResult); worksheet.Cells[i, 7] = new Cell(onlineStudentsSortedByResult[i].HomeworkSent); worksheet.Cells[i, 8] = new Cell(onlineStudentsSortedByResult[i].HomeworkEvaluated); worksheet.Cells[i, 9] = new Cell(onlineStudentsSortedByResult[i].TeamworkScore); worksheet.Cells[i, 10] = new Cell(onlineStudentsSortedByResult[i].AttendacesCount); worksheet.Cells[i, 11] = new Cell(onlineStudentsSortedByResult[i].Bonus); worksheet.Cells[i, 12] = new Cell(onlineStudentsSortedByResult[i].CalculateResult());}// saving the workbookworkbook.Worksheets.Add(worksheet);workbook.Save(pathToOutputFile);
ExcelLibrary源码:https://storage.googleapis.com/google-code-archive-source/v2/code.google.com/excellibrary/source-archive.zip
相关文档:
.NET(C#) 使用ExcelLibrary读取Excel(.xls,.xlsx)文件示例代码(不用安装Office)
.NET(C#)不安装Microsoft Office使用ExcelLibrary创建Excel(.xls,.xlsx)文件