一、MaterialDesignInXamlToolkit(UI)

这是GitHub一个WPF的UI控件主题库,star数截至2022年6月底为12.4K,样式好看,资料也比较全,我比较喜欢的是demo程序中可以直接复制对应控件代码使用。入门使用步骤:

1.创建WPF项目

在VS中新建WPF项目,注意:需要.net framework≥4.5.2或.net core ≥3.1
使用.netframework时发现报错信息,但是.net core直接用没问题

2.nuget安装

引用MaterialDesignInXamlToolkit项目:在nuget包管理中搜索MaterialDesignThemes 安装
离线网络使用其他方式,未测试,但应该不复杂

3.编辑 xaml文件

  1. <Application
  2. x:Class="YourProject.App"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="clr-namespace:YourProject"
  6. xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
  7. StartupUri="MainWindow.xaml">
  8. <Application.Resources>
  9. <ResourceDictionary>
  10. <ResourceDictionary.MergedDictionaries>
  11. <materialDesign:BundledTheme
  12. BaseTheme="Light"
  13. PrimaryColor="DeepPurple"
  14. SecondaryColor="Lime" />
  15. <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
  16. </ResourceDictionary.MergedDictionaries>
  17. </ResourceDictionary>
  18. </Application.Resources>
  19. </Application>
<Window
    x:Class="YourProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:YourProject"
    xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="800"
    Height="450"
    Background="{DynamicResource MaterialDesignPaper}"
    FontFamily="{DynamicResource MaterialDesignFont}"
    TextElement.FontSize="13"
    TextElement.FontWeight="Regular"
    TextElement.Foreground="{DynamicResource MaterialDesignBody}"
    TextOptions.TextFormattingMode="Ideal"
    TextOptions.TextRenderingMode="Auto"
    mc:Ignorable="d">
    <Grid>
        <StackPanel>
            <materialDesign:Card Margin="16" Padding="32">
                <TextBlock Style="{DynamicResource MaterialDesignHeadline6TextBlock}">My First Material Design App</TextBlock>
            </materialDesign:Card>
        </StackPanel>
    </Grid>
</Window>

4.Enjoy It

后续的使用,如果不太熟可以直接使用demo程序中代码复制,对于比较熟悉的就可以自己上手了。

二、EPPlus (Excel读写)

1.创建项目

2.nuget安装EPPlus

3.使用

这里使用最简单的示例

using OfficeOpenXml;
using System.IO;

//以下为使用代码
//下面这一行为非商业版声明,不写的话会报异常
 ExcelPackage.LicenseContext = LicenseContext.NonCommercial;

FileInfo newFile = new FileInfo(@"d:\test.xlsx");
if (newFile.Exists)
{
    newFile.Delete();
    newFile = new  FileInfo(@"d:\test.xlsx");
}
using (ExcelPackage package = new ExcelPackage(newFile))
{
    ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("test");
    worksheet.Cells[1, 1].Value = "名称";
    worksheet.Cells[1, 2].Value = "价格";
    worksheet.Cells[1, 3].Value = "销量";

    worksheet.Cells[2, 1].Value = "大米";
    worksheet.Cells[2, 2].Value = 56;
    worksheet.Cells[2, 3].Value = 100;

    worksheet.Cells[3, 1].Value = "玉米";
    worksheet.Cells[3, 2].Value = 45;
    worksheet.Cells[3, 3].Value = 150;

    worksheet.Cells[4, 1].Value = "小米";
    worksheet.Cells[4, 2].Value = 38;
    worksheet.Cells[4, 3].Value = 130;

    worksheet.Cells[5, 1].Value = "糯米";
    worksheet.Cells[5, 2].Value = 22;
    worksheet.Cells[5, 3].Value = 200;

    package.Save();
}

4.其他相关链接

EPPlus中文资料目前感觉不太多,收集了一些可以以后参考

博客园学习资料

导出EXCEL之EPPlus使用教程(基本介绍、样式设置、图表设置、其他设置)
https://www.cnblogs.com/rumeng/p/3785748.html

CSDN

C#读写Excel的4种方案(OpenXml、NPOI、EPPlus、Spire.Office for .NET)
链接:https://www.csdn.net/tags/NtzaEg0sMjQ5NTctYmxvZwO0O0OO0O0O.html 原文内容在项目中需要使用C#读写Excel,每天定时将数据输出到Excel表格中。在参考了很多的方案后,找到了4个常用的方案,并提供了一些小Demo。更多功能和示例可以参考官方文档。
1、Microsoft.Office.Interop.Excel:不推荐使用。
2、OpenXml:微软官方提供的SDK。
3、NPOI:第三方开源框架,口碑很好。
4、EPPlus:只能用于读写Excel,笔者目前使用最多。
5、Spire.Office for .NET:商业解决方案,功能完善强大。免费版功能也够用。


IDE:Visual Studio 2019 和 Visual Studio 2022
框架:.NET Framework 4.8 和 .NET 6.0


