【办公软件】C# NPOI 操作Excel 案例

文章目录

    • 1、加入NPOI 程序集,使用nuget添加程序集
    • 2、引用NPOI程序集
    • 3、设置表格样式
    • 4、excel加载图片
    • 5、导出excel


1、加入NPOI 程序集,使用nuget添加程序集

在这里插入图片描述

2、引用NPOI程序集

private IWorkbook ExportExcel(PrintQuotationOrderViewModel model){//if (model == null) return string.Empty;string tempDirPath = Server.MapPath("/Templates/Excel/");if (!Directory.Exists(tempDirPath)){Directory.CreateDirectory(tempDirPath);}IWorkbook workbook;string excelTempPath = tempDirPath + "quotaExcelTemp-new.xls";//加载excel模板using (FileStream fs = new FileStream(excelTempPath, FileMode.Open, FileAccess.Read)){//XSSFWorkbook 适用XLSX格式,HSSFWorkbook 适用XLS格式workbook = new HSSFWorkbook(fs);}ISheet sheet = workbook.GetSheetAt(0);sheet.GetRow(7).GetCell(1).SetCellValue(model.QuotationOrder.QuotedOn.ToString("yyyy-MM-dd"));sheet.GetRow(7).GetCell(6).SetCellValue(model.QuotationOrder.Number);sheet.GetRow(7).GetCell(9).SetCellValue(model.QuotationOrder.CustomerPurchaseNumber);//甲方sheet.GetRow(8).GetCell(1).SetCellValue(model.QuotationOrder.Buyer.Company.Name);sheet.GetRow(9).GetCell(1).SetCellValue(model.QuotationOrder.Buyer.Name);sheet.GetRow(10).GetCell(1).SetCellValue(model.QuotationOrder.Buyer.Email);sheet.GetRow(11).GetCell(1).SetCellValue(model.QuotationOrder.Receiver.Mobile);sheet.GetRow(12).GetCell(1).SetCellValue(model.QuotationOrder.Receiver.Address);//乙方sheet.GetRow(8).GetCell(8).SetCellValue("XXXXX有限公司");ICellStyle cstyle = workbook.CreateCellStyle();cstyle.Alignment = HorizontalAlignment.Left;sheet.GetRow(8).GetCell(8).CellStyle = cstyle;sheet.GetRow(9).GetCell(8).SetCellValue(model.QuotationOrder.SalesmanName);sheet.GetRow(9).GetCell(8).CellStyle = cstyle;sheet.GetRow(10).GetCell(8).SetCellValue(model.QuotationOrder.Salesman.Mobile);sheet.GetRow(10).GetCell(8).CellStyle = cstyle;sheet.GetRow(11).GetCell(8).SetCellValue(model.QuotationOrder.Salesman.Email);sheet.GetRow(11).GetCell(8).CellStyle = cstyle;int count = model.QuotationItems.Count;for (int i = 0; i < count; i++){//设置列头的单元格样式HSSFCellStyle cellStyle = workbook.CreateCellStyle() as HSSFCellStyle;IRow row = sheet.CopyRow(1, 15 + i);ICell cell = row.CreateCell(0);cell.SetCellValue((i + 1));ICellStyle style1 = SetCellStyle((HSSFWorkbook)workbook, HorizontalAlignment.Left);cell.CellStyle = style1;cell = row.CreateCell(1);cell.SetCellValue(model.QuotationItems[i].Product.Name);cell.CellStyle = style1;cell = row.CreateCell(2);cell.CellStyle = style1;//合并单元格CellRangeAddress region = new CellRangeAddress(15 + i, 15 + i, 1, 2);sheet.AddMergedRegion(region);cell = row.CreateCell(3);cell.CellStyle = style1;cell.SetCellValue(model.QuotationItems[i].CustomCode);cell = row.CreateCell(4);cell.CellStyle = style1;cell.SetCellValue(model.QuotationItems[i].Product.Code);cell = row.CreateCell(5);cell.CellStyle = style1;cell.SetCellValue("PCS");cell = row.CreateCell(6);cell.CellStyle = style1;cell.SetCellValue(model.QuotationItems[i].Quantity);cell = row.CreateCell(7);cell.CellStyle = style1;cell.SetCellValue(model.QuotationItems[i].Quotation.DispatchDays >= 0 ? ((int)model.QuotationItems[i].Quotation.DispatchDays).ToString() : "");cell = row.CreateCell(8);cell.CellStyle = style1;cell.SetCellValue(model.QuotationItems[i].Quotation.UnitPriceWithTax >= 0 ? ((decimal)model.QuotationItems[i].Quotation.UnitPriceWithTax).ToString("f2") : "");cell = row.CreateCell(9);cell.CellStyle = style1;cell.SetCellValue(model.QuotationItems[i].Quotation.SubtotalWithTax.ToString("f2"));cell = row.CreateCell(10);cell.CellStyle = style1;cell.SetCellValue(model.QuotationItems[i].Remark);}sheet.GetRow(15 + count).GetCell(1).SetCellValue(model.QuotationOrder.Shipping.Amount.ToString("f2"));sheet.GetRow(15 + count).GetCell(4).SetCellValue(model.QuotationOrder.TotalWithTax.ToString("f2"));sheet.GetRow(15 + count).GetCell(7).SetCellValue(model.QuotationOrder.TotalWithTaxInChinese);sheet.GetRow(20 + count).GetCell(2).SetCellValue(model.Payment);return workbook;}

3、设置表格样式

/// <summary>/// 给Excel添加边框/// </summary>private  ICellStyle SetCellStyle(HSSFWorkbook hssfworkbook, HorizontalAlignment ha){ICellStyle cellstyle = hssfworkbook.CreateCellStyle();cellstyle.Alignment = ha;//有边框cellstyle.BorderBottom = BorderStyle.Thin;cellstyle.BorderLeft = BorderStyle.Thin;cellstyle.BorderRight = BorderStyle.Thin;cellstyle.BorderTop = BorderStyle.Thin;return cellstyle;}

4、excel加载图片

  HSSFPatriarch patriarch = (HSSFPatriarch)sheet.DrawingPatriarch;HSSFClientAnchor anchor = new HSSFClientAnchor(10, 10, 0, 60, 7, 26 + count, 11, 38 + count);HSSFPicture picture = (HSSFPicture)patriarch.CreatePicture(anchor, LoadImage(tempDirPath + "1.png", (HSSFWorkbook)workbook));

LoadImage 方法

private int LoadImage(string path, HSSFWorkbook wb){FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);byte[] buffer = new byte[file.Length];file.Read(buffer, 0, (int)file.Length);return wb.AddPicture(buffer, PictureType.PNG);}

