1、加入NPOI 程序集,使用nuget添加程序集

2、引用NPOI程序集

        private IWorkbook ExportExcel(PrintQuotationOrderViewModel model)
{
//if (model == null) return string.Empty;
string tempDirPath = Server.MapPath("/Templates/Excel/");
if (!Directory.Exists(tempDirPath))
{
Directory.CreateDirectory(tempDirPath);
}
IWorkbook workbook;
string excelTempPath = tempDirPath + "quotaExcelTemp-new.xls";
//加载excel模板
using (FileStream fs = new FileStream(excelTempPath, FileMode.Open, FileAccess.Read))
{
//XSSFWorkbook 适用XLSX格式,HSSFWorkbook 适用XLS格式
workbook = new HSSFWorkbook(fs);
} ISheet sheet = workbook.GetSheetAt();
sheet.GetRow().GetCell().SetCellValue(model.QuotationOrder.QuotedOn.ToString("yyyy-MM-dd")); sheet.GetRow().GetCell().SetCellValue(model.QuotationOrder.Number); sheet.GetRow().GetCell().SetCellValue(model.QuotationOrder.CustomerPurchaseNumber);
//甲方
sheet.GetRow().GetCell().SetCellValue(model.QuotationOrder.Buyer.Company.Name);
sheet.GetRow().GetCell().SetCellValue(model.QuotationOrder.Buyer.Name);
sheet.GetRow().GetCell().SetCellValue(model.QuotationOrder.Buyer.Email);
sheet.GetRow().GetCell().SetCellValue(model.QuotationOrder.Receiver.Mobile);
sheet.GetRow().GetCell().SetCellValue(model.QuotationOrder.Receiver.Address); //乙方
sheet.GetRow().GetCell().SetCellValue("XXXXX有限公司");
ICellStyle cstyle = workbook.CreateCellStyle();
cstyle.Alignment = HorizontalAlignment.Left;
sheet.GetRow().GetCell().CellStyle = cstyle; sheet.GetRow().GetCell().SetCellValue(model.QuotationOrder.SalesmanName);
sheet.GetRow().GetCell().CellStyle = cstyle; sheet.GetRow().GetCell().SetCellValue(model.QuotationOrder.Salesman.Mobile); sheet.GetRow().GetCell().CellStyle = cstyle; sheet.GetRow().GetCell().SetCellValue(model.QuotationOrder.Salesman.Email);
sheet.GetRow().GetCell().CellStyle = cstyle; int count = model.QuotationItems.Count;
for (int i = ; i < count; i++)
{ //设置列头的单元格样式
HSSFCellStyle cellStyle = workbook.CreateCellStyle() as HSSFCellStyle; IRow row = sheet.CopyRow(, + i);
ICell cell = row.CreateCell();
cell.SetCellValue((i + ));
ICellStyle style1 = SetCellStyle((HSSFWorkbook)workbook, HorizontalAlignment.Left);
cell.CellStyle = style1; cell = row.CreateCell();
cell.SetCellValue(model.QuotationItems[i].Product.Name);
cell.CellStyle = style1; cell = row.CreateCell();
cell.CellStyle = style1;
//合并单元格
CellRangeAddress region = new CellRangeAddress( + i, + i, , );
sheet.AddMergedRegion(region); cell = row.CreateCell();
cell.CellStyle = style1;
cell.SetCellValue(model.QuotationItems[i].CustomCode);
cell = row.CreateCell();
cell.CellStyle = style1;
cell.SetCellValue(model.QuotationItems[i].Product.Code);
cell = row.CreateCell();
cell.CellStyle = style1;
cell.SetCellValue("PCS");
cell = row.CreateCell();
cell.CellStyle = style1;
cell.SetCellValue(model.QuotationItems[i].Quantity);
cell = row.CreateCell();
cell.CellStyle = style1;
cell.SetCellValue(model.QuotationItems[i].Quotation.DispatchDays >= ? ((int)model.QuotationItems[i].Quotation.DispatchDays).ToString() : "");
cell = row.CreateCell();
cell.CellStyle = style1;
cell.SetCellValue(model.QuotationItems[i].Quotation.UnitPriceWithTax >= ? ((decimal)model.QuotationItems[i].Quotation.UnitPriceWithTax).ToString("f2") : "");
cell = row.CreateCell();
cell.CellStyle = style1;
cell.SetCellValue(model.QuotationItems[i].Quotation.SubtotalWithTax.ToString("f2"));
cell = row.CreateCell();
cell.CellStyle = style1;
cell.SetCellValue(model.QuotationItems[i].Remark);
} sheet.GetRow( + count).GetCell().SetCellValue(model.QuotationOrder.Shipping.Amount.ToString("f2"));
sheet.GetRow( + count).GetCell().SetCellValue(model.QuotationOrder.TotalWithTax.ToString("f2"));
sheet.GetRow( + count).GetCell().SetCellValue(model.QuotationOrder.TotalWithTaxInChinese);
sheet.GetRow( + count).GetCell().SetCellValue(model.Payment); return workbook;
}

3、设置表格样式

  /// <summary>
/// 给Excel添加边框
/// </summary>
private ICellStyle SetCellStyle(HSSFWorkbook hssfworkbook, HorizontalAlignment ha)
{
ICellStyle cellstyle = hssfworkbook.CreateCellStyle();
cellstyle.Alignment = ha; //有边框
cellstyle.BorderBottom = BorderStyle.Thin;
cellstyle.BorderLeft = BorderStyle.Thin;
cellstyle.BorderRight = BorderStyle.Thin;
cellstyle.BorderTop = BorderStyle.Thin;
return cellstyle;
}