1、Microsoft.Office.Interop.Excel
最原始的操作库,兼容性一般,偶尔会出现内存泄漏和文件无法解除占用的问题(也可能是我太菜),不推荐使用。
2、OpenXml
GitHub:GitHub - OfficeDev/Open-XML-SDK: Open XML SDK by Microsoft
NuGet:NuGet Gallery | DocumentFormat.OpenXml 2.14.0
微软官方提供的一个SDK用于读写Office。
官方文档:Welcome to the Open XML SDK 2.5 for Office | Microsoft Docs
3、NPOI
在Java中,有Apache POI这样一个API用于操作Office。
Java POI:Apache POI - the Java API for Microsoft Documents
在C#中,有开源免费的NPOI。虽然不知道二者有什么关系,但总感觉很相似。
GitHub:GitHub - nissl-lab/npoi
NuGet:NuGet Gallery | NPOI 2.5.5
使用方法:
public void TestNPOI() { string sourceFile = @”D:\sourceFile.xlsx”; string targetFile = @”D:\targetFile.xlsx”; IWorkbook workbook = new XSSFWorkbook(sourceFile); ISheet sheet1 = workbook.GetSheet(“Sheet1”); sheet1.CreateRow(0).CreateCell(0).SetCellValue(1); sheet1.CreateRow(1).CreateCell(0).SetCellValue(2); sheet1.CreateRow(2).CreateCell(0).SetCellValue(3); FileStream fs = new FileStream(targetFile, FileMode.Create); workbook.Write(fs); workbook.Close(); }
NPOI在使用过程中有些不习惯,特别是在设置单元格内容时,和传统的思维方式不太一样。
4、EPPlus
官方网站:Excel spreadsheet library for .NET Framework/Core - EPPlus Software
GitHub:EPPlus Software · GitHub
NuGet:NuGet Gallery | EPPlus 5.8.3
EPPlus通过LicenseContext来区分商业应用(Commercial)和非商业应用(NonCommercial)。
使用方法:
public void TestEPPlus() { string sourceFile = @”D:\sourceFile.xlsx”; string targetFile = @”D:\targetFile.xlsx”; ExcelPackage.LicenseContext = LicenseContext.NonCommercial;//指明非商业应用 ExcelPackage package = new ExcelPackage(sourceFile);//加载Excel工作簿 ExcelWorksheet sheet1 = package.Workbook.Worksheets[“Sheet1”];//读取工作簿中名为”Sheet1”的工作表 sheet1.Cells[1, 1].Value = “A”;//设置单元格内容 sheet1.Cells[2, 2].Value = “B”; sheet1.Cells[3, 3].Value = “C”; sheet1.Cells[1, 2].Value = “1”; sheet1.Cells[2, 2].Value = “2”; sheet1.Cells[3, 2].Value = “3”; //package.Save();//将更改保存到原文件 package.SaveAs(targetFile);//将更改保存到新的文件,类似于另存为 }
笔者目前使用最多的框架,操作简单,符合使用习惯。但是EPPlus只能用于读写Excel,不能读写Word等其他文件。
5、Spire.Office for .NET
官方网站:C#/VB.Net Excel, Word, PowerPoint, PDF Component
GitHub:eiceblue (E-iceblue Product Family) · GitHub
NuGet:NuGet Gallery | FreeSpire.Office 4.3.1
Spire.Office提供了一整套的Office解决方案,可以读写、展示Word、Excel、PDF等。分为收费版和免费版。
使用方法:
public void TestSpireOffice() { string sourceFile = @”D:\sourceFile.xlsx”; string targetFile = @”D:\targetFile.xlsx”; Workbook workbook = new Workbook(); workbook.LoadFromFile(sourceFile);//加载Excel工作簿 Worksheet sheet1 = workbook.Worksheets[“Sheet1”];//读取工作簿中名为”Sheet1”的工作表 sheet1.SetCellValue(1, 1, “A”);//设置单元格内容 sheet1.SetCellValue(2, 1, “B”); sheet1.SetCellValue(3, 1, “C”); sheet1.SetCellValue(1, 2, “1”); sheet1.SetCellValue(2, 2, “2”); sheet1.SetCellValue(3, 2, “3”); workbook.Save();//将更改保存到原文件 workbook.SaveToFile(targetFile);//将更改保存到新的文件,类似于另存为 }
Spire.Office的收费版可以在WinForm中直接展示Word、Excel、PDF文件内容,但是免费版不能展示Excel。一种替代方式是先将Excel转换成PDF,再展示PDF,但这种方案只能展示前三页。


总结:
以上介绍的5中方案中,笔者最常使用的是EPPlusSpire.Office。毕竟是商业软件,体验确实不一样。免费版功能够用即可。
如果只是需要读写Excel,那么EPPlus非常方便而且符合使用习惯。
如果需要在WinForm中展示,那么Spire.Office可能是唯一选择。
如果需要读写多种Word、Excel等Office文件,OpenXml和NPOI也是不错的选择。

还有其他网站也介绍了许多关于C#读写Office的各种方法:
OpenXml:Read/Write Excel file using C# .NET Core | TheCodeBuzz
NPOI:Read and Write Excel file in C# .NET Core using NPOI | TheCodeBuzz
EPPlus:Read/Write Excel file in C# .NET Core using EPPlus | TheCodeBuzz