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, ...
随机推荐
- CF1501A Alexey and Train 题解
Content 一列火车从 \(0\) 时刻开始从 \(1\) 号站出发,要经过 \(n\) 个站,第 \(i\) 个站的期望到达时间和离开时间分别为 \(a_i\) 和 \(b_i\),并且还有一个 ...
- java 图形化小工具Abstract Window Toolit ;布局管理器FlowLayout流式布局;BorderLayout边界布局;GridLayout网格布局;CardLayou重叠卡片布局;BoxLayout方框布局;绝对定位
1.FlowLayout流式布局管理器: FlowLayout布局管理器中,组件像水流一样向某方向流动(排列),遇到障碍(边界)就折回,重头开始排列 .在默认情况下,FlowLayout局管理器从左向 ...
- Python3 shevel模块,更高级的json序列化数据类型模块(比pickle更高级)
直接将数据类型以字典的格式 存到文件中去. 直接.get读取出来,
- powerdesigner给列加上注释步骤
powerdesigner给列加上注释步骤如图:
- Spring Boot程序插入时间和MySQL数据库显示时间不一样(设置数据库时区)
首先查看数据库时区 show variables like "%time_zone%"; # 设置全局时区 mysql> set global time_zone = '+8 ...
- Qt5绘制仪表盘dashboard
说明 本文演示Qt版本: Qt5.14. 本文将使用QPainter一步一步绘制仪表盘:刻度.指针.刻度值 注意: 绘制顺序,如果先绘制,则后来绘制的将会覆盖住先前绘制的. 如果需要绘制半透明, 请设 ...
- win10+vs2015编译nanogui
!!版权声明:本文为博主原创文章,版权归原文作者和博客园共有,谢绝任何形式的 转载!! 作者:mohist 说明: A.OS : win10 1909 ,没有配置OpenGL开发环境的 经历. B. ...
- 【LeetCode】40. Combination Sum II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:ht ...
- Anti-prime Sequences
Anti-prime Sequences Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 3355 Accepted: 1 ...
- Spring MVC 文件上传、Restful、表单校验框架
目录 文件上传 Restful Restful 简介 Rest 行为常用约定方式 Restful开发入门 表单校验框架 表单校验框架介绍 快速入门 多规则校验 嵌套校验 分组校验 综合案例 实用校验范 ...