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. java 易错选择题 编辑中

    1 System.out.println(int(a+b)); 编译错误  应该是(int)(a+b) 2 String s="john"+3; 是正确的,结果就是 john3 3 ...

  2. Ranger-Kafka插件安装

    Ranger-Kafka插件安装, 使用Ranger0.7.0版本,集成Kafka插件到Kafka集群, Kafka Plugin需要安装到所有的Kafka的集群节点上面. 1.登陆Kafka的安装用 ...

  3. 2018-2019-2 20175217 实验二《Java面向对象程序设计》实验报告

    一.实验报告封面 课程:Java程序设计 班级:1752班 姓名:吴一凡 学号:20175217 指导教师:娄嘉鹏 实验日期:2019年4月15日 实验时间:--- 实验序号:实验二 实验名称:Jav ...

  4. Wonder 1.0正式版发布-----WebGL 3D引擎和编辑器

    介绍 我们很荣幸地向大家发布Wonder 1.0正式版!免费.开源,不用注册,直接打开在线编辑器即可使用! Wonder是web端3D开发的解决方案,包括引擎.编辑器,致力于打造开放.分享.互助的生态 ...

  5. day12(表达式,推导式,名称空间与作用域,函数的嵌套定义)

    一,复习 # 字符串的比较 # -- 按照从左往右比较每一个字符,通过字符对应的ascll进行比较 # print('a' > 'A') #True # print('ac' > 'ab' ...

  6. 【学习总结】win7使用anaconda安装tensorflow+keras

    tips: Keras是一个高层神经网络API(高层意味着会引用封装好的的底层) Keras由纯Python编写而成并基Tensorflow.Theano以及CNTK后端. 故先安装TensorFlo ...

  7. Windows之常用命令

    1. 重启/关机 shutdown命令 #关机 shutdown -s -t -f #重启 shutdown -r -t //30秒之后,重启 00是立即 #注销 shutdown -l -t #休眠 ...

  8. golang异常处理

    一.使用defer+recover 错误例子: [root@localhostgo_test]#cat t1.go package main func main(){ panic(1) } [root ...

  9. MT 互联网 面试标准

    能力模型 业务理解(每项2分) java知识(每项2分) 网络知识(每项1分) 设计模式(每项3分) 数据库知识(每项2分) 框架知识(每项1分) 数据结构与算法(每项1分) 架构知识(每项3分) 操 ...

  10. MarkdownPad

    MarkdownPad Markdown编辑器,只能在windows下使用 下载地址 :http://markdownpad.com/ 破解: 邮箱:Soar360@live.com 授权证书 GBP ...