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导出例子的更多相关文章

  1. NPOI导出多表头Execl(通过html表格遍历表头)

    关于NPOI的相关信息,我想博客园已经有很多了,而且NPOI导出Execl的文章和例子也很多,但导出多表头缺蛮少的:今天要讲的通过自己画html表格:通过html表格来导出自定义的多表头: 先来看要实 ...

  2. NPOI 导出添加批注功能

    这个问题在网上搜,都是说如下即可: //添加批注HSSFPatriarch patr = (HSSFPatriarch)sheet.CreateDrawingPatriarch();HSSFComme ...

  3. NPOI导出Excel (C#) 踩坑 之--The maximum column width for an individual cell is 255 charaters

    /******************************************************************* * 版权所有: * 类 名 称:ExcelHelper * 作 ...

  4. 基于NPOI导出和导入Excel

    概述 NPOI,顾名思义,就是POI的.NET版本.NPOI就是用.NET语言编写的一套数据导出Excel的开源项目,支持XML.xls.xlsx.ppt等格式..NET不仅实现Excel导出还可以实 ...

  5. (C#)使用NPOI导出Excel

    在做业务型的软件时,经常需要将某些数据导出,本文介绍了在Winform或Asp.net中使用NPOI(POI 项目的 .NET 版本)来操作Excel文件,而无需安装Office. 首先,需要获取NP ...

  6. Asp.Net 使用Npoi导出Excel

    引言 使用Npoi导出Excel 服务器可以不装任何office组件,昨天在做一个导出时用到Npoi导出Excel,而且所导Excel也符合规范,打开时不会有任何文件损坏之类的提示.但是在做导入时还是 ...

  7. C# NPOI导出Excel和EPPlus导出Excel比较

    系统中经常会使用导出Excel的功能. 之前使用的是NPOI,但是导出数据行数多就报内存溢出. 最近看到EPPlus可以用来导出Excel,就自己测了下两者导出上的差异. NPIO官网地址:http: ...

  8. NPOI导出EXCEL 打印设置分页及打印标题

    在用NPOI导出EXCEL的时候设置分页,在网上有查到用sheet1.SetRowBreak(i)方法,但一直都没有起到作用.经过研究是要设置  sheet1.FitToPage = false; 而 ...

  9. .NET NPOI导出Excel详解

    NPOI,顾名思义,就是POI的.NET版本.那POI又是什么呢?POI是一套用Java写成的库,能够帮助开发者在没有安装微软Office的情况下读写Office的文件. 支持的文件格式包括xls, ...

随机推荐

  1. CF151B Phone Numbers 题解

    Content 在一座城市中,每个人的电话号码都是由六位整数组成的,例如 11-45-14. 现在有 \(n\) 个人,第 \(i\) 个人有 \(s_i\) 个人的电话号码.已知: 出租车司机的电话 ...

  2. python 字符编码讲解

    ANSI不是一种具体的编码格式 ANSI在中文Windows操作系统代码指的是GBK编码 ANSI在中文Mac操作系统代码指的是UTF-8编码 ANSI在其他国家的操作系统中有其他的编码格式 #ASC ...

  3. href超级链接里加上javascript代码的,还有target问题

    href超级链接里加上javascript代码的,还有target问题 得把target="_blank"去掉才好用,在google浏览器有这个也没事,但是在Ie里有这个就不行了

  4. Jenkins安装部署使用图文详解(非常详细)

    前言 最近公司需要弄一套自动化运维部署,于是抽空学习了一下,用了两天左右完成Jenkins的安装部署和各种项目的配置化,于是整理一下进行分享. 介绍 Jenkins是一个独立的开源软件项目,是基于Ja ...

  5. Java 中的5个代码性能提升技巧,最高提升近10倍

    文章持续更新,可以关注公众号程序猿阿朗或访问未读代码博客. 本文 Github.com/niumoo/JavaNotes 已经收录,欢迎Star. 这篇文章介绍几个 Java 开发中可以进行性能优化的 ...

  6. tomcat startup.bat 启动中文显示乱码

    打开tomcat文件夹到conf目录下 修改logging.properties 找到 java.util.logging.ConsoleHandler.encoding = utf-8这行 更改为 ...

  7. summernote富文本图片上传,增加视频上传功能、批量上传方法

    Summernote 是一个简单灵活的所见即所得的 HTML 在线编辑器,基于 jQuery 和 Bootstrap 构建,支持快捷键操作,提供大量可定制的选项. 但是却只有图片上传功能,没有视频上传 ...

  8. 【LeetCode】14. Longest Common Prefix 最长公共前缀

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...

  9. 【LeetCode】328. Odd Even Linked List 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  10. 【LeetCode】707. Design Linked List 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...