//优化后导出excel
public System.IO.Stream ExcelStream(string search) //
{
var orderBusiniss = Containers.ObjectContainer.CreateObject<IOrderBusiness>();
var list = orderBusiniss.GetExcel(search); NPOI.HSSF.UserModel.HSSFWorkbook hssfworkbook = new NPOI.HSSF.UserModel.HSSFWorkbook(); NPOI.SS.UserModel.ISheet sheet1 = hssfworkbook.CreateSheet("生产计划"); //添加标题头
sheet1.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 13));
NPOI.SS.UserModel.IRow rowTitle = sheet1.CreateRow(0);
rowTitle.Height = 30 * 20;//设置行高
var cellTitel = rowTitle.CreateCell(0); //给标题头添加格式
NPOI.SS.UserModel.ICellStyle styleTitle = hssfworkbook.CreateCellStyle();
styleTitle.Alignment = HorizontalAlignment.Center;//单元格水平居中
IFont fontTitle = hssfworkbook.CreateFont();//新建一个字体样式对象
fontTitle.Boldweight = short.MaxValue;//设置字体加粗样式
fontTitle.FontHeightInPoints = 16;//设置字体大小
styleTitle.SetFont(fontTitle);//使用SetFont方法将字体样式添加到单元格样式中
cellTitel.CellStyle = styleTitle; cellTitel.SetCellValue(ApplicationContext.Current.CompanyName + "公司生产计划单"); //通过反射得到需要导出的属性
Type orderExcel = typeof(OrderToExcel);
PropertyInfo[] ps = orderExcel.GetProperties();
string[] title = new string[] {
"序号",
"生产时间" ,
"订单号",
"业务员",
"工程名称",
"建筑单位",
"施工部位",
"产品信息",
"特殊要求",
"方量(M3)",
"下单时间",
"联系人",
"联系电话",
"备注",
};
//添加行头
NPOI.SS.UserModel.IRow rowHeader = sheet1.CreateRow(1); //公共样式:加边框
NPOI.SS.UserModel.ICellStyle style = hssfworkbook.CreateCellStyle();
style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
style.Alignment = HorizontalAlignment.Center; NPOI.SS.UserModel.ICellStyle styleHeader = hssfworkbook.CreateCellStyle();
styleHeader.CloneStyleFrom(style);//克隆公共的样式
styleHeader.Alignment = HorizontalAlignment.Center;//单元格水平居中
IFont fontHeader = hssfworkbook.CreateFont();//新建一个字体样式对象
fontHeader.Boldweight = short.MaxValue;//设置字体加粗样式
styleHeader.SetFont(fontHeader);//使用SetFont方法将字体样式添加到单元格样式中 for (int i = 0; i < ps.Length + 1; i++)
{
var cellHeard = rowHeader.CreateCell(i);
cellHeard.CellStyle = styleHeader;
cellHeard.SetCellValue(title[i]);
} if (list != null)
{
//通过序列号,得到需要导出的字段
List<OrderToExcel> listExcel = list.ToObjectList<Order, OrderToExcel>();
//保存列集合的属性信息数组
PropertyInfo[] oProps = null;
for (int i = 0; i < listExcel.Count; i++)
{
NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 2); var cellNum = rowtemp.CreateCell(0, CellType.Numeric);
cellNum.CellStyle = style;
cellNum.SetCellValue(i + 1);
oProps = ((Type)listExcel[i].GetType()).GetProperties();
for (int j = 0; j < oProps.Length; j++)
{
Type colType = oProps[j].PropertyType;//得到属性的类型
var value = oProps[j].GetValue(listExcel[i]);//得到属性的值
var cell = rowtemp.CreateCell(j + 1);
cell.CellStyle = style;
cell.SetCellValue(value == null ? "" : value.ToString());
}
} } //自动调节行宽度
for (int i = 0; i < 15; i++)
sheet1.AutoSizeColumn(i); System.IO.MemoryStream file = new System.IO.MemoryStream();
hssfworkbook.Write(file);
file.Seek(0, System.IO.SeekOrigin.Begin); return file;
} public class OrderToExcel
{
public Nullable<System.DateTime> ProductionTime { get; set; }
public string Code { get; set; }
public string BSalesman { get; set; }
public string ProjectName { get; set; }
public string ConstructionUnit { get; set; }
public string ConstructionSite { get; set; }
public string ProductName { get; set; }
public string WP { get; set; }
public Nullable<decimal> Number { get; set; }
public Nullable<System.DateTime> CreationTime { get; set; }
public string ContactPerson { get; set; }
public string ContactPhone { get; set; }
public string Remarks { get; set; }
}

  

