当前位置:  编程技术>.net/c#/asp.net

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

    来源: 互联网  发布时间:2014-11-01

    本文导语:  本文实例展示了WinForm项目开发中NPOI用法,对于C#初学者有一定的借鉴价值。具体实例如下: private void ExportMergeExcel() { if (File.Exists(templateXlsPath)) { int i = 4, _recordNo = 1; using (FileStream file = new FileStream(templateXlsPath, FileMode....

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

定义样式:

/// 
/// 样式创建
/// 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;
///}
/// 为行设置样式
/// 
/// IRow
/// ICellStyle
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;
 }
  }
}

    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • WinForm实现同时让两个窗体有激活效果的特效实例
  • C# WinForm中Panel实现用鼠标操作滚动条的实例方法
  • winform使用委托和事件来完成两个窗体之间通信的实例
  • 关于C#.net winform程序验证moss的集成身份认证实例
  • WinForm实现拦截窗体上各个部位的点击特效实例
  • Winform 显示Gif图片的实例代码
  • C# WinForm捕获未处理的异常实例解析
  • C# WinForm开发中使用XML配置文件实例
  • winform树形菜单无限级分类实例
  • WinForm自定义控件应用实例
  • WinForm之BindingSource基础操作实例教程
  • WinForm 自动完成控件实例代码简析
  • c#多线程更新窗口(winform)GUI的数据
  • .Net WInform开发笔记(二)Winform程序运行结构图及TCP协议在Winform中的应用
  • C# WinForm中禁止改变窗口大小的方法
  • WinForm相对路径的陷阱
  • c# Winform 全窗口拖动的代码
  • Winform实现抓取web页面内容的方法
  • WinForm实现关闭按钮不可用或隐藏的方法
  • 解读在C#中winform程序响应键盘事件的详解
  • c# winform 关闭窗体时同时结束线程实现思路
  • WinForm实现读取Resource中文件的方法
  • C# Winform 整个窗口拖动的实现代码
  • WinForm下 TextBox只允许输入数字的小例子
  • Winform跨线程操作的简单方法
  • C# WinForm程序完全退出的问题解决
  • C# WinForm窗体编程中处理数字的正确操作方法
  • C# Winform 让整个窗口都可以拖动
  • 使用C# Winform应用程序获取网页源文件的解决方法
  • c# 天气预报查询(winform方法)的实现代码(图文)
  • C# Winform 禁止用户调整ListView的列宽


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3