5、导出excel

var stream = new MemoryStream();
var work = ExportExcel(printQuotationOrderViewModel);work.Write(stream);//mvc代码return File(stream.GetBuffer(), "application/vnd.ms-excel", quotedOrderModel.Number + ".xls");    

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/234554.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

虾皮电商 电商平台:东南亚最大的跨境电商平台

虾皮电商&#xff08;Shopee&#xff09;是新加坡的一家跨境电商平台&#xff0c;隶属于Sea Group公司。虾皮电商以其强大的社交功能和便捷的物流服务而闻名&#xff0c;为卖家和买家提供了出色的交易体验。本文将介绍虾皮电商的发展历程、优势以及为卖家提供的一站式解决方案。…

JVM征服面试篇-亿及流量系统设计(学习笔记)

一、如何拆解亿级流量系统-百万级结算系统如何设置JVM 1.先确认如下问题&#xff1a; 2.第一步&#xff1a;确定业务背景和核心流程 3.第二步&#xff1a;确认系统的压力在哪里 3.第三步&#xff1a;确定QPS 4.第四步&#xff1a;确定单笔订单耗时&#xff0c;寻找性能瓶颈 5.…

金蝶报表二开

本案例描述&#xff1a; 折旧明细报表中加入字段&#xff1a;存放地点、成本中心部门、使用人组织三个字段。 参考社区案例&#xff1a;报表二次开发添加自定义字段的指导方案 步骤&#xff1a; 1、加入报表插件 继承原报表的类。重写BuilderReportSqlAndTempTable、GetRe…

MyBatis 运行原理

MyBatis框架在操作数据库时&#xff0c;大体经过了8个步骤&#xff1a; 1.读取 MyBatis 配置文件&#xff1a;mybatis-config.xml 为 MyBatis 的全局配置文件&#xff0c;配置了 MyBatis 的运行环境等信息&#xff0c;例如数据库连接信息。 2.加载映射文件&#xff1a;映射文…

JDBC编程(主要针对其流程)

JDBC编程 注&#xff1a;在本篇博客中&#xff0c;使用的数据库是mysql&#xff01;&#xff01;&#xff01; 一、JDBC编程六步 1、注册驱动 这步就是在告诉Java程序&#xff0c;即将要连接的为哪个品牌的数据库&#xff0c; 这里有两种方法 ① //就是使用一个了多态&am…

Dokit 开源库:简化 Android 应用开发的利器

Dokit 开源库&#xff1a;简化 Android 应用开发的利器 一、Dokit 简介二、Dokit 功能三、Dokit 使用3.1 DoKit Android 最新版本3.2 DoKit Android 接入步骤 四、总结 在 Android 应用开发过程中&#xff0c;我们经常需要处理调试、性能优化和用户体验等方面的问题。然而&…

stm32学习总结:4、Proteus8+STM32CubeMX+MDK仿真串口收发

stm32学习总结&#xff1a;4、Proteus8STM32CubeMXMDK仿真串口收发 文章目录 stm32学习总结&#xff1a;4、Proteus8STM32CubeMXMDK仿真串口收发一、前言二、资料收集三、STM32CubeMX配置串口1、配置开启USART12、设置usart中断优先级3、配置外设独立生成.c和.h 四、MDK串口收发…

期货股市联动(期股联动助推资本市场上扬)

