excel npoi 连接_MVC导出Excel之NPOI简单使用(一)
public FileResult ToExcel()
{//数据源
var source =ProductsService.List();//保存路径
string prirkdFilePath = HttpContext.Server.MapPath("~/DownLoad") + "\\" + DateTime.Now.ToString("yyyyMM") + "\\";if (!Directory.Exists(prirkdFilePath))
Directory.CreateDirectory(prirkdFilePath);//文件名称
string fileNameNoExtension = "产品列表(" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ")";string fileName = fileNameNoExtension + ".xlsx";//模板路径
string priModel = Server.MapPath(@"~/DownLoad\ExcelTemp\产品列表.xlsx");//生成模板读取IO流
FileStream fileStreamRead = newFileStream(priModel, FileMode.Open, FileAccess.Read);//创建工作簿对象
XSSFWorkbook workBook = newXSSFWorkbook(fileStreamRead);//预设单元格样式
XSSFCellStyle comStyle =(XSSFCellStyle)workBook.CreateCellStyle();
comStyle.Alignment= HorizontalAlignment.Center;//垂直居中
XSSFCellStyle dateStyle=(XSSFCellStyle)workBook.CreateCellStyle();
dateStyle.Alignment= HorizontalAlignment.Center;//垂直居中
XSSFDataFormat format =(XSSFDataFormat)workBook.CreateDataFormat();
dateStyle.DataFormat= format.GetFormat("yyyy-mm-dd HH:mm:ss");//日期样式
XSSFCellStyle decimalStyle=(XSSFCellStyle)workBook.CreateCellStyle();
decimalStyle.Alignment=HorizontalAlignment.Center;
XSSFDataFormat format2=(XSSFDataFormat)workBook.CreateDataFormat();
decimalStyle.DataFormat= format2.GetFormat("0.00");//获取工作表
XSSFSheet xssfSheet = workBook.GetSheetAt(0) asXSSFSheet;int rowIndex = 1;//填充数据
foreach (V_Product row insource)
{//创建行
XSSFRow xssfRow = xssfSheet.CreateRow(rowIndex) asXSSFRow;
xssfRow.CreateCell(0).SetCellValue(row.ProductNumber);
xssfRow.Cells[0].CellStyle =comStyle;
xssfRow.CreateCell(1).SetCellValue(row.ProductName);
xssfRow.Cells[1].CellStyle =comStyle;
xssfRow.CreateCell(2);
xssfRow.CreateCell(3).SetCellValue(row.ProductStandard);
xssfRow.Cells[3].CellStyle =comStyle;
xssfRow.CreateCell(4).SetCellValue(Convert.ToDouble(row.Price));
xssfRow.Cells[4].CellStyle =decimalStyle;
xssfRow.CreateCell(5).SetCellValue(Convert.ToDouble(row.OrderId));
xssfRow.Cells[5].CellStyle =comStyle;
xssfRow.CreateCell(6).SetCellValue(row.CategoryName);
xssfRow.Cells[6].CellStyle =comStyle;
xssfRow.CreateCell(7).SetCellValue(row.Creator);
xssfRow.Cells[7].CellStyle =comStyle;
xssfRow.CreateCell(8).SetCellValue(Convert.ToDateTime(row.Createtime));
xssfRow.Cells[8].CellStyle =dateStyle;//第二列添加图片
if (!string.IsNullOrEmpty(row.ProductImg))
{byte[] picBytes =getImageByte(row.ProductImg);int pictureIdx = workBook.AddPicture(picBytes, NPOI.SS.UserModel.PictureType.JPEG); //添加图片
XSSFDrawing patriarch=(XSSFDrawing)xssfSheet.CreateDrawingPatriarch();
XSSFClientAnchor anchor= new XSSFClientAnchor(0, 0, 100, 50, 2, rowIndex, 2 + 1, rowIndex + 1);//图片位置,图片左上角为(col, row)
XSSFPicture pict =(XSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);//pict.Resize();//用图片原始大小来显示
}
}//合并路径
prirkdFilePath =Path.Combine(prirkdFilePath, fileName);//转化为流保存
FileStream files = newFileStream(prirkdFilePath, FileMode.Create);
workBook.Write(files);
files.Close();
files.Dispose();
return File(prirkdFilePath, "application/ms-excel", fileName);
}
excel npoi 连接_MVC导出Excel之NPOI简单使用(一)相关推荐
- WeihanLi.Npoi 根据模板导出Excel
WeihanLi.Npoi 根据模板导出Excel Intro 原来的导出方式比较适用于比较简单的导出,每一条数据在一行,数据列虽然自定义程度比较高,如果要一条数据对应多行就做不到了,于是就想支持根据 ...
- npoi把xlsx文件转为html,C# NPOI 导入与导出Excel文档 兼容xlsx, xls(xf13中已经引用了xlsx的npoi)...
这里使用的NPOI版本为: 2.1.3.1 版本内包含.Net 2.0 与.Net 4.0 .Net 4.0中包含文件 使用时需引用需要引用所有5个dll 使用到的引用 using NPOI.HSSF ...
- npoi 将html导出word,使用NPOI将数据导出为word格式里的table
开发环境:VS2013+MySQL5.5+EF6+NPOI2.0.6 格式:WinForm+CodeFirst PS:vs2013的CodeFirst很方便了啊 CodeFirst方式就不再赘述了. ...
- C# NPOI 和 CSV 导出Excel 功能实现
1.引言 程序中经常会使用到的一个功能就是导出 Excel ,而导出 Excel 的实现主要有两种方法,一种是 CSV 导出,一种是 NPOI 导出,而从效果上来说,NPOI 更能够符合Excel 导 ...
- C# NPOI 导入与导出Excel文档 兼容xlsx, xls
这里使用的NPOI版本为: 2.1.3.1 官方下载地址: http://npoi.codeplex.com/releases 版本内包含.Net 2.0 与.Net 4.0 .Net 4.0中包含文 ...
- C#使用NPOI实现导入导出Excel数据
一.NPOI简介 1.1.NPOI是什么 NPOI是POI的.NET版本,POI是一套用Java写成的库,我们在开发中经常用到导入导出表格.文档的情况,NPOI能够帮助我们在没有安装微软Office的 ...
- 导出 服务器 excel文件,服务器数据库导出excel文件格式
服务器数据库导出excel文件格式 内容精选 换一换 GaussDB(DWS)支持使用gs_dumpall工具导出所有数据库的全量信息,包含集群中每个数据库信息和公共的全局对象信息.可根据需要自定义导 ...
- java导出excel压缩包_java动态导出excel压缩成zip下载的方法
本文实例为大家分享了java动态导出excel压缩成zip下载的具体代码,供大家参考,具体内容如下 package pack.java.io.demo; import java.io.Buffered ...
- Java POI 导出EXCEL经典实现 Java导出Excel
转自http://blog.csdn.net/evangel_z/article/details/7332535 在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者 ...
- java ssh 下载excel,SSH整合WEB导出EXCEL案例
基于Spring,Struts2,Hibernate整合, jsp页面导出(下载)excel文件的简单应用 1)本例不采用Java任何导出excel文件常用的poi或者jxl 等第三方jar包,仅仅基 ...
最新文章
- Javascript使用Ajax跟踪历史
- 数字在数组中出现的次数
- mov sreg, r/m16 在16位和32位编程中的区别
- 文本处理三剑客之 awk
- 串口服务器端口配置及调试的6大技巧
- 精心挑选的23款美轮美奂的 jQuery 图片特效插件
- Photoshop Blendmodi in GLSL
- java 视频 缩略图_如何使用java提取视频缩略图或某一帧的图片
- cve-2020-0796_CVE20200796 | Windows SMBv3客户端/永痕之黑漏洞复现
- World Wind Java开发之四——搭建本地WMS服务器(转)
- iis7.5 php 404.17,部署IISHTTP 404.17无法由静态文件处理程序来处理
- Android开源项目第一篇——个性化控件(View)篇
- 几个可以整蛊你朋友的 Python 程序
- php5.2.17 pecl,php pecl的使用
- 每天一道Java编程01-厚度为0.0001米的纸折叠多少次可以折成珠穆朗玛峰8848米的高度?
- 网站出现DNS域名解析错误怎么办?
- ps技巧:从黑白图片中创建蒙版
- k8s create pod失败,cannot be handled as a Deployment: [pos 481]: json: expect char '' but got char '1
- .NetCore源码阅读笔记系列之Security (一) Authentication AddCookie
- 28.从键盘输入10个正负相间的整数,输出个位数是奇数、十位数是偶数的所有数。