DataExcel控件读取和保存excel xlsx 格式文件
需要引用NPOI库 https://github.com/dotnetcore/NPOI
调用Read 函数将excel读取到dataexcel控件
调用Save 函数将dataexcel控件文件保存为excel文件
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;namespace Feng.DataTool
{public class ExcelTools{public static void Save(Feng.Excel.DataExcel grid, string file){HSSFWorkbook hssfworkbook = new HSSFWorkbook();ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1");List<CellStyleTemp> cellstyletemplist = new List<CellStyleTemp>();foreach (Feng.Excel.Interfaces.IRow gridrow in grid.Rows){if (gridrow.Index < 1)continue;IRow row = sheet1.CreateRow(gridrow.Index-1);row.Height = (short)(gridrow.Height * 20);foreach (Feng.Excel.Interfaces.IColumn column in grid.Columns){if (column.Index < 1)continue;Feng.Excel.Interfaces.ICell gridcell = gridrow[column];if (gridcell == null)continue;ushort width = (ushort)((column.Width) / 7 * 256);sheet1.SetColumnWidth(column.Index-1, width);ICell cell = row.CreateCell(column.Index-1);string text = gridcell.Text;cell.SetCellValue(text);bool bottomlinestylevisible = false;bool leftlinestylevisible = false;bool rightlinestylevisible = false;bool toplinestylevisible = false;bool isstrikeout = false;bool isbold = false;double fontheightinpoints = 29;string fontname = string.Empty;if (gridcell.BorderStyle != null){if (gridcell.BorderStyle.BottomLineStyle != null){if (gridcell.BorderStyle.BottomLineStyle.Visible){bottomlinestylevisible = true;}}if (gridcell.BorderStyle.LeftLineStyle != null){if (gridcell.BorderStyle.LeftLineStyle.Visible){leftlinestylevisible = true;}}if (gridcell.BorderStyle.RightLineStyle != null){if (gridcell.BorderStyle.RightLineStyle.Visible){rightlinestylevisible = true;}}if (gridcell.BorderStyle.TopLineStyle != null){if (gridcell.BorderStyle.TopLineStyle.Visible){toplinestylevisible = true;}}}isstrikeout = gridcell.Font.Strikeout; isbold = gridcell.Font.Bold;fontheightinpoints = gridcell.Font.Size;fontname = gridcell.Font.Name;int alignment = GetHAlignment(gridcell.HorizontalAlignment);int verticalalignment = GetVAlignment(gridcell.VerticalAlignment);ICellStyle style = GetCellStyle(hssfworkbook, cellstyletemplist, bottomlinestylevisible, leftlinestylevisible, rightlinestylevisible, toplinestylevisible, isstrikeout, isbold, fontheightinpoints, fontname, alignment, verticalalignment);cell.CellStyle = style;}}foreach (Feng.Excel.Interfaces.IMergeCell item in grid.MergeCells){sheet1.AddMergedRegion(new CellRangeAddress(item.MinCell.Row.Index-1,item.MaxRowIndex - 1, item.MinCell.Column.Index - 1, item.MaxColumnIndex - 1));//sheet1.AddMergedRegion(new CellRangeAddress(2, 7, 2, 7));}FileStream stream = new FileStream(file, FileMode.Create);hssfworkbook.Write(stream);stream.Close();}public static int GetHAlignment(StringAlignment alignment){switch (alignment){case StringAlignment.Near:return 1; case StringAlignment.Center:return 2;case StringAlignment.Far:return 3;default:break;}return 0;}public static int GetVAlignment(StringAlignment alignment){switch (alignment){case StringAlignment.Near:return 0;case StringAlignment.Center:return 1;case StringAlignment.Far:return 2;default:break;}return 0;}public class CellStyleTemp{public CellStyleTemp(){}public bool BottomLineStyleVisible { get; set; }public bool LeftLineStyleVisible { get; set; }public bool RightLineStyleVisible { get; set; }public bool TopLineStyleVisible { get; set; }public bool IsStrikeout { get; set; }public bool IsBold { get; set; }public double FontHeightInPoints { get; set; }public string FontName { get; set; }public int Alignment { get; set; }public int VerticalAlignment { get; set; }public ICellStyle style { get; set; }}public static ICellStyle GetCellStyle(HSSFWorkbook hssfworkbook, List<CellStyleTemp> styles, bool bottomlinestylevisible, bool leftlinestylevisible, bool rightlinestylevisible, bool toplinestylevisible, bool isstrikeout, bool isbold, double fontheightinpoints, string fontname, int alignment, int verticalalignment){ foreach (CellStyleTemp item in styles){ if (item.BottomLineStyleVisible != bottomlinestylevisible){continue;}double upfontheightinpoints = fontheightinpoints + 0.1;double downfontheightinpoints = fontheightinpoints - 0.1;if (!((item.FontHeightInPoints > downfontheightinpoints) && (item.FontHeightInPoints < upfontheightinpoints))){continue;}if (item.FontName != fontname){continue;}if (item.IsBold != isbold){continue;}if (item.IsStrikeout != isstrikeout){continue;}if (item.LeftLineStyleVisible != leftlinestylevisible){continue;}if (item.RightLineStyleVisible != rightlinestylevisible){continue;}if (item.TopLineStyleVisible != toplinestylevisible){continue;}if (item.Alignment != alignment){continue;}if (item.VerticalAlignment != verticalalignment){continue;}return item.style;}CellStyleTemp cellStyleTemp = new CellStyleTemp();cellStyleTemp.BottomLineStyleVisible = bottomlinestylevisible;cellStyleTemp.FontHeightInPoints = fontheightinpoints;cellStyleTemp.FontName = fontname;cellStyleTemp.IsBold = isbold;cellStyleTemp.IsStrikeout = isstrikeout;cellStyleTemp.LeftLineStyleVisible = leftlinestylevisible;cellStyleTemp.RightLineStyleVisible = rightlinestylevisible;cellStyleTemp.TopLineStyleVisible = toplinestylevisible;cellStyleTemp.Alignment = alignment;cellStyleTemp.VerticalAlignment = verticalalignment;ICellStyle style = CreateCellStyle(hssfworkbook, bottomlinestylevisible, leftlinestylevisible, rightlinestylevisible, toplinestylevisible, isstrikeout, isbold, fontheightinpoints, fontname, alignment, verticalalignment);cellStyleTemp.style = style;styles.Add(cellStyleTemp);return cellStyleTemp.style;}public static ICellStyle CreateCellStyle(HSSFWorkbook hssfworkbook, bool BottomLineStyleVisible, bool LeftLineStyleVisible, bool RightLineStyleVisible, bool TopLineStyleVisible, bool IsStrikeout, bool IsBold, double FontHeightInPoints, string FontName, int alignment, int verticalalignment){ICellStyle style = hssfworkbook.CreateCellStyle();if (BottomLineStyleVisible){style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;}if (LeftLineStyleVisible){style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;}if (RightLineStyleVisible){style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;}if (TopLineStyleVisible){style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;}style.Alignment = (NPOI.SS.UserModel.HorizontalAlignment)alignment;style.VerticalAlignment = (NPOI.SS.UserModel.VerticalAlignment)verticalalignment;IFont font2 = hssfworkbook.CreateFont();font2.Color = HSSFColor.Black.Index;if (IsStrikeout){font2.IsStrikeout = true;}if (IsBold){font2.IsBold = true;}font2.FontHeightInPoints = FontHeightInPoints;font2.FontName = FontName;style.SetFont(font2);return style;}public static void Read(Feng.Excel.DataExcel grid, string file){FileStream stream = new FileStream(file, FileMode.Open);HSSFWorkbook hssfworkbook = new HSSFWorkbook(stream);stream.Close();ISheet sheet = hssfworkbook.GetSheetAt(0);int rowcount = sheet.LastRowNum;bool hassetcolumnwidth = false;for (int rowindex = 0; rowindex < rowcount; rowindex++){IRow row = sheet.GetRow(rowindex);if (row == null)continue;Feng.Excel.Interfaces.IRow gridrow = grid.GetRow(rowindex + 1);gridrow.Height = row.Height / 20;short columncount = row.LastCellNum;for (short columnindex = 0; columnindex < columncount; columnindex++){Feng.Excel.Interfaces.IColumn gridcolumn = grid.GetColumn(columnindex + 1);int width = sheet.GetColumnWidth(columnindex); gridcolumn.Width = (width / 256) * 7;ICell cell = row.GetCell(columnindex);if (cell == null)continue;Feng.Excel.Interfaces.ICell gridcell = grid.GetCell(gridrow.Index, gridcolumn.Index);gridcell.BorderStyle = new Excel.Styles.CellBorderStyle();try{if (cell.CellType == CellType.String){gridcell.Value = cell.StringCellValue;}if (cell.CellType == CellType.Numeric){gridcell.Value = cell.NumericCellValue;}if (cell.CellType == CellType.Boolean){gridcell.Value = cell.BooleanCellValue;}if (cell.CellType == CellType.Unknown){gridcell.Value = cell.StringCellValue;}if (cell.CellStyle.BorderBottom == NPOI.SS.UserModel.BorderStyle.Thin){gridcell.BorderStyle.BottomLineStyle = new Excel.Styles.LineStyle();gridcell.BorderStyle.BottomLineStyle.Visible = true;}if (cell.CellStyle.BorderLeft == NPOI.SS.UserModel.BorderStyle.Thin){gridcell.BorderStyle.LeftLineStyle = new Excel.Styles.LineStyle();gridcell.BorderStyle.LeftLineStyle.Visible = true;}if (cell.CellStyle.BorderRight == NPOI.SS.UserModel.BorderStyle.Thin){gridcell.BorderStyle.RightLineStyle = new Excel.Styles.LineStyle();gridcell.BorderStyle.RightLineStyle.Visible = true;}if (cell.CellStyle.BorderTop == NPOI.SS.UserModel.BorderStyle.Thin){gridcell.BorderStyle.TopLineStyle = new Excel.Styles.LineStyle();gridcell.BorderStyle.TopLineStyle.Visible = true;}}catch (Exception ex){Feng.Utils.TraceHelper.WriteTrace("SysTools", "ExcelTools", "Read", ex);}try{IFont font2 = cell.CellStyle.GetFont(hssfworkbook);System.Drawing.FontStyle fontstyle = System.Drawing.FontStyle.Regular;if (font2.IsStrikeout){fontstyle = fontstyle | System.Drawing.FontStyle.Strikeout;}if (font2.IsBold){fontstyle = fontstyle | System.Drawing.FontStyle.Bold;}font2.FontHeightInPoints = gridcell.Font.Size;font2.FontName = gridcell.Font.Name;gridcell.Font = new System.Drawing.Font(font2.FontName, (float)font2.FontHeightInPoints, fontstyle);}catch (Exception ex){Feng.Utils.TraceHelper.WriteTrace("SysTools", "ExcelTools", "Read", ex);}try{if (cell.CellStyle.Alignment == NPOI.SS.UserModel.HorizontalAlignment.Center){gridcell.HorizontalAlignment = StringAlignment.Center;}if (cell.CellStyle.Alignment == NPOI.SS.UserModel.HorizontalAlignment.Left){gridcell.HorizontalAlignment = StringAlignment.Near;}if (cell.CellStyle.Alignment == NPOI.SS.UserModel.HorizontalAlignment.Right){gridcell.HorizontalAlignment = StringAlignment.Far;}}catch (Exception ex){ Feng.Utils.TraceHelper.WriteTrace("SysTools", "ExcelTools", "Read", ex);}try{if (cell.CellStyle.VerticalAlignment == NPOI.SS.UserModel.VerticalAlignment.Center){gridcell.VerticalAlignment = StringAlignment.Center;}if (cell.CellStyle.VerticalAlignment == NPOI.SS.UserModel.VerticalAlignment.Top){gridcell.VerticalAlignment = StringAlignment.Near;}if (cell.CellStyle.VerticalAlignment == NPOI.SS.UserModel.VerticalAlignment.Bottom){gridcell.VerticalAlignment = StringAlignment.Far;}}catch (Exception ex){Feng.Utils.TraceHelper.WriteTrace("SysTools", "ExcelTools", "Read", ex);}}}List<CellRangeAddress> listmer = sheet.MergedRegions;if (listmer != null){foreach (CellRangeAddress item in listmer){grid.MergeCell(item.FirstRow + 1, item.FirstColumn + 1, item.LastRow + 1, item.LastColumn + 1);}}}}
}
相关文章:

DataExcel控件读取和保存excel xlsx 格式文件
需要引用NPOI库 https://github.com/dotnetcore/NPOI 调用Read 函数将excel读取到dataexcel控件 调用Save 函数将dataexcel控件文件保存为excel文件 using NPOI.HSSF.UserModel; using NPOI.HSSF.Util; using NPOI.SS.UserModel; using NPOI.SS.Util; using System; using …...

【JavaEE】CAS(Compare And Swap)操作
文章目录 什么是 CASCAS 的应用如何使用 CAS 操作实现自旋锁CAS 的 ABA 问题CAS 相关面试题 什么是 CAS CAS(Compare and Swap)是一种原子操作,用于在无锁情况下保证数据一致性的问题。它包含三个操作数——内存位置、预期原值及更新值。在执…...
第三章:最新版零基础学习 PYTHON 教程(第三节 - Python 运算符—Python 中的关系运算符)
关系运算符用于比较值。它根据条件返回 True 或 False。这些运算符也称为比较运算符。 操作员描述 句法> 大于:如果左操作数大于右操作数,则为 Truex > y...

【GDB】使用 GDB 自动画红黑树
阅读本文前需要的基础知识 用 python 扩展 gdb python 绘制 graphviz 使用 GDB 画红黑树 前面几节中介绍了 gdb 的 python 扩展,参考 用 python 扩展 gdb 并且 python 有 graphviz 模块,那么可以用 gdb 调用 python,在 python 中使用 grap…...

使用Vue3+elementPlus的Tree组件实现一个拖拽文件夹管理
文章目录 1、前言2、分析3、实现4、踩坑4.1、拖拽辅助线的坑4.2、数据的坑4.3、限制拖拽4.4、样式调整 1、前言 最近在做一个文件夹管理的功能,要实现一个树状的文件夹面板。里面包含两种元素,文件夹以及文件。交互要求如下: 创建、删除&am…...

小谈设计模式(7)—装饰模式
小谈设计模式(7)—装饰模式 专栏介绍专栏地址专栏介绍 装饰模式装饰模式角色Component(抽象组件)ConcreteComponent(具体组件)Decorator(抽象装饰器)ConcreteDecorator(具…...

nginx 多层代理 + k8s ingress 后端服务获取客户真实ip 配置
1.nginx http 七层代理 修改命令空间: namespace: nginx-ingress : configmap:nginx-configuration kubectl get cm nginx-configuration -n ingress-nginx -o yaml添加如上配置 compute-full-forwarded-for: “true” forwarded-for-header: X-Forwa…...

6种最常用的3D点云语义分割AI模型对比
由于增强现实/虚拟现实的发展及其在计算机视觉、自动驾驶和机器人领域的广泛应用,点云学习最近引起了人们的关注。 深度学习已成功用于解决 2D 视觉问题,然而,由于其处理面临独特的挑战,深度学习技术在点云上的使用仍处于起步阶段…...
UG NX二次开发(C#)-获取UI中选择对象的handle值
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 1、前言2、设计一个简单的UI界面3、创建工程项目4、测试结果1、前言 我在哔哩哔哩的视频中看到有人问我如何获取UI选择对象的Handle,本来想把Tag、Taggedobject、Handle三者的关系讲一下,然后看…...

win10,WSL的Ubuntu配python3.7手记
1.装linux 先在windows上安装WSL版本的Ubuntu Windows10系统安装Ubuntu子系统_哔哩哔哩_bilibili (WSL2什么的一直没搞清楚) 图形界面会出一些问题,注意勾选ccsm出的界面设置 win10安装Ubuntu16.04子系统,并开启桌面环境_win…...

02-Zookeeper实战
上一篇:01-Zookeeper特性与节点数据类型详解 1. zookeeper安装 Step1: 配置JAVA环境,检验环境: java -versionStep2: 下载解压 zookeeper wget https://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.5.8/apache-zookeepe…...

【C语言深入理解指针(1)】
1.内存和地址 1.1内存 在讲内存和地址之前,我们想有个⽣活中的案例: 假设有⼀栋宿舍楼,把你放在楼⾥,楼上有100个房间,但是房间没有编号,你的⼀个朋友来找你玩,如果想找到你,就得挨…...

模拟实现简单的通讯录
前言:生活中处处都会看到或是用到通讯录,今天我们就通过C语言来简单的模拟实现一下通讯录。 鸡汤:跨越山海,终见曙光! 链接:gitee仓库:代码链接 目录 主函数声明部分初始化通讯录实现扩容的函数增加通讯录所…...

rabbitMQ死信队列快速编写记录
文章目录 1.介绍1.1 什么是死信队列1.2 死信队列有什么用 2. 如何编码2.1 架构分析2.2 maven坐标2.3 工具类编写2.4 consumer1编写2.5 consumer2编写2.6 producer编写 3.整合springboot3.1 架构图3.2 maven坐标3.3 构建配置类,创建exchange,queue&#x…...
数位dp,338. 计数问题
338. 计数问题 - AcWing题库 给定两个整数 a 和 b,求 a 和 b 之间的所有数字中 0∼90∼9 的出现次数。 例如,a1024,b1032,则 a 和 b 之间共有 9 个数如下: 1024 1025 1026 1027 1028 1029 1030 1031 1032 其中 0 出…...
如何解决git clone http/https仓库失败(403错误)
本来不打算写这篇文章,但是后来又遇到这个问题忘了之前是怎么解决的了。 一般情况下,个人使用 GitHub 等平台时是使用 SSH 协议的,这样不光方便管理可访问用户,也保证了安全性。但是 GitHub 上仓库的 SSH 地址是要登陆才能看到&a…...

华为云云耀云服务器L实例评测 | 实例评测使用之硬件性能评测:华为云云耀云服务器下的硬件运行评测
华为云云耀云服务器L实例评测 | 实例评测使用之硬件性能评测:华为云云耀云服务器下的硬件运行评测 介绍华为云云耀云服务器 华为云云耀云服务器 (目前已经全新升级为 华为云云耀云服务器L实例) 华为云云耀云服务器是什么华为云云耀…...

Elasticsearch:使用 Elasticsearch 进行语义搜索
在数字时代,搜索引擎在通过浏览互联网上的大量可用信息来检索数据方面发挥着重要作用。 此方法涉及用户在搜索栏中输入特定术语或短语,期望搜索引擎返回与这些确切关键字匹配的结果。 虽然关键字搜索对于简化信息检索非常有价值,但它也有其局…...
JVM的主要组成及其作用
jvm主要组成部分有: 类加载器、运行时数据区 (内存结构)、执行引擎、本地接口库、垃圾回收机制 Java程序运行的时候,首先会通过类加载器把Java 代码转换成字节码。然后运行时数据区再将字节码加载到内存中,但字节码文件只是JVM 的一套指令集规范…...

会议AISTATS(Artificial Intelligence and Statistics) Latex模板参考文献引用问题
前言 在看AISTATS2024模板的时候,发现模板里面根本没有教怎么引用,要被气死了。 如下,引用(Cheesman, 1985)的时候,模板是自己手打上去的?而且模板提供的那三个引用,根本也没有Cheesman这个人,…...

Opencv中的addweighted函数
一.addweighted函数作用 addweighted()是OpenCV库中用于图像处理的函数,主要功能是将两个输入图像(尺寸和类型相同)按照指定的权重进行加权叠加(图像融合),并添加一个标量值&#x…...
相机Camera日志分析之三十一:高通Camx HAL十种流程基础分析关键字汇总(后续持续更新中)
【关注我,后续持续新增专题博文,谢谢!!!】 上一篇我们讲了:有对最普通的场景进行各个日志注释讲解,但相机场景太多,日志差异也巨大。后面将展示各种场景下的日志。 通过notepad++打开场景下的日志,通过下列分类关键字搜索,即可清晰的分析不同场景的相机运行流程差异…...
大数据学习(132)-HIve数据分析
🍋🍋大数据学习🍋🍋 🔥系列专栏: 👑哲学语录: 用力所能及,改变世界。 💖如果觉得博主的文章还不错的话,请点赞👍收藏⭐️留言Ǵ…...
C++课设:简易日历程序(支持传统节假日 + 二十四节气 + 个人纪念日管理)
名人说:路漫漫其修远兮,吾将上下而求索。—— 屈原《离骚》 创作者:Code_流苏(CSDN)(一个喜欢古诗词和编程的Coder😊) 专栏介绍:《编程项目实战》 目录 一、为什么要开发一个日历程序?1. 深入理解时间算法2. 练习面向对象设计3. 学习数据结构应用二、核心算法深度解析…...
在鸿蒙HarmonyOS 5中使用DevEco Studio实现企业微信功能
1. 开发环境准备 安装DevEco Studio 3.1: 从华为开发者官网下载最新版DevEco Studio安装HarmonyOS 5.0 SDK 项目配置: // module.json5 {"module": {"requestPermissions": [{"name": "ohos.permis…...

MacOS下Homebrew国内镜像加速指南(2025最新国内镜像加速)
macos brew国内镜像加速方法 brew install 加速formula.jws.json下载慢加速 🍺 最新版brew安装慢到怀疑人生?别怕,教你轻松起飞! 最近Homebrew更新至最新版,每次执行 brew 命令时都会自动从官方地址 https://formulae.…...

Unity中的transform.up
2025年6月8日,周日下午 在Unity中,transform.up是Transform组件的一个属性,表示游戏对象在世界空间中的“上”方向(Y轴正方向),且会随对象旋转动态变化。以下是关键点解析: 基本定义 transfor…...

Axure 下拉框联动
实现选省、选完省之后选对应省份下的市区...

自然语言处理——文本分类
文本分类 传统机器学习方法文本表示向量空间模型 特征选择文档频率互信息信息增益(IG) 分类器设计贝叶斯理论:线性判别函数 文本分类性能评估P-R曲线ROC曲线 将文本文档或句子分类为预定义的类或类别, 有单标签多类别文本分类和多…...

企业大模型服务合规指南:深度解析备案与登记制度
伴随AI技术的爆炸式发展,尤其是大模型(LLM)在各行各业的深度应用和整合,企业利用AI技术提升效率、创新服务的步伐不断加快。无论是像DeepSeek这样的前沿技术提供者,还是积极拥抱AI转型的传统企业,在面向公众…...