在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. 18. class

    Class 基本用法 class n { constructor(x,y) { this.x = x; this.y = y; console.log(x,y) } proint() { consol ...

  2. java中HashMap重要性质和优化总结

    原文: http://www.cnblogs.com/junyuhuang/p/4519257.html

  3. HTML学习

    <!DOCTYPE html> <html> <head> <title>标题</title> <meta charset=" ...

  4. HotSpot JVM常用参数设置

    转自:https://www.zybuluo.com/jewes/note/57352 选项的分类 Hotspot JVM提供以下三大类选项: 1. 标准选项:这类选项的功能是很稳定的,在后续版本中也 ...

  5. iOS之访问权限以及跳转到系统界面

    iOS开发中有时候有这样的需求:当用户设置不允许访问照片.麦克风和相机等系统权限的时候,这时需要直接跳转到系统的隐私界面进行设置. 判断是否开启权限 前面已经说过,我们需要在用户不允许访问的时候跳转, ...

  6. vim的编译安装及其插件YouCompleteMe安装

    相关的环境: win 7 x64 vs2013 community python 2.7.10 AMD64 python 3.5 AMD64 LLVM 3.5 cmake 3.5   YouCompl ...

  7. Visual Studio EventHandler Delegate 和 EventArgs

    EventHandler代理 用来表示处理一个没有事件数据(event data)的事件(event)的 方法. 无论何时事件发生时,事件代理就被调用来触发以前事件驱动的其他事件(监听当前事件TCur ...

  8. VBA中使用计时器的两种方法

    '================================ ' VBA采用Application.OnTime实现计时器 ' ' http://www.cnhup.com '========= ...

  9. 国内github访问慢的解决方法

    本文是windows处理方法,macos方法也差不多. 一般Github的访问有两部分:主站的访问和二级域名的资源加载(比如样式文件等) 一般Github加载缓慢,主要是 assets-cdn.git ...

  10. WebRTC的一个例子

    内容引自:一个WebRTC实现获取内网IP的例子(穿透NAT) 网页代码直接复制到下面(如果以上链接被墙,可以直接将下面代码保存文件,然后在浏览器打开即可,不支持IE浏览器): <!doctyp ...