WinForm项目开发中NPOI用法实例解析
更新时间:2020年6月25日 11:35 点击:2037
本文实例展示了WinForm项目开发中NPOI用法,对于C#初学者有一定的借鉴价值。具体实例如下:
private void ExportMergeExcel() { if (File.Exists(templateXlsPath)) { int i = 4, _recordNo = 1; using (FileStream file = new FileStream(templateXlsPath, FileMode.Open, FileAccess.Read)) { HSSFWorkbook _excel = new HSSFWorkbook(file); ICellStyle _cellStyle = CreateCellStly(_excel); ISheet _sheetBasic = _excel.GetSheet(ExcelReadHelper.sheet_BasicInfo.Replace("$", "")); ISheet _sheetStreatLamp = _excel.GetSheet(ExcelReadHelper.sheet_LampMoreLess.Replace("$", "")); ISheet _sheetBasicEx = _excel.GetSheet(ExcelReadHelper.sheet_BasicExInfo.Replace("$", "")); ISheet _sheetStreatLampEx = _excel.GetSheet(ExcelReadHelper.sheet_LampMoreLessExInfo.Replace("$", "")); ISheet _sheetBasicTeamEx = _excel.GetSheet(ExcelReadHelper.sheet_BasicTeamStatistics.Replace("$", "")); ISheet _sheetBasicLampTypeEx = _excel.GetSheet(ExcelReadHelper.sheet_BasicTypeStatistics.Replace("$", "")); ISheet _sheetStreetLampMLEx = _excel.GetSheet(ExcelReadHelper.sheet_LampMoreLessTeamStatistics.Replace("$", "")); ISheet _sheetStreetLampTeamML = _excel.GetSheet(ExcelReadHelper.sheet_LampMoreLessTypeStatistics.Replace("$", "")); file.Close(); FillBasicSheetDb(_sheetBasic, i, _recordNo); _recordNo = 1; i = 4; FillStreetLampDb(_sheetStreatLamp, i, _recordNo); _recordNo = 1; i = 4; FillBasicExSheetDb(_sheetBasicEx, i, _recordNo); _recordNo = 1; i = 4; FillStreetLampExDb(_sheetStreatLampEx, i, _recordNo); i = 1; IRow _rowSum = null; int _lampTotalLampCnt = 0, _colLampCnt = 0, _ncolLampCnt = 0; double _lampTotalLampPw = 0, _colLampPw = 0, _ncolLampPw = 0; FillBasicTeamExSheetDb(_excel, _rowSum, _sheetBasicTeamEx, _cellStyle, i, _lampTotalLampCnt, _colLampCnt, _ncolLampCnt, _lampTotalLampPw, _colLampPw, _ncolLampPw); i = 1; _lampTotalLampCnt = 0; _colLampCnt = 0; _ncolLampCnt = 0; _lampTotalLampPw = 0; _colLampPw = 0; _ncolLampPw = 0; FillbasicLampTypeExSheetDb(_excel, _rowSum, _sheetBasicLampTypeEx, _cellStyle, i, _lampTotalLampCnt, _colLampCnt, _ncolLampCnt, _lampTotalLampPw, _colLampPw, _ncolLampPw); _lampTotalLampCnt = 0; _lampTotalLampPw = 0; i = 1; FillsheetStreetLampMLSheetDb(_excel, _rowSum, _sheetStreetLampMLEx, _cellStyle, i, _lampTotalLampCnt, _lampTotalLampPw); _lampTotalLampCnt = 0; _lampTotalLampPw = 0; i = 1; FillStreetLampTeamMLSheetDb(_excel, _rowSum, _sheetStreetLampTeamML, _cellStyle, i, _lampTotalLampCnt, _lampTotalLampPw); OutPutMergeExcel(_excel); } } } private void FillBasicTeamExSheetDb(HSSFWorkbook _excel, IRow _rowSum, ISheet _sheetBasicTeamEx, ICellStyle _cellStyle, int i, int _lampTotalLampCnt, int _colLampCnt, int _ncolLampCnt, double _lampTotalLampPw, double _colLampPw, double _ncolLampPw) { foreach (ExcelStatistics excelBasicEx in basicTeamExList) { IRow _row = _sheetBasicTeamEx.CreateRow(i); ExcelWriteHelper.CreateStatisticsExcelRow(_row, excelBasicEx, "BasicTeam"); #region 总灯数 int _lTotalLampCnt = 0; int.TryParse(excelBasicEx.LampCount, out _lTotalLampCnt); _lampTotalLampCnt += _lTotalLampCnt; #endregion #region 总计算功率(KW) double _lTotalLampPw = 0; double.TryParse(excelBasicEx.LampPower, out _lTotalLampPw); _lampTotalLampPw += _lTotalLampPw; #endregion #region 汇总灯数 int _cLampCount = 0; int.TryParse(excelBasicEx.CollectCount, out _cLampCount); _colLampCnt += _cLampCount; #endregion #region 汇总功率(KW) double _cLampPw = 0; double.TryParse(excelBasicEx.CollectPower, out _cLampPw); _colLampPw += _cLampPw; #endregion #region 非汇总灯数 int _ncLampCount = 0; int.TryParse(excelBasicEx.NotCollectCount, out _ncLampCount); _ncolLampCnt += _ncLampCount; #endregion #region 非汇总功率(KW) double _ncLampPw = 0; double.TryParse(excelBasicEx.NotCollectPower, out _ncLampPw); _ncolLampPw += _ncLampPw; #endregion i++; } _rowSum = _sheetBasicTeamEx.CreateRow(i); _rowSum.HeightInPoints = 20; _rowSum.CreateCell(0).SetCellValue("合计:"); _rowSum.CreateCell(1).SetCellValue(_lampTotalLampCnt); _rowSum.CreateCell(2).SetCellValue(_lampTotalLampPw); _rowSum.CreateCell(3).SetCellValue(_colLampCnt); _rowSum.CreateCell(4).SetCellValue(_colLampPw); _rowSum.CreateCell(5).SetCellValue(_ncolLampCnt); _rowSum.CreateCell(6).SetCellValue(_ncolLampPw); SetRowStyle(_rowSum, _cellStyle); }
定义样式:
/// <summary> /// 样式创建 /// eg: ///private ICellStyle CreateCellStly(HSSFWorkbook _excel) ///{ /// IFont _font = _excel.CreateFont(); /// _font.FontHeightInPoints = 11; /// _font.FontName = "宋体"; /// _font.Boldweight = (short)FontBoldWeight.Bold; /// ICellStyle _cellStyle = _excel.CreateCellStyle(); /// //_cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightGreen.Index; /// //_cellStyle.FillPattern = NPOI.SS.UserModel.FillPattern.SolidForeground; /// _cellStyle.SetFont(_font); /// return _cellStyle; ///} /// 为行设置样式 /// </summary> /// <param name="row">IRow</param> /// <param name="cellStyle">ICellStyle</param> public static void SetRowStyle(this IRow row, ICellStyle cellStyle) { if (row != null && cellStyle != null) { for (int u = row.FirstCellNum; u < row.LastCellNum; u++) { ICell _cell = row.GetCell(u); if (_cell != null) _cell.CellStyle = cellStyle; } } }
相关文章
- 本篇文章是对C#中WinForm控件之Dock顺序调整进行了详细的分析介绍,需要的朋友参考下...2020-06-25
- 这篇文章主要介绍了C# Winform中实现主窗口打开登录窗口关闭的方法,这在需要用户名密码的软件项目中是必用的一个技巧,要的朋友可以参考下...2020-06-25
- 下面小编就为大家带来一篇C# winform打开Excel文档的方法总结(必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- 这篇文章主要给大家介绍C# winform快捷键设置技巧,涉及到C winform快捷键相关知识,对C winform知识感兴趣的朋友可以参考下本篇文章...2020-06-25
c#中Winform实现多线程异步更新UI(进度及状态信息)
本篇文章主要介绍了c#中Winform实现多线程异步更新UI(进度及状态信息) ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25- 这篇文章主要介绍了C#中winform使用相对路径读取文件的方法,实例分析了C#使用相对路径读取文件的技巧与实际应用,需要的朋友可以参考下...2020-06-25
- 这篇文章主要给大家介绍了关于c# winform异步不卡界面的实现方法,文中通过示例代码介绍的非常详细,对大家学习或者使用c#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2020-06-25
C# WinForm程序处理后台繁忙导致前台控件假死现象解决方法
这篇文章主要介绍了C# WinForm程序处理后台繁忙导致前台控件假死现象解决方法,本文通过Application.DoEvents()解决这个问题,并讲解了Application.DoEvents()的作用,需要的朋友可以参考下...2020-06-25winform中的ListBox和ComboBox绑定数据用法实例
这篇文章主要介绍了winform中的ListBox和ComboBox绑定数据用法,实例分析了将集合数据绑定到ListBox和ComboBox控件的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25- 这篇文章主要介绍了C#在Winform开发中使用Grid++报表,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- 这篇文章主要为大家详细介绍了C# WinForm程序设计简单计算器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
C# WinForm控件对透明图片重叠时出现图片不透明的简单解决方法
这篇文章主要介绍了C# WinForm控件对透明图片重叠时出现图片不透明的简单解决方法,结合实例形式分析了WinForm图片重叠后造成图片不透明的原因与相应的解决方法,需要的朋友可以参考下...2020-06-25- 这篇文章主要介绍了C#实现Winform中打开网页页面的方法,涉及WinForm中WebBrowser的相关使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 在工作中遇到这样一个问题,在系统使用过程中,输入法会变灰导致无法使用输入法输入文字,就好像输入法被禁用了没有启用似的。对此,在这里做个备录...2020-06-25
C#实现winform中RichTextBox在指定光标位置插入图片的方法
这篇文章主要介绍了C#实现winform中RichTextBox在指定光标位置插入图片的方法,涉及RichTextBox控件及剪切板的相关操作技巧,非常简单实用,需要的朋友可以参考下...2020-06-25C# Winform 调用系统接口操作 INI 配置文件的代码
封装了一小段代码, 调用系统接口, 操作配置文件. 一般用于 .ini 文件, 或者其它键值对格式的配置文件...2020-06-25- 这篇文章主要介绍了WinForm调用百度地图接口用法,结合具体实例形式简单分析了WinForm WebBrower控件与前端百度接口交互的相关操作技巧,需要的朋友可以参考下...2020-06-25
- 在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式...2020-06-25
- 这篇文章主要介绍了C# WinForm中禁止改变窗口大小的方法,需要把FormBorderStyle和MaximizeBox的值固定即可,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了C# inForm编程获取文件物理路径的方法,获取的物理路径是软件即软件安装所在目录,需要的朋友可以参考下...2020-06-25