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. IIS部署,发布网站

    一.IIS部署 1.打开控制面板,选择 '程序' 2.程序和功能下,选择打开或关闭Windows功能 3.等待加载,选择Internet信息服务,勾选如下选项 在弹出的"windows功能& ...

  2. C++实现反射---RTTR库的使用

    使用过C#或者Java 的童鞋,应该对这些语言提供的反射机制有所了解.所谓反射,在我看来就是在只知道一个类的名字(字符串形式)的情况下,自动创建出具体的类实例,并且能够枚举该类型拥有的属性.方法等信息 ...

  3. ACwing1211. 蚂蚁感冒

    题目: 长 100 厘米的细长直杆子上有 n 只蚂蚁. 它们的头有的朝左,有的朝右. 每只蚂蚁都只能沿着杆子向前爬,速度是 1 厘米/秒. 当两只蚂蚁碰面时,它们会同时掉头往相反的方向爬行. 这些蚂蚁 ...

  4. JENKINS中创建全局变量并在JOB中使用

    配置了一个 "PASSWORD"的变量值 然后再脚本里面使用   注意这里必须要用双引号 不然不行

  5. Linux(centos7)安装redis并设置redis开机自启动

    1.下载redis安装包 wget http://download.redis.io/releases/redis-4.0.6.tar.gz 2.解压安装包 tar -zxvf redis-4.0.6 ...

  6. C++实现二叉搜索书(参考算法导论)

    1 #include <iostream> 2 using namespace std; 3 4 struct node 5 { 6 // 数据域 7 int data; 8 9 // 左 ...

  7. Android NDK开发篇:Java与原生代码通信(数据操作)

    虽然说使用NDK可以提高Android程序的执行效率,但是调用起来还是稍微有点麻烦.NDK可以直接使用Java的原生数据类型,而引用类型,因为Java的引用类型的实现在NDK被屏蔽了,所以在NDK使用 ...

  8. C++基础之虚析构函数原理

    结论 虚函数表指针 + 虚函数表 共同实现 演示 VS2017(32位) 基类有析虚构函数 一段代码演示 #include <iostream> #include <memory&g ...

  9. 【LeetCode】822. Card Flipping Game 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/card-flip ...

  10. 1083 - Histogram

    1083 - Histogram   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 64 MB A hist ...