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. Linux CentOS 6 解决 Device eth0 does not seem to be present

    一.故障现象: [root@c1node01 ~]# service network restart Shutting down loopback insterface:                ...

  2. RocketMQ4.3.X关于设置useEpollNativeSelector = true报错问题

    前一阵子刚整理完RocketMQ4.3.x版本的相关配置的工作,接下来就来测试一下改变参数会带来什么好的结果 首先我就选中了useEpollNativeSelector 这个参数 默认这个参数是 fa ...

  3. SharpZipLib压缩解压的使用

    项目中使用 Velocity 将模板和生成的动态内容(HTML.XML等)合并保存到redis数据库中,考虑到压缩的文件容量会比较小,方便传输而且存储所使用的空间也会比较小,所以要压缩一下,读取的时候 ...

  4. 利用ELK分析Nginx日志生产实战(高清多图)

    本文以api.mingongge.com.cn域名为测试对象进行统计,日志为crm.mingongge.com.cn和risk.mingongge.com.cn请求之和(此二者域名不具生产换环境统计意 ...

  5. springmvc的前端控制器

    <servlet> <servlet-name>xxx</servlet-name> <servlet-class>org.springframewor ...

  6. python操作随笔

    # -*- encoding: utf-8 -*-import urllib2from bs4 import BeautifulSoupimport re f1 = open('E:/1.txt')l ...

  7. 偶写的第一个控件,一个用选择代替输入的Edit控件…

    FDataSource :=TDataSource.Create(self); FDBGrid.FreeNotification(self); FADOQuery.FreeNotification(s ...

  8. Spring Boot 2.x 编写 RESTful API (二) 校验

    用Spring Boot编写RESTful API 学习笔记 约束规则对子类依旧有效 groups 参数 每个约束用注解都有一个 groups 参数 可接收多个 class 类型 (必须是接口) 不声 ...

  9. [模板] tarjan/联通分量/dfs树

    //to update 边的分类 有向图边分为四类: 树边, 前向边, 返祖边(后向边), 横叉边. 上图: 判定 有向图 对图进行dfs, 不考虑已经遍历过的点, 得到dfs序 \(dfn_i\). ...

  10. ShoppingCart

    数据库设计 表结构 [dbo].[AdminInfo] AdminID, AdminName, AdminPassword, RoleID [dbo].[BK_Car] ID, CarID, ISBN ...