之前做了导出excel功能,没有样式比较难看。最近优化了下,NPOI版本为最新版本2.0.4.1版本

优化添加:

1.添加了标题。

2.通过对象的属性来导出excel,表头添加也比较容易点。

NPOI2.0导出excel之添加样式、边框和表头的更多相关文章

  1. C#+Aspose.Cells 导出Excel及设置样式 (Webform/Winform)

    在项目中用到,特此记录下来,Aspose.Cells 不依赖机器装没有装EXCEL都可以导出,很方便.具体可以参考其他 http://www.aspose.com/docs/display/cells ...

  2. C# 使用Epplus导出Excel [5]:样式

    C# 使用Epplus导出Excel [1]:导出固定列数据 C# 使用Epplus导出Excel [2]:导出动态列数据 C# 使用Epplus导出Excel [3]:合并列连续相同数据 C# 使用 ...

  3. wpf 导出Excel Wpf Button 样式 wpf简单进度条 List泛型集合对象排序 C#集合

    wpf 导出Excel   1 private void Button_Click_1(object sender, RoutedEventArgs e) 2 { 3 4 ExportDataGrid ...

  4. POI通用导出Excel数据(包括样式设计)

    前言 前一段时间我写过通用的导入Excel,前几天也写了导出pdf格式的,还有我之前搞得导出Word,我在之前的博客也都介绍了导出和导入是一个道理,无非是一个获取一个是赋值.昨天有一位同仁看了我的Ex ...

  5. vue2.0 导出Excel表格数据

    1.安装三个依赖包 npm install -S file-saver npm install -S xlsx npm install -D script-loader 2.在项目中创建一个文件夹(比 ...

  6. NPOI导出Excel,添加图片和设置格式,添加条形码

    先上代码 using grproLib; using System; using System.Collections.Generic; using System.Data; using System ...

  7. C# 导出Excel Table td 样式

    <td style="vnd.ms-excel.numberformat:@;"><s:property value="accountCode" ...

  8. HTML导出Excel数据类型转换样式参考

    mso-number-format:"0" NO Decimals mso-number-format:"0/.000" 3 Decimals mso-numb ...

  9. 使用aspose.cell导出excel需要注意什么?

    1.如果导出的数据源是汇总出来的,最好方法是将数据源放到缓存里面,当基本数据源变化的时候,在改变数据2.使用模板导出EXCEL,这样很多样式可以在模板文件里面直接设置,例如:默认打开页签,让列头固定3 ...

随机推荐

  1. solr的查询语法、查询参数、检索运算符

    转载自:http://martin3000.iteye.com/blog/1328931 1.查询语法 solr的一些查询语法 1.1. 首先假设我的数据里fields有:name, tel, add ...

  2. Etyma01 ced ceed cess

    一. etyma ['ɛtə,mə] ced.ceed.cess -> go -> 行走,前进 二.for instance 1. precede=pre+ced+e pre- 在前 2. ...

  3. 290. Word Pattern 单词匹配模式

    [抄题]: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...

  4. 高性能MySQL笔记-第5章Indexing for High Performance-001B-Tree indexes(B+Tree)

    一. 1.什么是B-Tree indexes? The general idea of a B-Tree is that all the values are stored in order, and ...

  5. 触摸屏、X11去掉鼠标

    cursor disable in X11 Last updated 8 years ago 摘自:http://www.noah.org/wiki/cursor_disable_in_X11 Whe ...

  6. SPOJ - BALNUM Balanced Numbers(数位dp+三进制状压)

    Balanced Numbers Balanced numbers have been used by mathematicians for centuries. A positive integer ...

  7. java基础之转义符、数据类型

    一.  转义符 1.\n \n的作用是换行,也就是和键盘上的回车键相同 2.\t \t的作用是制表,就是以八个空格为一个单位,当不足八个时会自动补齐八个,如asd\tfgh,那么输出的将会是 . 3. ...

  8. uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型

    在nesc的代码中,你会看到很多你不认识的数据类型,比如uint8_t等.咋一看,好像是个新的数据类型,不过C语言(nesc是C的扩展)里面好像没有这种数据类型啊!怎么又是u又是_t的?很多人有这样的 ...

  9. Serialization之BinaryFormatter

    前言 BinaryFormatter序列化二进制序列化使用二进制编码来生成精简的序列化,以用于存储或基于套接字的网络流等. 内容 下面通过一个小小的例子来给大家说明什么是BinaryFormatter ...

  10. Div的移动

    //JQuery 拖拽本体DIV,把以下代码全部复制即可 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...