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, ...
随机推荐
- CF1547B Alphabetical Strings 题解
Content 我们有一个空的字符串,第 \(i\) 次操作我们可以将字母表中第 \(i\) 个字母加入字符串的最前面或最后面.我们称一个长度为 \(n\) 的字符串是合法的,当且仅当这个字符串可以通 ...
- Vue页面内公共的多类型附件图片上传区域并适用折叠面板
在前端项目中,附件上传是很常用的功能,几乎所有的app相关项目中都会使用到,一般在选择使用某个前端UI框架时,可以查找其内封装好的图片上传组件,但某些情况下可能并不适用于自身的项目需求,本文中实现的附 ...
- Spring Boot程序插入时间和MySQL数据库显示时间不一样(设置数据库时区)
首先查看数据库时区 show variables like "%time_zone%"; # 设置全局时区 mysql> set global time_zone = '+8 ...
- c++设计模式概述之状态
代码写的不够规范,目的是为了缩短篇幅,实际中请不要这样做 参看:https://www.runoob.com/design-pattern/state-pattern.html 1.概述 这个有点抽象 ...
- 【LeetCode】377. Combination Sum IV 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- <学习opencv>跨平台和本机windows
/*=========================================================================*/ // 跨平台和本机Windows /*=== ...
- 《MySQL数据操作与查询》- 综合项目 - 学生管理系统
<MySQL数据操作与查询>综合项目需求 一.系统整体功能 维护学生信息.老师信息和成绩信息. 支持按多种条件组合查询学生信息和成绩信息. 二.系统的信息需求 一个班级有一个讲师一个班主任 ...
- localstorage的浏览器支持情况
localStorage的兼容性不错,就国内的情况,已经基本没有问题了.localStorage的原理很简单,浏览器为每个域名划出一块本地存储空间,用户网页可以通过localStorage命名空间进行 ...
- 深入 Laravel 内核之IOC容器
升级工厂前的准备工作 无规矩不成方圆,随着越来越多的行为出现,我们需要需要定下一些规范. 为了约束每一个行为的规范,需要定义一个行为接口: interface BehaviorInterface { ...