4、excel加载图片

  HSSFPatriarch patriarch = (HSSFPatriarch)sheet.DrawingPatriarch;
HSSFClientAnchor anchor = new HSSFClientAnchor(, , , , , + count, , + count);
HSSFPicture picture = (HSSFPicture)patriarch.CreatePicture(anchor, LoadImage(tempDirPath + "1.png", (HSSFWorkbook)workbook));

LoadImage 方法

    private int LoadImage(string path, HSSFWorkbook wb)
{
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[file.Length];
file.Read(buffer, , (int)file.Length);
return wb.AddPicture(buffer, PictureType.PNG); }

5、导出excel

var stream = new MemoryStream();
var work = ExportExcel(printQuotationOrderViewModel);
work.Write(stream);
//mvc代码
return File(stream.GetBuffer(), "application/vnd.ms-excel", quotedOrderModel.Number + ".xls");

C# NPOI 操作Excel 案例的更多相关文章

  1. NPOI操作Excel辅助类

    /// <summary> /// NPOI操作excel辅助类 /// </summary> public static class NPOIHelper { #region ...

  2. NPOI操作excel之写入数据到excel表

    在上一篇<NPOI操作excel之读取excel数据>我们把excel数据写入了datatable中,本篇就讲如何把datatable数据写入excel中. using System; u ...

  3. C#开发中使用Npoi操作excel实例代码

    C#开发中使用Npoi操作excel实例代码 出处:西西整理 作者:西西 日期:2012/11/16 9:35:50 [大 中 小] 评论: 0 | 我要发表看法 Npoi 是什么? 1.整个Exce ...

  4. 用NPOI操作EXCEL关于HSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2)的参数

    2.4.1 用NPOI操作EXCEL关于HSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2)的参数   NPOI教程:http://www.cnb ...

  5. NPOI读取Excel案例

    3.4用NPOI操作EXCEL--从Excel中抽取文本 我们知道,搜索引擎最擅长处理的就是文本,而Excel中的内容并不是以文本方式存储的.那么如果想要搜索引擎爬虫能够抓取到Excel中的内容是比较 ...

  6. C# 如何使用NPOI操作Excel以及读取合并单元格等

    C#操作Excel方法有很多,以前用的需要电脑安装office才能用,但因为版权问题公司不允许安装office.所以改用NPOI进行Excel操作,基本上一些简单的Excel操作都没有问题,读写合并单 ...

  7. 用NPOI操作EXCEL-锁定列CreateFreezePane()

    public void ExportPermissionRoleData(string search, int roleStatus) { var workbook = new HSSFWorkboo ...

  8. .NET 通过 NPOI 操作 Excel

    目录 .NET 通过 NPOI 操作 Excel 第一步:通过 NuGet 获取 NPOI 包并引入程序集 第二步:引入 NPOI 帮助类 第三步:在程序中调用相应的方法对数据进行导出导入操作 将 D ...

  9. 2.6.2 用NPOI操作EXCEL--设置密码才可以修改单元格内容

    2.6.2 用NPOI操作EXCEL--设置密码       有时,我们可能需要某些单元格只读,如在做模板时,模板中的数据是不能随意让别人改的.在Excel中,可以通过“审阅->保护工作表”来完 ...

随机推荐

  1. 父页面内获取获取iframe内的变量或者是获取iframe内的值

    前提:页面不可跨域访问,必须同一域名下,否则返回值为空 父页面 <!DOCTYPE html> <html lang="en"> <head> ...

  2. 基本MVVM 和 ICommand用法举例(转)

    引言 在本贴中,我们将学习WPF Commands. Commands 可以很好地与 MVVM 模式 (Model- View-ViewModel)结合在一起.我们也将看到,视图(view)实际上是怎 ...

  3. 数据结构学习之栈求解n皇后问题

    数据结构学习之栈求解n皇后问题 0x1 目的 ​ 深入掌握栈应用的算法和设计 0x2 内容 ​ 编写一个程序exp3-8.cpp求解n皇后问题. 0x3 问题描述 即在n×n的方格棋盘上,放置n个皇后 ...

  4. 好程序员分享该如何选择background-image和img标签

    好程序员分享该如何选择background-image和img标签,用img标签 如果你希望别人打印页面时候包含这张图片请使用img标签 当这张图片有非常有意义的语义,比如警告图标,请使用img标签及 ...

  5. 论学好Linux系统的超级重要性

    不知道各位在日常的工作生活中有没有接触过“rm -rf /*”这个命令,因为这个命令搞出来的事情可还不少呢!前段时间就在一个群里看到了有个小伙子,老板让他去维护一下服务器,这小伙也不太懂,就问群里的大 ...

  6. vue 相关

    1.vue v-for 循环一个数组,key值报错,但是数据是正常显示的 报错: v-for使用key,需要在key前加上:key;srcList是个数组,key值绑定不能是数据类型Object的it ...

  7. Java静态代码块、构造代码块执行顺序问题

    package com.zxl.staticdemo; public class BlockTest { static { System.out.println("BlockTest静态代码 ...

  8. 浅谈kafka streams

    随着数据时代的到来,数据的实时计算也越来越被大家重视.实时计算的一个重要方向就是实时流计算,目前关于流计算的有很多成熟的技术实现方案,比如Storm.Spark Streaming.flink等.我今 ...

  9. html表单的方便操作

    //表单阻止复制粘贴 <input class="pass" type="text" oncopy="return false" on ...

  10. CentOS7使用yum安装MySQL8.0

    1.yum仓库下载MySQL:sudo yum localinstall https://repo.mysql.com//mysql80-community-release-el7-1.noarch. ...