在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中追加数据)的更多相关文章

  1. Java利用POI导入导出Excel中的数据

         首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据.下面POI包的下载地 ...

  2. java jxl 向Excel中追加数据而不覆盖原来数据的例子

    向先原来就有数据的Excel写数据是不会覆盖原有的数据,只是在追加数据. public class Excel {     public Excel() {     }     public void ...

  3. POI向Excel中写入数据及追加数据

    import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import ...

  4. C#读取Excel表格数据到DataGridView中和导出DataGridView中的数据到Excel

    其实想在datagridview中显示excel表格中的数据跟读取数据库中的数据没什么差别,只不过是创建数据库连接的时候连接字段稍有差别. private void btnShow_Click(obj ...

  5. (最全最灵活地)利用Jxl工具包实现Excel表的内容读取 、写入(可向已有表中追加数据)

    1.引子 (1)读取 Jxl工具比较强大,可以方便地实现Excel表的读取和写入.另一款工具Poi也具有相似的功能,并且功能更多,运用也相对复杂.Poi读取Excel表内容时,需要先判断其内容格式,如 ...

  6. 使用OpenXml把Excel中的数据导出到DataSet中

    public class OpenXmlHelper { /// <summary> /// 读取Excel数据到DataSet中,默认读取所有Sheet中的数据 /// </sum ...

  7. 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的路由方案,与原来的方案在使用上差别不 ...

  8. sql 读取excel中的数据

    select 列名 as 字段名 from openBowSet('MSDASQL.1','driver=Microsoft Excel Driver(*.xls);dbq=文件存放地址','sele ...

  9. SpringBoot(十三)_springboot上传Excel并读取excel中的数据

    今天工作中,发现同事在整理数据,通过excel上传到数据库.所以现在写了篇利用springboot读取excel中的数据的demo.至于数据的进一步处理,大家肯定有不同的应用场景,自行修改 pom文件 ...

随机推荐

  1. WdatePicker小结

    WdatePicker.js日期插件: 1. %y  当前年  %M  当前月  %d  当前日  %ld 本月最后一天  %H  当前时  %m  当前分  %s  当前秒 2.可以通过配置minD ...

  2. MAC上安装 HLA(High Level Assembly)

    1.安装HLA 最新版的hla汇编器可在这里下载,支持MacOs,Linux,Windows平台 2.安装步骤 将下载好的hla程序包放在Mac根目录下 最重要的一步是设置好环境变量,打开Mac根目录 ...

  3. SQL中部分语法整理

    1.SELECT DISTINCT 语句 关键词DISTINCT用于返回唯一不同的值. 语法: SELECT DISTINCT 列名称 FROM 表名称 2.SELECT INTO语句 SELECT ...

  4. 使用sublime一键格式化XML文件

    1 sublime简介 sublime是一款代码编辑和阅读软件,体积小,运行快,界面非常简洁漂亮.官方地址:https://www.sublimetext.com/ 2 在sublime上安装插件 使 ...

  5. MySQL的if,case语句使用总结

    原文地址: http://outofmemory.cn/code-snippet/1149/MySQL-if-case-statement-usage-summary

  6. 【01-05】hibernate BaseDao

    BaseDao接口定义 package org.alohaworld.util.dao; import java.io.Serializable; import java.util.List; imp ...

  7. visio二次开发——图纸解析之形状

    今天有空,下班前补齐解析visio图形形状的方法,包含图形背景色.字体颜色.备注信息.形状数据取值. /// <summary> /// 设置形状的选择属性 /// </summar ...

  8. HTML Select 标签选择后触发jQuery事件代码实例

    页面设计原由: 因为很多客户不知道如何来到我们公司,领导想让我在微信公众号上面做一个链接,客户可以直接通过微信公众号打开地图并导航到我们公司的办公地点. 实现起来并不难,但由于公司有很多办事处,所以需 ...

  9. Linux搭建Nginx

    1.Nginx安装 1.1 pcre (1)下载编译包 http://www.pcre.org/ (注意需要的是pcce,而非pcre2) (2)tar -zxvf pcre-8.36.tar.gz  ...

  10. 菜鸟笔记:java编程基础一

    Java中的关键字 Java 语言中有一些具有特殊用途的词被称为关键字.关键字对 Java 的编译器有着特殊的意义,在程序中应用时一定要慎重哦!! Java 中常用关键字: 问:这么多,记不住啊... ...