添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
ProductInfo .Clear () ; ProductInfo .Columns .Add ( "_Product" ) ; ProductInfo .Columns .Add ( "_WH" ) ; ProductInfo .Columns .Add ( "_Qty" ) ; DataRow ProductRow = ProductInfo .NewRow () ; ProductRow[ "_Product" ] = "AS133" ; ProductRow[ "_WH" ] = "W101" ; ProductRow[ "_Qty" ] = "10" ; ProductInfo .Rows .Add (ProductRow) ; ProductRow = ProductInfo .NewRow () ; ProductRow[ "_Product" ] = "AS133" ; ProductRow[ "_WH" ] = "W102" ; ProductRow[ "_Qty" ] = "7" ; ProductInfo .Rows .Add (ProductRow) ; ProductRow = ProductInfo .NewRow () ; ProductRow[ "_Product" ] = "AS133" ; ProductRow[ "_WH" ] = "W103" ; ProductRow[ "_Qty" ] = "5" ; ProductInfo .Rows .Add (ProductRow) ; ProductRow = ProductInfo .NewRow () ; ProductRow[ "_Product" ] = "AS156" ; ProductRow[ "_WH" ] = "W101" ; ProductRow[ "_Qty" ] = "6" ; ProductInfo .Rows .Add (ProductRow) ; ProductRow = ProductInfo .NewRow () ; ProductRow[ "_Product" ] = "TS156" ; ProductRow[ "_WH" ] = "W101" ; ProductRow[ "_Qty" ] = "8" ; ProductInfo .Rows .Add (ProductRow) ; ProductRow = ProductInfo .NewRow () ; ProductRow[ "_Product" ] = "TS156" ; ProductRow[ "_WH" ] = "W102" ; ProductRow[ "_Qty" ] = "8" ; ProductInfo .Rows .Add (ProductRow) ; private void button1_Click(object sender, EventArgs e) createDataTable() ; string strFilePath = string .Format ( "template.xlt" ) ; HSSFWorkbook workbook ; using (FileStream fs = new FileStream(strFilePath, FileMode .Open , FileAccess .ReadWrite )) workbook = new HSSFWorkbook(fs) ; fs .Close () ; if (workbook != null) //加载模板 HSSFSheet RawData = (HSSFSheet)workbook .GetSheet ( "RawData" ) ; //数据到新的Sheet HSSFCell hc ; HSSFRow hr ; HSSFSheet hst = RawData ; for (int i = 0 ; i < ProductInfo.Rows.Count; i++) hr = (HSSFRow)hst .CreateRow (i + 1 ) ; for (int j = 0 ; j < ProductInfo.Columns.Count; j++) hc = (HSSFCell)hr .CreateCell (j) ; //.注意! 数量是整型而不是字符串。 if (ProductInfo .Columns [j] .Caption == "_Qty" ) hc .SetCellType (CellType .Numeric ) ; if (!string .IsNullOrEmpty (ProductInfo .Rows [i][j] .ToString ())) int number = Convert .ToInt 32(ProductInfo .Rows [i][j] .ToString ()) ; hc .SetCellValue (number) ; hc .SetCellValue (ProductInfo .Rows [i][j] .ToString ()) ; hc .SetCellValue (ProductInfo .Rows [i][j] .ToString ()) ; //导出新的EXCEL文件 String filename = "P_" + DateTime .Now .ToString ( "yyyyMMdd_HHmmss" ) + ".xls" ; FileStream fsExcelNew = new FileStream(filename, FileMode .Create ) ; workbook .Write (fsExcelNew) ; //删除工作表 workbook .RemoveSheetAt ( 0 ) ; workbook = null ; fsExcelNew .Close () ; //打开excel //System .Windows .Forms .Application .StartupPath + "\\" + filename ; //string file = @ "C:\Windows\explorer.exe" ; //System .Diagnostics .Process .Start (file, filename) ;

template.xlt内容:

运行效果如图:

在无尽的追寻中,你会有一个又一个巧合和偶然,也会有一个又一个意外和错过。现实的城市犹如雾中的风景,隐隐地散发着忧郁的美,承载着没有承诺的梦Form1.cs代码:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;usi 技术杂谈,聚焦优质文章、开源项目、实用工具和学习、工作、面试心得分享。博客园推荐博客、阿里云专家博主,擅长于C#、.NET、.NET Core、Golang、TypeScript、Vue、Uni App、Angular开发。 02-25 * 其实有这个数组现在想想直接用一个新的datatable写也行, * 但还是用数组方便点,不知道数组有没有数量限制,三维数组能写的数我想大概够用了吧。 * 有了数组在写成 的时候横着放竖着放就怎么都行了, * 其他的还可以灵活些的就是那些运算方法了, * 聚合时候我只写了sum,其实可以写更多,还有条件选择,我也只写了等于,这部分可以做个扩展,感觉没有难度了 * 挺草的一个范例,别笑话我啊!!! */
在N POI 中,本身并不支持Chart等高级对象的创建,但通过l模板的方式可以巧妙地利用 Excel 强大的 透视 和图 功能,请看以下例子。 首先建立模板文件,定义两列以及指向此区域的名称“sales”: 创建 数据 数据 来源填入刚才定义的区域: 最后生成的 数据 透视 所在Sheet的样式如下: 至此,模板已经建好,另存为“D:\MyProject\N POI Demo\Chart...
实际 C# 项目中经常会涉及到需要对本地 Excel 文件进行操作,特别是一些包含 数据 记录、分析、汇总功能模块的项目。常用的操作 Excel 文件的方法主要有三个: 1. OleDb: 这种方式是把整个 Excel 文件当做一个 数据 源来进行 数据 的读取操作。 优点: 实现 方式简单,读取速度快; 缺点:读取 Excel 数据 的过程不太灵活,对内存的占用比较高,当 数据 量变的很大时,容易由...
数据 透视 (Pivot Table)是一种交互式的 ,可以进行某些计算,如求和与计数等,可动态地改变 透视 版面布置,也可以重新安排行号、列标和页字段。当改变版面布置时, 数据 透视 也会按照新的布置来进行更新,可以说是一个功能强大的 数据 分析工具。因此,本篇文章将介绍在 C# 中关于 Excel 数据 透视 的操作示例,示例内容主要包含以下要点: 创建 透视 1.1 创建 数据 缓存1.2创建 数据 透视 1... N POI ,顾名思义,就是 POI 的.NET 版本。 POI 是一套用Java 写成的库,能够帮助开发者在没有安装微软Office 的情况下读写Office 文件。N POI 是一套开源的项目,常用的命名空间,包含以下内容: N POI .Util: 基础辅助库 N POI . POI FS: OLE2 格式读写库 N POI .DDF: Microsoft Drawing格式读写库 N POI .SS: Excel ... var style = workbook.CreateCellStyle(); style.BorderBottom = BorderStyle.Thin; style.BorderTop = BorderStyle.Thin; style.BorderLeft = BorderStyle.Thin; style.BorderRight = BorderStyle.Thin; style.Alignment = HorizontalAlignment.Center; // 创建字体对象 var font = workbook.CreateFont(); font.FontHeightIn Poi nts = 12; font.Boldweight = (short)FontBoldWeight.Bold; style.SetFont(font); // 获取 格对象 var table = sheet.CreateTable(); // 应用样式 table.Style = style; 在上面的代码中,我们首先创建了一个样式对象,并设置了一些样式属性,例如边框、水平对齐和字体。然后,我们获取一个 格对象,并将样式应用于该 格对象的Style属性上。请注意,样式对象也可以应用于单元格,只需将它们应用于单元格的CellStyle属性即可。