在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. iOS无限循环滚动scrollview

    经常有园友会问"博主,有没有图片无限滚动的Demo呀?", 正儿八经的图片滚动的Demo我这儿还真没有,今天呢就封装一个可以在项目中直接使用的图片轮播.没看过其他iOS图片无限轮播 ...

  2. myql Connect

    mysql折腾笔记 2014-01-05 10:58 经常吹嘘自己玩过各种数据库. redis, mysql, sqlite, mongodb..常用数据库都不在话下,不料今天却在远程连接mysql上 ...

  3. js拖拽原理和碰撞原理

    拖拽的原理onmousedown 选择元素onmousemove 移动元素onmouseup 释放元素 1:如果拖拽的时候有文字:被选中,会产生问题原因:当鼠标按下的时如果页面中有文字或者图片被选中的 ...

  4. boostrap 模态框

    <div class="modal fade" id="myModal" tabindex="-1" role="dialo ...

  5. nginx配置ssl证书的方法

    Nginx (读音"engine x") 是一个高性能的HTTP和反向代理服务器,比Apache占用更少的内存,同时也像Apache一样支持HTTPS方式访问(SSL加密).本教程 ...

  6. NC凭证接口(Java发送流和处理返回结果)

    问题描述: 金融行业在系统模块分为财务和业务两个系统,我公司是负责业务模块系统,NC公司负责财务系统.但是财务有时候需要生成凭证,这时候就涉及业务模块了,我方就需要写NC凭证接口.这时候就需要三方交互 ...

  7. ajax传输中文乱码解决方法

    问题描述: 我是在一个jsp页面有一个保存按钮,点击时会触发saveForm()的js函数,在saveForm()函数里经过校验后,会通过ajax发送数据请求,这样就不用通过提交表单来传输数据了,aj ...

  8. SSH正反向隧道

    正向隧道 拓扑如下: 说明: CLIENT不能直接访问WEB服务器,AGENT可访问WEB服务器: 在AGENT上通过创建ssh正向隧道,使CLIENT可以通过AGENT间接访问WEB服务器: AGE ...

  9. 5 Hbase

    # 大纲: * 认识 HBase * HBase 架构 * HBase读写流程   定义: *  HBase是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,利用Hbase 技术可在廉价PC S ...

  10. Linux搭建Nginx

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