ExcelLibrary是一个比较轻量级的Excel读写组件,由国人开发的。可以读写 xls 和 xlsx 格式的 Excel。支持简单的公式,可插入图片,对于格式的设置也不是很丰富,可设置单元格宽度,数据格式化显示。对字体,前景、背景色进行设置是它的局限性。虽比不上NPOI,但作为小巧的用来读写纯数据内容的 Excel也是很高效的。本文主要介绍一下.NET(C#)中,,使用ExcelLibrary读写Excel(.xls,.xlsx)文件数据的方法,以及相关的示例代码,并且可以不安装Microsoft Office Excel软件。

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

2、使用ExcelLibrary读取Excel文件示例代码

FileStream fileStream = new FileStream(@"c:\cjavapy.xls", FileMode.Open);
Workbook workbook = Workbook.Load(fileStream);
//也可以直接传个文件名
//https://www.cjavapy.com
//Workbook workbook = Workbook.Load(@"c:\cjavapy.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();

3、通过ExcelLibrary写入Excel文件导出数据

List<Student> students = GetInput();
List<Student> onlineStudentsSortedByResult = students
    .Where(student => student.StudentType == "Online")
    .OrderByDescending(student => student.CalculateResult())
    .ToList();
// creating new workbook and sheet
const string pathToOutputFile = "../../output/outputFile.xls";
Workbook workbook = new Workbook();
Worksheet worksheet = new Worksheet("Online Students");
// adding all the students
for (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 workbook
workbook.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)文件

推荐文档