NPOI导出例子
public static string ExportAOrder(ExportData data)
{ var cellHeard = new Dictionary<string, string> {
{ "sort","序号"},
{ "modeName","型号名称"},
{ "modeCode","型号编码"},
{ "expectedQty","预定数量"},
{ "atualQty","已出库数量"},
}; string sheetName = "设备出库订单";
string fileName = sheetName + "-" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls"; // 文件名称
string urlPath = "\\ExcelFiles\\" + fileName; // 文件下载的URL地址,供给前台下载
//Q8FilePath
string filePath = AppDomain.CurrentDomain.BaseDirectory + urlPath;
// 1.检测是否存在文件夹,若不存在就建立个文件夹
string directoryName = Path.GetDirectoryName(filePath);
if (!Directory.Exists(directoryName))
{
Directory.CreateDirectory(directoryName);
}
HSSFWorkbook workbook = new HSSFWorkbook(); // 工作簿
ISheet sheet = workbook.CreateSheet(sheetName); // 工作表
//设置列数
int ColumnCount = 5; sheet.SetColumnWidth(0, 13 * 256);
sheet.SetColumnWidth(1, 15 * 256);
sheet.SetColumnWidth(2, 15 * 256);
sheet.SetColumnWidth(3, 13 * 256);
sheet.SetColumnWidth(4, 15 * 256); int rowindex = 0; //标题
IRow rowTitle = sheet.CreateRow(rowindex);
rowTitle.Height = 40 * 20;
//在行中:建立单元格,参数为列号,从0计
ICell cell = rowTitle.CreateCell(0);
//设置单元格内容
cell.SetCellValue(sheetName);
ICellStyle style = workbook.CreateCellStyle();
//设置单元格的样式:水平对齐居中
style.Alignment = HorizontalAlignment.Center; style.VerticalAlignment = VerticalAlignment.Center;
//新建一个字体样式对象
IFont font = workbook.CreateFont();
//设置字体加粗样式
//font.Boldweight = short.MaxValue;
font.IsBold = true;
font.FontHeightInPoints = 25;
//使用SetFont方法将字体样式添加到单元格样式中
style.SetFont(font);
//将新的样式赋给单元格
cell.CellStyle = style;
sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, ColumnCount - 1)); rowindex += 3;
IRow mainRow1 = sheet.CreateRow(rowindex); mainRow1.CreateCell(0).SetCellValue("单号:");
mainRow1.CreateCell(1).SetCellValue(data.orderId); rowindex++;
IRow mainRow2 = sheet.CreateRow(rowindex); mainRow2.CreateCell(0).SetCellValue("客户编号:");
mainRow2.CreateCell(1).SetCellValue(data.comNumber); mainRow2.CreateCell(3).SetCellValue("销售姓名:");
mainRow2.CreateCell(4).SetCellValue(data.saleName); rowindex++;
IRow mainRow3 = sheet.CreateRow(rowindex); mainRow3.CreateCell(0).SetCellValue("客户名称:");
mainRow3.CreateCell(1).SetCellValue(data.comName); mainRow3.CreateCell(3).SetCellValue("联系人:");
mainRow3.CreateCell(4).SetCellValue(data.agentName); rowindex++;
IRow mainRow4 = sheet.CreateRow(rowindex); mainRow4.CreateCell(0).SetCellValue("客户电话:");
mainRow4.CreateCell(1).SetCellValue(data.comPhone); mainRow4.CreateCell(3).SetCellValue("联系号码:");
mainRow4.CreateCell(4).SetCellValue(data.agentPhone); rowindex++;
IRow mainRow5 = sheet.CreateRow(rowindex); mainRow5.CreateCell(0).SetCellValue("客户地址:");
mainRow5.CreateCell(1).SetCellValue(data.comAdress); mainRow5.CreateCell(3).SetCellValue("联系人地址::");
mainRow5.CreateCell(4).SetCellValue(data.agentAdress); IRow row = sheet.CreateRow(rowindex);
List<string> keys = new List<string>();
foreach (var item in cellHeard.Keys)
{
keys.Add(item);
} for (int i = 0; i < keys.Count; i++)
{
ICell FiledTitle = row.CreateCell(i);
ICellStyle FiledStyle = workbook.CreateCellStyle();
FiledTitle.SetCellValue(cellHeard[keys[i]]); // 列名为Key的值
FiledStyle.Alignment = HorizontalAlignment.Center;
//新建一个字体样式对象
IFont Filedfont = workbook.CreateFont();
//设置字体加粗样式
Filedfont.Boldweight = short.MaxValue;
//使用SetFont方法将字体样式添加到单元格样式中
FiledStyle.SetFont(Filedfont);
FiledStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.CornflowerBlue.Index;
FiledStyle.FillPattern = FillPattern.SolidForeground;
FiledTitle.CellStyle = FiledStyle;
}
rowindex++; foreach (var item in data.list)
{
IRow rowTmp = sheet.CreateRow(rowindex); int cellIndex = 0;
foreach (var key in keys)
{
string cellValue = ""; // 单元格的值
object properotyValue = null; // 属性的值 var property = item.GetType().GetProperty(key);
if (property.IsNotNull())
{
properotyValue = property.GetValue(item, null);
}
if (properotyValue.IsNotNull())
{
cellValue = properotyValue.ToString();
}
rowTmp.CreateCell(cellIndex).SetCellValue(cellValue);
cellIndex++;
}
rowindex++;
}
using (var file = new FileStream(filePath, FileMode.Create))
{
workbook.Write(file);
file.Close();
} return filePath; } public class ExportData
{
public ExportData()
{
list = new List<RowData>();
}
public string orderId { get; set; }
public string comNumber { get; set; }
public string comName { get; set; }
public string comPhone { get; set; }
public string comAdress { get; set; }
public string saleName { get; set; }
public string agentName { get; set; }
public string agentPhone { get; set; }
public string agentAdress { get; set; } public List<RowData> list { get; set; }
} public class RowData
{
public int sort { get; set; }
public string modeCode { get; set; }
public string modeName { get; set; }
public int expectedQty { get; set; }
public int atualQty { get; set; } }
public static string ExportAOrder(ExportData data) {
var cellHeard = new Dictionary<string, string> { { "sort","序号"}, { "modeName","型号名称"}, { "modeCode","型号编码"}, { "expectedQty","预定数量"}, { "atualQty","已出库数量"}, };
string sheetName = "设备出库订单"; string fileName = sheetName + "-" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls"; // 文件名称 string urlPath = "\\ExcelFiles\\" + fileName; // 文件下载的URL地址,供给前台下载 //Q8FilePath string filePath = AppDomain.CurrentDomain.BaseDirectory + urlPath; // 1.检测是否存在文件夹,若不存在就建立个文件夹 string directoryName = Path.GetDirectoryName(filePath); if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); } HSSFWorkbook workbook = new HSSFWorkbook(); // 工作簿 ISheet sheet = workbook.CreateSheet(sheetName); // 工作表 //设置列数 int ColumnCount = 5;
sheet.SetColumnWidth(0, 13 * 256); sheet.SetColumnWidth(1, 15 * 256); sheet.SetColumnWidth(2, 15 * 256); sheet.SetColumnWidth(3, 13 * 256); sheet.SetColumnWidth(4, 15 * 256);
int rowindex = 0;
//标题 IRow rowTitle = sheet.CreateRow(rowindex); rowTitle.Height = 40 * 20; //在行中:建立单元格,参数为列号,从0计 ICell cell = rowTitle.CreateCell(0); //设置单元格内容 cell.SetCellValue(sheetName); ICellStyle style = workbook.CreateCellStyle(); //设置单元格的样式:水平对齐居中 style.Alignment = HorizontalAlignment.Center;
style.VerticalAlignment = VerticalAlignment.Center; //新建一个字体样式对象 IFont font = workbook.CreateFont(); //设置字体加粗样式 //font.Boldweight = short.MaxValue; font.IsBold = true; font.FontHeightInPoints = 25; //使用SetFont方法将字体样式添加到单元格样式中 style.SetFont(font); //将新的样式赋给单元格 cell.CellStyle = style; sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, ColumnCount - 1));
rowindex += 3; IRow mainRow1 = sheet.CreateRow(rowindex);
mainRow1.CreateCell(0).SetCellValue("单号:"); mainRow1.CreateCell(1).SetCellValue(data.orderId);
rowindex++; IRow mainRow2 = sheet.CreateRow(rowindex);
mainRow2.CreateCell(0).SetCellValue("客户编号:"); mainRow2.CreateCell(1).SetCellValue(data.comNumber);
mainRow2.CreateCell(3).SetCellValue("销售姓名:"); mainRow2.CreateCell(4).SetCellValue(data.saleName);
rowindex++; IRow mainRow3 = sheet.CreateRow(rowindex);
mainRow3.CreateCell(0).SetCellValue("客户名称:"); mainRow3.CreateCell(1).SetCellValue(data.comName);
mainRow3.CreateCell(3).SetCellValue("联系人:"); mainRow3.CreateCell(4).SetCellValue(data.agentName);
rowindex++; IRow mainRow4 = sheet.CreateRow(rowindex);
mainRow4.CreateCell(0).SetCellValue("客户电话:"); mainRow4.CreateCell(1).SetCellValue(data.comPhone);
mainRow4.CreateCell(3).SetCellValue("联系号码:"); mainRow4.CreateCell(4).SetCellValue(data.agentPhone);
rowindex++; IRow mainRow5 = sheet.CreateRow(rowindex);
mainRow5.CreateCell(0).SetCellValue("客户地址:"); mainRow5.CreateCell(1).SetCellValue(data.comAdress);
mainRow5.CreateCell(3).SetCellValue("联系人地址::"); mainRow5.CreateCell(4).SetCellValue(data.agentAdress);
IRow row = sheet.CreateRow(rowindex); List<string> keys = new List<string>(); foreach (var item in cellHeard.Keys) { keys.Add(item); }
for (int i = 0; i < keys.Count; i++) { ICell FiledTitle = row.CreateCell(i); ICellStyle FiledStyle = workbook.CreateCellStyle(); FiledTitle.SetCellValue(cellHeard[keys[i]]); // 列名为Key的值 FiledStyle.Alignment = HorizontalAlignment.Center; //新建一个字体样式对象 IFont Filedfont = workbook.CreateFont(); //设置字体加粗样式 Filedfont.Boldweight = short.MaxValue; //使用SetFont方法将字体样式添加到单元格样式中 FiledStyle.SetFont(Filedfont); FiledStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.CornflowerBlue.Index; FiledStyle.FillPattern = FillPattern.SolidForeground; FiledTitle.CellStyle = FiledStyle; } rowindex++;
foreach (var item in data.list) { IRow rowTmp = sheet.CreateRow(rowindex);
int cellIndex = 0; foreach (var key in keys) { string cellValue = ""; // 单元格的值 object properotyValue = null; // 属性的值
var property = item.GetType().GetProperty(key); if (property.IsNotNull()) { properotyValue = property.GetValue(item, null); } if (properotyValue.IsNotNull()) { cellValue = properotyValue.ToString(); } rowTmp.CreateCell(cellIndex).SetCellValue(cellValue); cellIndex++; } rowindex++; } using (var file = new FileStream(filePath, FileMode.Create)) { workbook.Write(file); file.Close(); }
return filePath;
}
public class ExportData { public ExportData() { list = new List<RowData>(); } public string orderId { get; set; } public string comNumber { get; set; } public string comName { get; set; } public string comPhone { get; set; } public string comAdress { get; set; } public string saleName { get; set; } public string agentName { get; set; } public string agentPhone { get; set; } public string agentAdress { get; set; }
public List<RowData> list { get; set; } }
public class RowData { public int sort { get; set; } public string modeCode { get; set; } public string modeName { get; set; } public int expectedQty { get; set; } public int atualQty { get; set; }
}
NPOI导出例子的更多相关文章
- NPOI导出多表头Execl(通过html表格遍历表头)
关于NPOI的相关信息,我想博客园已经有很多了,而且NPOI导出Execl的文章和例子也很多,但导出多表头缺蛮少的:今天要讲的通过自己画html表格:通过html表格来导出自定义的多表头: 先来看要实 ...
- NPOI 导出添加批注功能
这个问题在网上搜,都是说如下即可: //添加批注HSSFPatriarch patr = (HSSFPatriarch)sheet.CreateDrawingPatriarch();HSSFComme ...
- NPOI导出Excel (C#) 踩坑 之--The maximum column width for an individual cell is 255 charaters
/******************************************************************* * 版权所有: * 类 名 称:ExcelHelper * 作 ...
- 基于NPOI导出和导入Excel
概述 NPOI,顾名思义,就是POI的.NET版本.NPOI就是用.NET语言编写的一套数据导出Excel的开源项目,支持XML.xls.xlsx.ppt等格式..NET不仅实现Excel导出还可以实 ...
- (C#)使用NPOI导出Excel
在做业务型的软件时,经常需要将某些数据导出,本文介绍了在Winform或Asp.net中使用NPOI(POI 项目的 .NET 版本)来操作Excel文件,而无需安装Office. 首先,需要获取NP ...
- Asp.Net 使用Npoi导出Excel
引言 使用Npoi导出Excel 服务器可以不装任何office组件,昨天在做一个导出时用到Npoi导出Excel,而且所导Excel也符合规范,打开时不会有任何文件损坏之类的提示.但是在做导入时还是 ...
- C# NPOI导出Excel和EPPlus导出Excel比较
系统中经常会使用导出Excel的功能. 之前使用的是NPOI,但是导出数据行数多就报内存溢出. 最近看到EPPlus可以用来导出Excel,就自己测了下两者导出上的差异. NPIO官网地址:http: ...
- NPOI导出EXCEL 打印设置分页及打印标题
在用NPOI导出EXCEL的时候设置分页,在网上有查到用sheet1.SetRowBreak(i)方法,但一直都没有起到作用.经过研究是要设置 sheet1.FitToPage = false; 而 ...
- .NET NPOI导出Excel详解
NPOI,顾名思义,就是POI的.NET版本.那POI又是什么呢?POI是一套用Java写成的库,能够帮助开发者在没有安装微软Office的情况下读写Office的文件. 支持的文件格式包括xls, ...
随机推荐
- GDAL重投影重采样像元配准对齐
研究通常会涉及到多源数据,需要进行基于像元的运算,在此之前需要对数据进行地理配准.空间配准.重采样等操作.那么当不同来源,不同分辨率的数据重采样为同一空间分辨率之后,各个像元不一一对应,有偏移该怎么办 ...
- odoo14 继承改写原生模块的视图优先级问题
需要类似这样的改写方法: 1.更改id名,方便下方引用!!! 2.使用原生模块的"model"!!! 3.添加字段priority!!!(越小越大) 4.form 和 kanban ...
- 如何把Electron做成一个Runtime,让多个应用共享同一个Electron
这个问题涉及到很多知识,而且要想把这个Runtime做好很绕. 下面我就说一下我的思路:(以下内容以Windows平台为基础,Mac平台和Linux平台还得去调查一下,才能确定是否可行) 首先,我们先 ...
- js中字符串和数组的常用转换处理方法
1.split("分割条件(正则表达式或者字符)") 字符串 ==> 数组 默认返回数组 (1) 将单词分割为字符 "hello".split(" ...
- 【LeetCode】840. Magic Squares In Grid 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 利用河图规律 暴力解法 日期 题目地址:https: ...
- PowerDotNet平台化软件架构设计与实现系列(10):文件平台
很多业务系统少不了需要进行文件管理,比如各种图片.excel.pdf.压缩包等等,为了高度可复用,我们抽象出文件平台,加强对文件进行管理. PowerDotNet文件平台目前支持阿里云OSS.Fast ...
- iGPT and ViT
目录 概 主要内容 iGPT ViT 代码 Chen M., Radford A., Child R., Wu J., Jun H., Dhariwal P., Luan D., Sutskever ...
- 台湾旺玖MA8601|USB HUB方案|MA8601测试版
MA8601是USB 2.0高速4端口集线器控制器的高性能解决方案,完全符合通用串行总线规范2.0.MA8601继承了先进的串行接口技术,当4个DS(下游)端口同时工作时,功耗最低. MA8601采用 ...
- spring练习,使用Eclipse搭建的Spring开发环境,属性注入通过构造方法方式实现,模拟用户的正常登录。
相关 知识 >>> 相关 练习 >>> 实现要求: 使用Eclipse搭建的Spring开发环境,属性注入通过构造方法方式实现,模拟用户的正常登录.要求如下: 通过 ...
- Linux 安装并启用 PHP-FPM
首先,在编译时带上 --enable-fpm 参数: [root@localhost local]# yum -y install libxml2 libxml2-devel gd gd-devel ...