WinForm项目开发中NPOI用法实例解析
作者:shichen2014 发布时间:2021-09-17 16:53:57
标签:WinForm,NPOI
本文实例展示了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;
}
}
}


猜你喜欢
- 对于大规模乱序的数组,插入排序很慢,因为它只会交换相邻的元素,因此元素只能一点一点地从数组地一段移动到另一端。希尔排序改进了插入排序,交换不
- 一、简述mybatis驼峰式命名规则自动转换:使用前提:数据库表设计按照规范“字段名中各单词使用下划线"_"划分”;使用
- 前言随着使用es场景的增多,工作当中避免不了去使用es进行数据的存储,在数据存储到es当中以后就需要使用DSL语句进行数据的查询、聚合等操作
- CSRF介绍CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click atta
- 一、前言随着互联网项目前后端分离方式的流行,前端与后端交给不同的人员开发,项目沟通成本也随之提高。主要表现在WebAPI接口的沟通,Swag
- 前言是否会遇到这样的场景,你向线程池提交了多个任务,你希望这批任务全部完成后能够反向通知你。你可能会使用线程计数的方式,等到计数器累加到提交
- 自从接触javascript以来,对this参数的理解一直是模棱两可。虽有过深入去理解,但却也总感觉是那种浮于表面,没有完全理清头绪。但对于
- JFrame默认的窗体比较土,可以通过一定的美化,让窗体表现的比较漂亮,具体要根据设计的设计图进行美化;JFrame美化的大致思路:先将JF
- 一、延迟加载:LazyLoading使用延迟加载,关联的实体必须标注为virtual。本例是标注Destination类里的Lodgings
- 使用Aspose.Cells创建和读取Excel文件,供大家参考,具体内容如下1. 创建ExcelAspose.Cells.License
- Android 四种获取屏幕宽度的方法方法一: WindowManager wm = (WindowManager) this
- 本文实例为大家分享了Qt TCP实现简单通信的具体代码,供大家参考,具体内容如下在.pro文件中添加网络模块 Qt += network服务
- Android:4.4.4一、应用场景在Android设备上,现在我们外接了一个USB转串口的设备,设备节点是/dev/ttyUSB0:#
- 正确使用并行流,避免共享可变状态错用并行流而产生错误的首要原因,就是使用的算法改变了某些共享状态。下面是另一种实现对前n个自然数求和的方法,
- Listview现在用的很少了,基本都是使用Recycleview,但是不得不说Listview具有划时代的意义,拓展性很强,我们可以自己添
- 概述AOP(Aspect Orient Programming),我们一般称为面向方面(切面)编程,作为面向对象的一种补充,用于处理系统中分
- Application.Idle()方法表示:当应用程序处于空闲状态时执行相应代码。示例程序1、界面设计:一个简单的Lable控件2、代码u
- 这篇文章将向大家展示Java编程利用socket多线程访问服务器文件代码示例,如果您想先了解Java多线程socket编程的基础知识,可以看
- 在android开发中,当不满足触发条件就按返回键的时候,就要对此进行检测。尤其是当前Activity需要往前一个Activity传送消息时
- 一般情况下在Word中输入的文字都是横向的,今天给大家分享两种方法来设置/更改一个section内的所有文本的方向及部分文本的方向,有兴趣的