期股联动——期货股市助推资本市场上扬 随着我国资本市场的不断发展&#xff0c;期货和股票这两个市场也在逐渐紧密地联系起来。期货和股票的相互作用是一种“期股联动”&#xff0c;它能够促进资本市场的上扬。 期货与股票市场 期货市场是一种标准化的场外交易市场&#xf…

【jvm从入门到实战】(十) 实战篇-内存调优

内存溢出和内存泄漏&#xff1a;在Java中如果不再使用一个对象&#xff0c;但是该对象依然在GC ROOT的引用链上&#xff0c;这个对象就不会被垃圾回收器回收&#xff0c;这种情况就称之为内存泄漏。内存泄漏绝大多数情况都是由堆内存泄漏引起的。少量的内存泄漏可以容忍&#x…

mysql:查看尝试连接mysql服务器的次数(包含成功和失败的尝试)

运行命令show global status like Connections;查看尝试连接mysql服务器的次数&#xff08;包含成功和失败的尝试&#xff09;。 例如&#xff1a;

华为云Stack 8.X 流量模型分析(二)

二、流量模型分析相关知识 1.vNIC ​ 虚拟网络接口卡(vNIC)是基于主机物理 NIC 的虚拟网络接口。每个主机可以有多个 NIC&#xff0c;每个 NIC 可以是多个 vNIC 的基础。 ​ 将 vNIC 附加到虚拟机时&#xff0c;Red Hat Virtualization Manager 会在虚拟机之间创建多个关联的…

七:爬虫-数据解析之正则表达式

七&#xff1a;正则表达式概述 正则表达式&#xff0c;又称规则表达式,&#xff08;Regular Expression&#xff0c;在代码中常简写为regex、regexp或RE&#xff09;&#xff0c;是一种文本模式&#xff0c;包括普通字符&#xff08;例如&#xff0c;a 到 z 之间的字母&#xf…

如何下载知网论文、专利的PDF格式

知网的论文格式有其特有的格式&#xff1a;CAJ。将CAJ格式转化为Word或者PDF非常麻烦&#xff0c;且会出现乱码的情况&#xff0c;直接用知网官方的CAJ浏览器也不太方便。为此&#xff0c;困扰了许久。 其实&#xff0c;知网可以直接下载PDF格式&#xff0c;只需在浏览器上安装…

Python启动提示ERROR: [WinError 10013] ��һ�ַ���Ȩ�޲�����ķ�ʽ����һ�������׽��ֵij��ԡ�

启动项目后&#xff1a; 其实就是默认的5000端口号被占用&#xff0c;端口号冲突 &#xff0c;改下端口即可。 点击&#xff1a;编辑配置 空格加 --port5001 启动成功&#xff0c;点击下图标记位置

【STM32工具篇】使用CLion开发STM32

本文主要记录使用CLion开发STM32&#xff0c;并调试相关功能 使用的CLion版本&#xff1a;2023.3.1 CLion嵌入式配置教程&#xff1a;STM32CubeMX项目 |CLion 文档 (jetbrains.com) OpenOCD官网下载&#xff1a;Download OpenOCD for Windows (gnutoolchains.com) GNU ARM工…

【C++】理解string类的核心理念(实现一个自己的string类)

目录 一、引言 二、自我实现 1.成员变量的读写 2.构造与析构 3.迭代器 4.插入字符或字符串 尾插 中间插入 5.删除字符或子字符串 6.查找字符或子串 7.获取子串 三、补充 一、引言 实现自己的 string 类是学习 C 语言和面向对象编程的一个好方法。通过编写一个简单的…

浅析 ArrayList

ArrayList是一个使用List接口实现的Java类。顾名思义&#xff0c;Java ArrayList提供了动态数组的功能&#xff0c;其中数组的大小不是固定的。它实现了所有可选的列表操作&#xff0c;并允许所有元素&#xff0c;包括null。 ArrayList 继承于 AbstractList &#xff0c;实现了…

《数据结构、算法与应用C++语言描述》- 最小输者树模板的C++实现

输者树 完整可编译运行代码见&#xff1a;Github::Data-Structures-Algorithms-and-Applications/_31loserTree 输者树&#xff1a;每一个内部节点所记录的都是比赛的输者&#xff0c;晋级的节点记录在边上。本文中&#xff0c;赢者是分数较低的那个&#xff0c;输者是分数高…

虾皮电商申请:一站式开店指南

随着跨境电商的快速发展&#xff0c;越来越多的商家开始意识到东南亚市场的潜力。虾皮电商&#xff08;Shopee&#xff09;作为东南亚地区最大的电商平台之一&#xff0c;为商家提供了一个开拓市场的机会。本文将详细介绍如何在虾皮电商平台上开店&#xff0c;并给出一些建议来…

STM32/STM8资源节约主义编程方式

STM32/STM8资源节约主义编程方式 在小资源芯片进行代码设计时&#xff0c;如STM32C0系列&#xff0c;STM8系列&#xff0c;因为官方库本身要包含各种场景应用特征的支持&#xff0c;所以会有一些冗余的代码占用更多FLASH空间。当需要实现资源占用最简化设计方式时&#xff0c;…