首页 .Net .NET Core NPOI Linux上配置使用及生成Word和Excel文件示例代码

.NET Core NPOI Linux上配置使用及生成Word和Excel文件示例代码

1、安装引用DotNetCore.NPOI

1)使用Nuget界面管理器

搜索"DotNetCore.NPOI",在列表中找到它,点击"安装"

相关文档VS(Visual Studio)中Nuget的使用

2)使用Package Manager命令安装

PM> Install-Package DotNetCore.NPOI

3)使用.NET CLI命令安装

> dotnet add TodoApi.csproj package DotNetCore.NPOI

2、生成Excel文件代码

var newFile = @"newbook.core.xlsx";
using (var fs = new FileStream(newFile, FileMode.Create, FileAccess.Write)) {
IWorkbook workbook = new XSSFWorkbook();
ISheet sheet1 = workbook.CreateSheet("Sheet1");
sheet1.AddMergedRegion(new CellRangeAddress(0, 0, 0, 10));
var rowIndex = 0;
IRow row = sheet1.CreateRow(rowIndex);
row.Height = 30 * 80;
row.CreateCell(0).SetCellValue("this is content");
sheet1.AutoSizeColumn(0);
rowIndex++;
var sheet2 = workbook.CreateSheet("Sheet2");
var > > > var > > > var cell2 = sheet2.CreateRow(0).CreateCell(0);
cell2.CellStyle = >1).CreateCell(0);
cell2.CellStyle = >@"newbook.core.docx";
using (var fs = new FileStream(newFile2, FileMode.Create, FileAccess.Write)) {
XWPFDocument doc = new XWPFDocument();
var p0 = doc.CreateParagraph();
p0.Alignment = ParagraphAlignment.CENTER;
XWPFRun r0 = p0.CreateRun();
r0.FontFamily = "microsoft yahei";
r0.FontSize = 18;
r0.IsBold = true;
r0.SetText("This is title");
var p1 = doc.CreateParagraph();
p1.Alignment = ParagraphAlignment.LEFT;
p1.IndentationFirstLine = 500;
XWPFRun r1 = p1.CreateRun();
r1.FontFamily = "·ÂËÎ";
r1.FontSize = 12;
r1.IsBold = true;
r1.SetText("This is content, content content content content content content content content content");
doc.Write(fs);
}

4、配置Linux上使用NPOI

在Linux上,您需要安装 libgdiplus。从1.2.0开始,还需要libdl

1) Ubuntu 16.04及以上版本配置

apt-get install libgdiplus libc6-dev
cd /usr/lib
ln -s libgdiplus.so gdiplus.dll

2) Fedora 23及以上版本配置

dnf install libgdiplus
cd /usr/lib64/
ln -s libgdiplus.so.0 gdiplus.dll

3) CentOS 7及以上版本配置

yum install autoconf automake libtool
yum install freetype-devel fontconfig libXft-devel
yum install libjpeg-turbo-devel libpng-devel giflib-devel libtiff-devel libexif-devel
yum install glib2-devel cairo-devel
git clone https://github.com/mono/libgdiplus
cd libgdiplus
./autogen.sh
make
make install
cd /usr/lib64/
ln -s /usr/local/lib/libgdiplus.so gdiplus.dll

4) Docker中配置

Alpine:

# base sdk-alpine/aspnetcore-runtime-alpine images
RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories
RUN apk --update add libgdiplus

Debian:

FROM microsoft/dotnet:2.1-aspnetcore-runtime
RUN apt-get update && apt-get install -y libgdiplus libc6-dev && ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll

相关文档:

https://github.com/dotnetcore/NPOI

.NET Core Asposeword(.doc,docx)文件加水印

.NET Core Asposeword(.doc,docx)转成pdf文件

.NET(C#)/.NET Core 不安装MS Office实现创建excel(.XLS和.XLSX)文件

.Net Core Aspose.Cells创建和读取excel(.xls,.xlsx)数据

特别声明:本站部分内容收集于互联网是出于更直观传递信息的目的。该内容版权归原作者所有,并不代表本站赞同其观点和对其真实性负责。如该内容涉及任何第三方合法权利,请及时与824310991@qq.com联系,我们会及时反馈并处理完毕。