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);}}}}
}

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

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

canvas-绘图库fabric.js简介

一般情况下简单的绘制&#xff0c;其实canvas原生方法也可以满足&#xff0c;比如画个线&#xff0c;绘制个圆形、正方形、加个文案。 let canvas document.getElementById(canvas);canvas.width 1200;canvas.height 600;canvas.style.width 1200px;canvas.style.height 6…

【算法基础】栈和队列及常见变种与使用,双栈、动态栈、栈的迭代器,双端队列、优先队列、并发队列、延迟队列的使用

目录 一、栈&#xff08;Stack&#xff09; 二、 队列&#xff08;Queue&#xff09; 三、栈和队列的常见变种与使用 3.1 栈的常见的变种与使用 3.1.1 最小栈&#xff08;Min Stack&#xff09; 3.1.2 双栈&#xff08;Two Stacks&#xff09; 3.1.3 固定大小栈&#xf…

eclipse svn插件安装

1.进入eclipse的help->Eclipse Marketplace,如下图所示&#xff1a; 2.输入“svn”,再按回车&#xff0c;如下图&#xff1a; 3.这我选择的是 Subversive,点击后面的“install”按钮&#xff0c;如下图 Eclipse 下连接 SVN 库有两种插件 —— Subclipse 与 Subversive &…

开源C# Winform Scada 上位机系统

开源Winform Scada系统 功能展示C#源码程序说明下载程序源码获取 功能展示 本软件目前包含: 常用PLC通讯控件, 常用IO读写控件, 权限过滤, 用户管理, 日志记录, 报警记录. 使用方式: 在VS2022里面拖放控件, 填写控件属性,完成组态.即可成为一个完整的上位机. C#源码 程序说明…

弱信号的采样与频谱分析(修订中...)

1.频谱混叠效应 - 波形数据抽样 这是一组经过抽样的数据的频谱&#xff0c;红圈圈出的两条谱线&#xff0c;是我们需要关注的特征谱线。这个信号与右侧的临近信号比较&#xff0c;求频率比值&#xff0c;比值恒定与理论推导相符。再5取1降低采样率后&#xff0c;大致相同的频率…

宝塔nginx搭建Ftp文件服务器

一&#xff1a;创建FTP 填入账号密码后&#xff0c;选择根目录&#xff0c;这个根目录就是nginx要代理的目录 二&#xff1a;配置nginx root的地址就是上面填的FTP根目录 三&#xff1a;http访问 服务器ip端口号加图片 例如我放了一个320.jp 我服务器ip是110.120.120.120 那…

使用MySQL聚合函数来聚合数据,结果发现有刺客...

问题&#xff1a; 使用MySQL聚合函数 group_concat 的坑&#xff01; 现象&#xff1a; 我有个业务&#xff0c;需要将表中符合条件的数据行的id聚合成一个字符串&#xff0c;以供另外一张表的查询过滤。 SELECTx FROMt_A WHEREFIND_IN_SET(guan_lian,(SELECTgroup_concat( i…

iOS自动化测试方案(一):MacOS虚拟机保姆级安装Xcode教程

文章目录 一、环境准备二、基础软件三、扩展&#xff1a;usb拓展插件 一、环境准备 1、下载VMware虚拟机的壳子&#xff0c;安装并注册软件(可以百度注册码)&#xff0c;最新版本&#xff1a;v17 2、下MacOS系统iOS镜像文件&#xff0c;用于vmware虚拟机安装&#xff0c;当前镜…

Unity WebSocket-Server

&#x1f33c;WebSocket-Server &#x1f96a;效果展示&#x1f32d;启动Server&#x1f371;连接Server &#x1f96a;效果展示 在Unity中创建WebSocket服务器&#xff0c;从网页连接到该服务器进行消息通信&#xff0c;在Unity中接收到的消息都在主线程中 &#x1f32d;启…

RK3588 VDD_NPU电源PCB设计注意事项

1、VDD_NPU的覆铜宽度需满足芯片的电流需求&#xff0c;连接到芯片电源管脚的覆铜足够宽&#xff0c;路径不能被过孔分割太严重&#xff0c;必须计算有效线宽&#xff0c;确认连接到CPU每个电源PIN脚的路径都足够。 2、VDD_NPU的电源在外围换层时&#xff0c;要尽可能的多打电…

计算机,软件工程,网络工程,大数据专业毕业设计选题有哪些(附源码获取)

