• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > WinForm项目开发中NPOI用法实例解析

WinForm项目开发中NPOI用法实例解析

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了c winform npoi,winform npoi,c#winform编程实例,c#winform实例,winform项目实例等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例展示了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);
}

</div>

定义样式:

/// <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;
 }
  }
}
</div> </div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • WinForm项目开发中NPOI用法实例解析

相关文章

  • 2017-05-28C#通过属性名称获取(读取)属性值的方法
  • 2017-05-28c#中合并DataTable重复行的值
  • 2017-05-28C#进度轴控件分享
  • 2017-05-28Revit API取得变量的内参名称实例代码
  • 2017-05-28C#使用iTextSharp设置PDF所有页面背景图功能实例
  • 2017-05-28C#控制台模拟电梯工作原理
  • 2017-05-28C# String Replace高效的实例方法
  • 2017-05-28c#文件名/路径处理方法示例
  • 2017-05-28C# char类型字符转换大小写的实现代码
  • 2017-05-28C#集合遍历时删除和增加元素的方法

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C#编程自学之数据类型和变量一
    • C#中DataGridView操作技巧
    • c#文件的复制,移动,创建(实例代码)
    • C#中的集合用法分析
    • C#远程发送和接收数据流生成图片的方法
    • C# 设计模式系列教程-简单工厂模式
    • 为IObservable实现自己的运算符(详解)
    • C#生成唯一值的方法汇总
    • C#引用类型和值类型的介绍与实例
    • c# 配置文件App.config操作类库的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有