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;
}
}
}
0
投稿
猜你喜欢
- 实现Callable的对象中,用@Autowired注入别的对象失败场景是这样:我需要在一个实现类A中写一个拿到返回值的多线程,于是用的Ca
- 效果视频引用描述本示例采用的是非常、非常、非常好用的一款第三方SDK——helloCharts传送门导包第一步 :导入mavenmaven
- 在没讲.net如何随机生成汉字之前先给大家讲下汉字编码组成及原理。1、汉字编码原理到底怎么办到随机生成汉字的呢?汉字从哪里来的呢?是不是有个
- 1、Java字符串在 Java 中字符串被作为 String 类型的对象处理。 String 类位于 java.lang 包中,默认情况下该
- 其他的不多说了!我们来看看效果吧 一、实现方式一:直接引入compile方式A
- 先说明一下,项目代码已上传至github,不想看长篇大论的也可以先去下代码,对照代码,哪里不懂点哪里。代码在这https://github.
- 下面讲解以下在使用listview时最常见的几个问题。1.如何改变item的背景色和按下颜色listview默认情况下,item的背景色是黑
- 一、下载https://www.eclipse.org/downloads/download.php?file=/oomph/epp/202
- /** * 朴素字符串算法通过两层循环来寻找子串, * 好像是一个包含模式的“模板”沿待查文本滑动。 * 算法的思想是:从主串S的第p
- 各位亲们可以尝试以下代码:注:这里我就只有一个html标签对来说明问题了,首部之类的东西,自己添加。<html> &n
- 一、概述在日常开发中,获取一批数据后,可能需要跟据一定规则对这批数据进行排序操作。在JAVA中,动态数组ArrayList经常被用来存储数据
- 本文实例讲述了Java简单实现调用命令行并获取执行结果。分享给大家供大家参考,具体如下:import java.io.BufferedRea
- 今天给大家提供一个由今天给大家提供一个由Java swing实现的酒店管理系统,数据库采用sqlserver,我会贴上部分代码,完整的代码请
- 1.用途在SpringBoot中,通过jasypt可以进行加密解密. 这个是双向的, 且可以配置密钥.2.使用:2.1通过UT创建工具类,并
- 本文实例为大家分享了Android实现双曲线折线图的具体代码,供大家参考,具体内容如下先看一下效果图1.先下载jar包 mpandroidc
- 我们在使用一些开源调度系统(比如:elastic-job等)的时候,对于任务的执行时间通常都是有规律性的,可能是每隔半小时执行一次,或者每天
- Java 8 lambda表达式引入详解及实例eclipse下载安装Help -> EclipseMarketplace ->
- 目录一、事出有因二、解决方案困境三、柳暗花明,终级解决方案第一种实现方案第二种实现方案第三种实现方案四、引发的思考一、事出有因最近有一个场景
- 1. Spring简介Spring是一个轻量级控制反转(IoC)和面向切面(AOP)的容器框架。2. Spring的优势 1.方便解耦, 简
- SpringMVC文件下载说明: 在 SpringMVC 中,通过返回 ResponseEntity的类型,可以实现文件下载的功能案例演示1