如何在Linux环境下配置.NET Core NPOI并实现Word、Excel文件生成示例?
- 内容介绍
- 文章标签
- 相关推荐
本文共计328个文字,预计阅读时间需要2分钟。
在.NET Core中,使用NPOI安装和配置方法如下,并生成Word和Excel文件示例代码:
安装NPOI
shelldotnet add package NPOI
配置NPOI
确保你的项目中已经添加了NPOI包。
生成Word文件示例
csharpusing NPOI.XWPF.UserModel;using System;using System.IO;
class Program{ static void Main() { // 创建Word文档 XWPFDocument document=new XWPFDocument();
// 添加 XWPFParagraph title=document.CreateParagraph(); XWPFRun titleRun=title.CreateRun(); titleRun.Text=Hello, Word!; titleRun.SetFontFamily(Arial); titleRun.SetFontSize(24);
// 添加内容 XWPFParagraph content=document.CreateParagraph(); XWPFRun contentRun=content.CreateRun(); contentRun.Text=This is a sample Word document created using NPOI.; contentRun.SetFontFamily(Arial); contentRun.SetFontSize(12);
// 保存文档 using (FileStream fileStream=File.Create(example.docx)) { document.Write(fileStream); } }}
生成Excel文件示例
csharpusing NPOI.SS.UserModel;using NPOI.XSSF.UserModel;using System;using System.IO;
class Program{ static void Main() { // 创建Excel文档 IWorkbook workbook=new XSSFWorkbook();
// 创建工作表 ISheet sheet=workbook.CreateSheet(Sheet1);
// 创建行和单元格 IRow row=sheet.CreateRow(0); ICell cell=row.CreateCell(0); cell.SetCellValue(Hello, Excel!);
// 保存文档 using (FileStream fileStream=File.Create(example.xlsx)) { workbook.Write(fileStream); } }}
Linux上配置
在Linux上,确保你的.NET Core SDK已经安装,然后按照上述步骤在命令行中执行。
shelldotnet add package NPOI
确保你的环境变量和.NET Core SDK配置正确,以便在Linux上编译和运行.NET Core应用程序。
本文主要.NET Core中,使用NPOI的安装引用配置方法,和生成Word和Excel文件的示例代码,以及在Linux上使用的配置方法,
原文地址:.NET Core NPOI Linux上配置使用及生成Word和Excel文件示例代码
本文共计328个文字,预计阅读时间需要2分钟。
在.NET Core中,使用NPOI安装和配置方法如下,并生成Word和Excel文件示例代码:
安装NPOI
shelldotnet add package NPOI
配置NPOI
确保你的项目中已经添加了NPOI包。
生成Word文件示例
csharpusing NPOI.XWPF.UserModel;using System;using System.IO;
class Program{ static void Main() { // 创建Word文档 XWPFDocument document=new XWPFDocument();
// 添加 XWPFParagraph title=document.CreateParagraph(); XWPFRun titleRun=title.CreateRun(); titleRun.Text=Hello, Word!; titleRun.SetFontFamily(Arial); titleRun.SetFontSize(24);
// 添加内容 XWPFParagraph content=document.CreateParagraph(); XWPFRun contentRun=content.CreateRun(); contentRun.Text=This is a sample Word document created using NPOI.; contentRun.SetFontFamily(Arial); contentRun.SetFontSize(12);
// 保存文档 using (FileStream fileStream=File.Create(example.docx)) { document.Write(fileStream); } }}
生成Excel文件示例
csharpusing NPOI.SS.UserModel;using NPOI.XSSF.UserModel;using System;using System.IO;
class Program{ static void Main() { // 创建Excel文档 IWorkbook workbook=new XSSFWorkbook();
// 创建工作表 ISheet sheet=workbook.CreateSheet(Sheet1);
// 创建行和单元格 IRow row=sheet.CreateRow(0); ICell cell=row.CreateCell(0); cell.SetCellValue(Hello, Excel!);
// 保存文档 using (FileStream fileStream=File.Create(example.xlsx)) { workbook.Write(fileStream); } }}
Linux上配置
在Linux上,确保你的.NET Core SDK已经安装,然后按照上述步骤在命令行中执行。
shelldotnet add package NPOI
确保你的环境变量和.NET Core SDK配置正确,以便在Linux上编译和运行.NET Core应用程序。
本文主要.NET Core中,使用NPOI的安装引用配置方法,和生成Word和Excel文件的示例代码,以及在Linux上使用的配置方法,
原文地址:.NET Core NPOI Linux上配置使用及生成Word和Excel文件示例代码

