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. 专注于C#.Net WPF软件开发-软件反编译-软件破解-逆向-靖芯科技-包括安卓APK反编译

    靖芯科技提供.Net软件开发,软件修改定制二次开发,软件破解,反编译,逆向等各项优质服务: 包括安卓APK软件反编译. 包括但不限于C#,WPF,Surface,Winform,Asp.net.JAV ...

  2. jquery字符串操作

    目的:把自己常用到的jquery操作字符串总结一下 w3cSchool关于js字符串的整理:有需要的时候可以查 http://www.w3school.com.cn/js/jsref_obj_stri ...

  3. windows本地远程虚拟机docker中的数据的问题

    关闭各种防火墙 打开宿主机(windows)的cmd,在其中添加通往192.168.1.0/24网络的路由. 通往192.168.1.0/24网络的数据包由172.20.1.12来转发 route a ...

  4. process.nextTick

    回调函数同步执行 function asyncFake(data, callback) { if(data === 'foo') { callback(true); }else{ callback(f ...

  5. 从输出日志中提取接口的入参和返回做为用例导入到excel中

    1  背景 接口用例已经在项目中的yml文件中编写,但是yml文件不能做为交付文档用,本文对工作中从接口输出日志中提取用例信息,并导入到excel文件中做了总些 2  工具 idea,notepad+ ...

  6. 安装Linux虚拟系统

    VMWare创建虚拟机与Linux系统的安装 准备工作:VMWare虚拟机,Linux系统镜像 创建好虚拟机之后就可以进入Bios(Basic input ouput system)界面设置安装引导顺 ...

  7. git 添加、提交、推送

    只添加本地修改的一个文件 如,只添加package.json一个文件 git add package.json git commit -m "修改qa环境版本号" git push ...

  8. 随心测试_软测基础_006<测试人职业发展>

    接上篇:熟悉了_测试人员的工作职责范围与具体的工作内容 ,如何规划:测试人员的职业路线呢? 贴心小提示:以下内容,仅供参考,不挖坑 Q1:如何规划测试工程师的职业发展路线? A1:SX的观点:预定目标 ...

  9. Docker 核心技术之Dockerfile

    Dockerfile 简介 什么是Dockerfile Dockerfile其实就是根据特定的语法格式撰写出来的一个普通的文本文件 利用docker build命令依次执行在Dockerfile中定义 ...

  10. consumer zookeeper is not a recognized option

    kafka 创建消费者报错 consumer zookeeper is not a recognized option 在做kafka测试的时候,使用命令bin/kafka-console-consu ...