1. 新建一个.net core 3.0控制台应用程序,添加引用DotNetCore.NPOI, Version: 1.2.2
2. 创建一个.xlsx文件
class Program
{
static void Main(string[] args)
{
var workbook = new XSSFWorkbook();
var sheet = workbook.CreateSheet("TestSheet");
sheet.CreateRow(0).CreateCell(0).SetCellValue("Hello World");
var path = @"D:\Test\Test.xlsx";
using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write))
{
workbook.Write(stream);
}
}
}
3. 创建一个.xls文件
class Program
{
static void Main(string[] args)
{
var workbook = new HSSFWorkbook();
var sheet = workbook.CreateSheet("TestSheet");
sheet.CreateRow(0).CreateCell(0).SetCellValue("Hello World");
var path = @"D:\Test\Test.xls";
using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write))
{
workbook.Write(stream);
}
}
}
Result:
在引入NPOI的同时,还引入了System.Drawing.Common.dll, Microsoft.Win32.SystemEvents.dll, ISCharpCode.SHarpZipLib.dll.
DotNetCore.NPOI的依赖:
ICSharpCode.SharpZipLib, Version=1.2.0.246, Culture=neutral, PublicKeyToken=1b03e6acf1164f73
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
System.Drawing.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
System.Text.Encoding.CodePages, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a