计算机&#xff0c;软件工程&#xff0c;网络工程&#xff0c;大数据专业毕业设计选题有哪些?&#xff08;附源码获取&#xff09; ✌全网粉丝20W,csdn特邀作者、博客专家、CSDN新星计划导师、java领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于J…

纽禄美卡Neuromeka亮相美国FABTECH,展示用于焊接的3D视觉协作机器人

原创 | 文 BFT机器人 纽禄美卡Neuromeka公司在由美国精密成型协会、美国焊接协会、化工涂料协会等5大协会举办的美国金属加工及焊接展览会FABTECH上精彩亮相。这家总部位于韩国首尔的公司成立于2013年&#xff0c;是机器人解决方案领域的领先供应商&#xff0c;致力于提高各种…

超大表格组件滚动渲染优化

引用自 摸鱼wiki 背景 业务中需要渲染一个最多有100列的表格&#xff0c;由于表格使用原生dom实现&#xff0c;因此会出现同屏有近1000个单元格同时绘制&#xff0c;在快速滑动时页面会产生卡顿&#xff0c;影响用户体验。 方案 如下图所示&#xff0c;由于用户显示屏区域有…

现代数据中心发明人Luiz André Barroso去世,享年59岁,Jeff Dean、劈柴发推悼念

Luiz Andr Barroso因故去世&#xff0c;享年59岁。他作为现代云计算行业的奠基人&#xff0c;为谷歌的发展做出了不可磨灭的贡献。 数据中心发明人&#xff0c;云计算的奠基人&#xff0c;谷歌22年老兵Luiz Andr Barroso于9月16日意外去世&#xff0c;享年59岁。 谷歌CEO 劈柴…

Docker——认识并安装Docker(上篇)

Docker 一、Docker认识二、Docker功能1、更快速的交付和部署2、更高效的虚拟化3、更轻松的迁移和扩展4、更简单的管理Docker 和 VM 三、学习Docker前的必备知识1、环境配置2、虚拟化部署方式3、虚拟化优点4、虚拟化局限性5、容器与虚拟机的区别6、Docker为什么比VM快&#xff1…

TikTok营销成功秘籍:ROI指标的黄金法则

在当今数字营销领域&#xff0c;TikTok已经崭露头角&#xff0c;成为了品牌和营销者们争相追逐的热门平台。 然而&#xff0c;要在TikTok上取得成功&#xff0c;不仅需要创意和内容&#xff0c;还需要精确的ROI&#xff08;投资回报率&#xff09;指标来衡量和优化你的营销策略…

Matlab论文插图绘制模板第116期—带时间刻度的图

之前的文章中&#xff0c;分享了Matlab带线标记的图&#xff1a; 带阴影标记的图&#xff1a; 带箭头标记的图&#xff1a; 带图形标记的图&#xff1a; 带Latex公式的图&#xff1a; 进一步&#xff0c;分享一下带时间刻度的图&#xff0c;先来看一下成品效果&#xff1a; 特别…

软考高级+系统架构设计师教程+第二版新版+电子版pdf

注意&#xff01;&#xff01;&#xff01; 系统架构设计师出新版教程啦&#xff0c;2022年11月出版。所以今年下半年是新版第一次考试&#xff0c;不要再复习老版教程了&#xff0c;内容改动挺大的。 【内容简介】系统架构设计师教程&#xff08;第2版&#xff09;作为全国计…

CV经典任务(二)目标检测 |单目标,多目标 非极大值抑制等

文章目录 1 目标检测1.1 单目标检测1.2 多目标检测3.2.1 阶段一 单像素点采样目标检测3.2.2 阶段二 多像素点采样目标检测3.2.3 阶段三 RNN3.2.4 阶段四 一阶段的目标检测 Yolo/SSD 1 目标检测 目标检测的重要任务是 目标定位&#xff1a;目标检测的首要任务是确定图像中对象…

杭州亚运会虚实短视频《功成》,给数字人三维动画宣传片制作有何启发?

杭州亚运会功夫创意视频《功成》&#xff0c;由超写实数字人筱竹、武术演员张晋、青年运动员共同参演&#xff0c;通过数字人筱竹拜师功夫大师张晋的故事&#xff0c;将动静结合的中国功夫与亚运赛场上的体育竞技创意结合&#xff0c;以全新的数字人三维动画宣传片&#xff0c;…