NOPI读取模板导出(Excel中追加数据)
在Controller里,我们定义一个FileResult的Action,返回值是一个文件形式被浏览器下载下来。
[HttpGet]
public FileResult ExportProductList1(ProductQueryParam param)
{
param.PageSize = ;
var results = _baseInfoBusiness.ExportProduct(param, Customer.BookId);try
{
string filePath = Server.MapPath("~/others/tempFiles/商品列表.xls");///文件模板路径
FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read);///读取文件流
var buffer = DataExport.ExportProduct(results.Data.Items, file);///在Excel中追加数据,返回值是二进制数据流
var name = string.Format("{0}_{1:yyyyMMddHHmmss}.xls", "商品列表", DateTime.Now);
return File(buffer, "application/vnd.ms-excel", name);
}
catch (Exception e)
{ } return null;
}
Excel追加数据处理方法
public byte[] ExportProduct(List<ProductInfo> productList, FileStream file)
{
HSSFWorkbook hssfworkbook = new HSSFWorkbook(file);///如果带参数是创建一个Excel,带参数就是读取一个Excel
ISheet sheet = hssfworkbook.GetSheet("商品资料");///读完Sheet using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
for (int i = ; i < productList.Count; i++)
{
IRow row = sheet.CreateRow(i + );
row.CreateCell().SetCellValue(productList[i].CategoryName);
row.CreateCell().SetCellValue(productList[i].No);
row.CreateCell().SetCellValue(productList[i].Name);
row.CreateCell().SetCellValue(productList[i].IniQty.ToString("f2"));///期初数量
row.CreateCell().SetCellValue(productList[i].IniPrice.ToString("f2"));///期初单价
row.CreateCell().SetCellValue(productList[i].IniTotal.ToString("f2"));///期初总价
row.CreateCell().SetCellValue(productList[i].Specification);
for (int j = ; j < productList[i].ProductProp.Count; j++)
{
row.CreateCell( + j).SetCellValue(productList[i].ProductProp[j].Name);///属性
}
for (int j = productList[i].ProductProp.Count; j < ; j++)
{
row.CreateCell( + productList[i].ProductProp.Count + j).SetCellValue("");///属性
}
///基本单位
row.CreateCell().SetCellValue(productList[i].UnitName);
row.CreateCell().SetCellValue(productList[i].Barcode);
row.CreateCell().SetCellValue(productList[i].RetailPrice.ToString("f2"));
row.CreateCell().SetCellValue(productList[i].WholesalePrice.ToString("f2"));
row.CreateCell().SetCellValue(productList[i].LowestsalePrice.ToString("f2"));
row.CreateCell().SetCellValue(productList[i].PurchasePrice.ToString("f2")); if (productList[i].unitPrice == null | productList[i].unitPrice.Count>)
{
///副单位1
row.CreateCell().SetCellValue(productList[i].unitPrice[].UnitName);
row.CreateCell().SetCellValue(productList[i].unitPrice[].Urate.ToString());
row.CreateCell().SetCellValue(productList[i].unitPrice[].Barcode);
row.CreateCell().SetCellValue(productList[i].unitPrice[].RetailPrice.ToString("f2"));
row.CreateCell().SetCellValue(productList[i].unitPrice[].WholesalePrice.ToString("f2"));
row.CreateCell().SetCellValue(productList[i].unitPrice[].LowestsalePrice.ToString("f2"));
row.CreateCell().SetCellValue(productList[i].unitPrice[].PurchasePrice.ToString("f2"));
if(productList[i].unitPrice.Count>)
{
row.CreateCell().SetCellValue(productList[i].unitPrice[].UnitName);
row.CreateCell().SetCellValue(productList[i].unitPrice[].Urate.ToString());
row.CreateCell().SetCellValue(productList[i].unitPrice[].Barcode);
row.CreateCell().SetCellValue(productList[i].unitPrice[].RetailPrice.ToString("f2"));
row.CreateCell().SetCellValue(productList[i].unitPrice[].WholesalePrice.ToString("f2"));
row.CreateCell().SetCellValue(productList[i].unitPrice[].LowestsalePrice.ToString("f2"));
row.CreateCell().SetCellValue(productList[i].unitPrice[].PurchasePrice.ToString("f2"));
}
}
///库存预警
row.CreateCell().SetCellValue(productList[i].MinStock.ToString("f2"));
row.CreateCell().SetCellValue(productList[i].MaxStock.ToString("f2"));
row.CreateCell().SetCellValue(productList[i].Memo);
row.CreateCell().SetCellValue(productList[i].IsStop?"停用":"启用"); }
sheet.ForceFormulaRecalculation = true;
hssfworkbook.Write(ms);
return ms.ToArray();
} }
NOPI读取模板导出(Excel中追加数据)的更多相关文章
- Java利用POI导入导出Excel中的数据
首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据.下面POI包的下载地 ...
- java jxl 向Excel中追加数据而不覆盖原来数据的例子
向先原来就有数据的Excel写数据是不会覆盖原有的数据,只是在追加数据. public class Excel { public Excel() { } public void ...
- POI向Excel中写入数据及追加数据
import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import ...
- C#读取Excel表格数据到DataGridView中和导出DataGridView中的数据到Excel
其实想在datagridview中显示excel表格中的数据跟读取数据库中的数据没什么差别,只不过是创建数据库连接的时候连接字段稍有差别. private void btnShow_Click(obj ...
- (最全最灵活地)利用Jxl工具包实现Excel表的内容读取 、写入(可向已有表中追加数据)
1.引子 (1)读取 Jxl工具比较强大,可以方便地实现Excel表的读取和写入.另一款工具Poi也具有相似的功能,并且功能更多,运用也相对复杂.Poi读取Excel表内容时,需要先判断其内容格式,如 ...
- 使用OpenXml把Excel中的数据导出到DataSet中
public class OpenXmlHelper { /// <summary> /// 读取Excel数据到DataSet中,默认读取所有Sheet中的数据 /// </sum ...
- ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案 try.dot.net 的正确使用姿势 .Net NPOI 根据excel模板导出excel、直接生成excel .Net NPOI 上传excel文件、提交后台获取excel里的数据
ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案 ASP.NET Core 从2.2版本开始,采用了一个新的名为Endpoint的路由方案,与原来的方案在使用上差别不 ...
- sql 读取excel中的数据
select 列名 as 字段名 from openBowSet('MSDASQL.1','driver=Microsoft Excel Driver(*.xls);dbq=文件存放地址','sele ...
- SpringBoot(十三)_springboot上传Excel并读取excel中的数据
今天工作中,发现同事在整理数据,通过excel上传到数据库.所以现在写了篇利用springboot读取excel中的数据的demo.至于数据的进一步处理,大家肯定有不同的应用场景,自行修改 pom文件 ...
随机推荐
- broadcasting Theano vs. Numpy
broadcasting Theano vs. Numpy broadcast mechanism allows a scalar may be added to a matrix, a vector ...
- BZOJ1923: [Sdoi2010]外星千足虫
传送门 高斯消元求解Xor方程. 这个方程很容易换成xor的方程.然后用高斯消元搞就行了. 用bitset实现这个非常方便. //BZOJ 1923 //by Cydiater //2016.11.3 ...
- unity之自制玻璃啤酒瓶shader
客户的要求如下 步骤: 1.进行玻璃瓶效果分析 效果如下:高光,类次表面散射(里层通透而外层较为暗淡),外层白色勾勒轮廓. 高光:unity内部提供光滑度参数,越光滑则高光效果越明显,啤酒瓶材质是属于 ...
- [Unreal]学习笔记之材质说明
取消蓝图中的连接线:Alt+鼠标左键 在蓝图中,通过按住1,2,3,4加鼠标左键,可以快速生成1,2,3,4维的向量 材质和材质实例的区别:使用一个母材质,可以创建出多种场景中的材质实例:每次修改母材 ...
- Openstack学习目录
1.2016.12.27 ceph简介 crush算法 OSD(boject storage device) Mds(cephfs) 使用cephfs时需要安装metadata s ...
- java内存泄漏的几种情况
转载于http://blog.csdn.net/wtt945482445/article/details/52483944 Java 内存分配策略 Java 程序运行时的内存分配策略有三种,分别是静态 ...
- TaskCompletionSource<TResult>
参考:https://blogs.msdn.microsoft.com/pfxteam/2009/06/02/the-nature-of-taskcompletionsourcetresult/
- 第三章 --- 关于Javascript 设计模式 之 代理模式
第一.定义: 代理模式是为一个对象提供代用品或者占位符,以便控制对它的访问. 比如说,某男生小明想向他的女神 A 表白,刚好小明认识的一个女生B 和 女神A 是好朋友,那么小明就想让 女生B 帮忙送花 ...
- JavaScript 构造函数与原型链
构造函数.原型链: function Person(name, age, job) { this.name = name; this.age = age; this.job = job; // thi ...
- 欢迎来到Joyful Physics博客
本博客主要包括以下内容: 物理课程 预计会涵盖非物理专业普通物理.物理专业普通物理.理论物理(四大力学).凝聚态物理,会特别关注软物质物理,因为博主是做软物质物理的. 软硬科普 软科普写给非专业人士. ...