private string PushToDown(string addtime)
{
DataTable dt = _bCreateCode.PushtoExcel(addtime);
//1、实例化workbook工作簿对象
HSSFWorkbook hssfworkbook = new HSSFWorkbook();
//2、创建文档摘要信息
DocumentSummaryInformation dsf = PropertySetFactory.CreateDocumentSummaryInformation();
dsf.Company = "公司名称";//公司
dsf.Category = "类别";//类别
//CustomProperties 自定义属性
SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
si.Author = "作者";//作者
si.Subject = "序列号";//主题
si.Title = "序列号列表";//标题
//si.RevNumber = "1.0";//版本号
//3、将写好的文档摘要 赋值workbook对象
hssfworkbook.DocumentSummaryInformation = dsf;
hssfworkbook.SummaryInformation = si;
//4、创建Sheet
HSSFSheet sheet1 = (HSSFSheet)hssfworkbook.CreateSheet("Sheet1");
//HSSFSheet Sheet2 = (HSSFSheet)hssfworkbook.CreateSheet("Sheet2");
//HSSFSheet Sheet3 = (HSSFSheet)hssfworkbook.CreateSheet("Sheet3");
//5、创建页眉页脚
sheet1.CreateRow(0).CreateCell(1).SetCellValue(123);
sheet1.Header.Center = "统计数据";
sheet1.Header.Left = "logo.png";
sheet1.Header.Right = "address";
sheet1.Footer.Center = "page";
//6、标题
string yeartime = DateTime.Today.Year + "-" + DateTime.Today.Month + "-" + DateTime.Today.Day + "-" + DateTime.Now.Hour + "-" + DateTime.Now.Minute + "-" + DateTime.Now.Second; HSSFCell fcell = (HSSFCell)sheet1.CreateRow(0).CreateCell(0);//第一行
fcell.SetCellValue(addtime + "序列号列表");//文本
//合并单元格
sheet1.AddMergedRegion(new CellRangeAddress(0, 0, 0, 13));//2.0使用 2.0以下为Region
//标题样式
HSSFCellStyle fCellStyle = (HSSFCellStyle)hssfworkbook.CreateCellStyle();
HSSFFont ffont = (HSSFFont)hssfworkbook.CreateFont();
ffont.FontHeight = 20 * 20;
ffont.FontName = "宋体";
ffont.Color = HSSFColor.Black.Index;
fCellStyle.SetFont(ffont);
fCellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;//垂直对齐
fCellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;//水平对齐
fcell.CellStyle = fCellStyle; //7、设置单元格格式 创建单元格
/*模拟设定7列*/
HSSFDataFormat dataformat = (HSSFDataFormat)hssfworkbook.CreateDataFormat();//数据格式
HSSFFont font = (HSSFFont)hssfworkbook.CreateFont();//数据字体
font.Color = HSSFColor.Black.Index; //颜色
font.IsItalic = false;//斜体
font.IsStrikeout = false;//加粗
font.FontName = "宋体";//字体 //必不可少 可以变更在循环输出数据时指定类型 需要调用sqlDbType 较复杂
//Id int类型
HSSFCell cell1 = (HSSFCell)sheet1.CreateRow(1).CreateCell(0); //创建单元格
HSSFCellStyle cellStyle1 = (HSSFCellStyle)hssfworkbook.CreateCellStyle();//单元格样式
cellStyle1.DataFormat = HSSFDataFormat.GetBuiltinFormat("");
// CellRangeAddressList ranglist1 = new CellRangeAddressList(0, 65535, 0, 0);//集合限定类型
// DVConstraint constraint1 = DVConstraint.CreateNumericConstraint(DVConstraint.ValidationType.INTEGER, DVConstraint.OperatorType.BETWEEN, "0", "100");//约束
cellStyle1.SetFont(font);
cell1.CellStyle = cellStyle1;
cell1.SetCellValue(""); //Name
HSSFCell cell2 = (HSSFCell)sheet1.CreateRow(1).CreateCell(1);
HSSFCellStyle cellStyle2 = (HSSFCellStyle)hssfworkbook.CreateCellStyle();
cellStyle2.DataFormat = HSSFDataFormat.GetBuiltinFormat("");
cellStyle2.SetFont(font);
cell2.CellStyle = cellStyle2;
cell2.SetCellValue(""); //phone
HSSFCell cell3 = (HSSFCell)sheet1.CreateRow(1).CreateCell(2);
HSSFCellStyle cellStyle3 = (HSSFCellStyle)hssfworkbook.CreateCellStyle();
cellStyle3.DataFormat = HSSFDataFormat.GetBuiltinFormat("");
cellStyle3.SetFont(font);
cell3.CellStyle = cellStyle3;
cell3.SetCellValue(""); //address
HSSFCell cell4 = (HSSFCell)sheet1.CreateRow(1).CreateCell(3);
HSSFCellStyle cellStyle4 = (HSSFCellStyle)hssfworkbook.CreateCellStyle();
cellStyle4.DataFormat = HSSFDataFormat.GetBuiltinFormat("");
cellStyle4.SetFont(font);
cell4.CellStyle = cellStyle4;
cell4.SetCellValue(""); //Status
HSSFCell cell5 = (HSSFCell)sheet1.CreateRow(1).CreateCell(4);
HSSFCellStyle cellStyle5 = (HSSFCellStyle)hssfworkbook.CreateCellStyle();
cellStyle5.DataFormat = HSSFDataFormat.GetBuiltinFormat("");
cellStyle5.SetFont(font);
cell5.CellStyle = cellStyle5;
cell5.SetCellValue(""); //balance
HSSFCell cell6 = (HSSFCell)sheet1.CreateRow(1).CreateCell(5);
HSSFCellStyle cellStyle6 = (HSSFCellStyle)hssfworkbook.CreateCellStyle();
cell6.SetCellValue("");
cellStyle6.DataFormat = HSSFDataFormat.GetBuiltinFormat("");
cellStyle6.SetFont(font);
cell6.CellStyle = cellStyle6; //CreateDate
HSSFCell cell7 = (HSSFCell)sheet1.CreateRow(1).CreateCell(6);
HSSFCellStyle cellStyle7 = (HSSFCellStyle)hssfworkbook.CreateCellStyle();
cellStyle7.DataFormat = HSSFDataFormat.GetBuiltinFormat("");
cellStyle7.SetFont(font);
cell7.CellStyle = cellStyle7;
cell7.SetCellValue(""); HSSFCell cell8 = (HSSFCell)sheet1.CreateRow(1).CreateCell(7);
HSSFCellStyle cellStyle8 = (HSSFCellStyle)hssfworkbook.CreateCellStyle();
cellStyle8.DataFormat = HSSFDataFormat.GetBuiltinFormat("");
cellStyle8.SetFont(font);
cell8.CellStyle = cellStyle8;
cell8.SetCellValue(""); HSSFCell cell9 = (HSSFCell)sheet1.CreateRow(1).CreateCell(8);
HSSFCellStyle cellStyle9 = (HSSFCellStyle)hssfworkbook.CreateCellStyle();
cellStyle9.DataFormat = HSSFDataFormat.GetBuiltinFormat("");
cellStyle9.SetFont(font);
cell9.CellStyle = cellStyle9;
cell9.SetCellValue(""); HSSFCell cell10 = (HSSFCell)sheet1.CreateRow(1).CreateCell(9);
HSSFCellStyle cellStyle10 = (HSSFCellStyle)hssfworkbook.CreateCellStyle();
cellStyle10.DataFormat = HSSFDataFormat.GetBuiltinFormat("");
cellStyle10.SetFont(font);
cell10.CellStyle = cellStyle10;
cell10.SetCellValue(""); HSSFCell cell11 = (HSSFCell)sheet1.CreateRow(1).CreateCell(10);
HSSFCellStyle cellStyle11 = (HSSFCellStyle)hssfworkbook.CreateCellStyle();
cellStyle11.DataFormat = HSSFDataFormat.GetBuiltinFormat("");
cellStyle11.SetFont(font);
cell11.CellStyle = cellStyle11;
cell11.SetCellValue(""); HSSFCell cell12 = (HSSFCell)sheet1.CreateRow(1).CreateCell(11);
HSSFCellStyle cellStyle12 = (HSSFCellStyle)hssfworkbook.CreateCellStyle();
cellStyle12.DataFormat = HSSFDataFormat.GetBuiltinFormat("");
cellStyle12.SetFont(font);
cell12.CellStyle = cellStyle12;
cell12.SetCellValue(""); HSSFCell cell13 = (HSSFCell)sheet1.CreateRow(1).CreateCell(12);
HSSFCellStyle cellStyle13 = (HSSFCellStyle)hssfworkbook.CreateCellStyle();
cellStyle13.DataFormat = HSSFDataFormat.GetBuiltinFormat("");
cellStyle13.SetFont(font);
cell13.CellStyle = cellStyle13;
cell13.SetCellValue(""); //8、创建单元格 加入数据
HSSFRow r = (HSSFRow)sheet1.CreateRow(1);//第二行 标题
for (int i = 0; i < dt.Columns.Count; i++)
{
r.CreateCell(i).SetCellValue(dt.Columns[i].ToString());
}
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
HSSFRow row = (HSSFRow)sheet1.CreateRow(i + 2);//写入行
for (int j = 0; j < dt.Columns.Count; j++)//写入列
{
row.CreateCell(j).SetCellValue(dt.Rows[i][j].ToString());
}
}
} FileStream fs = new FileStream(Server.MapPath("~/PushtoExcel/" + yeartime + ".xls"), FileMode.Create);
hssfworkbook.Write(fs);
fs.Close();
return yeartime + ".xls";
}

asp.net mvc NPOI 生成Excel文件的更多相关文章

  1. asp.net 使用NPOI读取excel文件

    asp.net 使用NPOI读取excel文件内容 NPOI下载地址:NPOI public class ExcelHelper { /// <summary> /// 读取Excel文件 ...

  2. .net利用NPOI生成excel文件

    整理代码,这个是生成excel文件,用的是HSSF的方式,只能生成65535行,256列的数据,如果要看office07之后的生成,之前的随笔里提过.这个是一个完整的过程. 首先是已经查找好的数据,这 ...

  3. Aspose.Cell和NPOI生成Excel文件

    1.使用Aspose.Cell生成Excel文件,Aspose.Cell是.NET组件控件,不依赖COM组件 1首先一点需要使用新建好的空Excel文件做模板,否则容易产生一个多出的警告Sheet 1 ...

  4. ASP.NET MVC NPOI导入Excel DataTable批量导入到数据库

    使用NPOI导入Excel 首先在MVC项目中导入NPOI 查询NPOI安装,排序依据,选择:最高下载量,选择第一个. 在控制器中创建ExcelController 在Index视图中写入代码: @u ...

  5. asp.net MVC NPOI导出excel通用

    一.创建一个类文件添加 public class ExportToExcelColumn { public ExportToExcelColumn(string _Columnnames, strin ...

  6. asp.net mvc + javascript生成下载文件

    近期做的是对现有项目进行重构.WEB FROM改成MVC,其实也算是推倒重来了. 里面有一个导出功能,将数据输出成txt文件,供下载.原先的做法是有一个隐藏的iframe,在这个iframe的页面中设 ...

  7. Asp.net MVC NPOI导出Excel

    public class NpoiMemoryStream : MemoryStream { public NpoiMemoryStream() { AllowClose = true; } publ ...

  8. ASP.NET MVC - NPOI读取Excel

    引入: using System; using System.Data; using System.IO; using NPOI.SS.UserModel; using NPOI.XSSF.UserM ...

  9. Aspose.Cell和NPOI生成Excel文件2

    NPOI还是比较好用的,引用dll程序集即可 1创建workbook和工作流 HSSFWorkbook workbook = new HSSFWorkbook(); MemoryStream ms = ...

随机推荐

  1. nginx自定义模块编写-实时统计模块--转载

    原文:http://www.vimer.cn/2012/05/nginx%E8%87%AA%E5%AE%9A%E4%B9%89%E6%A8%A1%E5%9D%97%E7%BC%96%E5%86%99- ...

  2. Android(java)学习笔记120:Android中的Application类用法

    1.简介 如果想在整个应用中使用全局变量,在java中一般是使用静态变量,public类型:而在android中如果使用这样的全局变量就不符合Android的框架架构,但是可以使用一种更优雅的方式就是 ...

  3. percona-toolkit工具检查MySQL复制一致性及修复

    利用percona-toolkit工具检查MySQL数据库主从复制数据的一致性,以及修复. 一.             pt-table-checksum检查主从库数据的一致性 pt-table-c ...

  4. hackerrank Day15: Linked List

    #include <iostream> #include <cstddef> using namespace std; class Node { public: int dat ...

  5. JAVA构造器、this、super

    构造器是为了创建一个类的实例.这个过程也可以在创建一个对象的时候用到: Platypus p1 = new Platypus(); 相反,方法的作用是为了执行java代码. 修饰符,返回值和命名的不同 ...

  6. activiti源码解读之心得整编

    TaskService.completeTask()的执行内幕是啥? activiti采取了command模式,completeTask会被包装成一个CompleteTaskCmd,一个Cmd执行的时 ...

  7. 搭建高可用的MongoDB集群

    http://www.csdn.net/article/2014-04-09/2819221-build-high-avialable-mongodb-cluster-part-1/1 在大数据的时代 ...

  8. sharepoint 删除list里的所有内容

    [System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") $siteUrl = " ...

  9. 20160526-20160531mybatis入门进阶

    mybatis第二天  高级映射 查询缓存 和spring整合 课程复习: mybatis是什么? mybatis是一人持久层框架,mybatis是一个不完全的ORM框架.sql语句需要程序员自己去编 ...

  10. zxing.dll生成条码

    引入zxing.dll using System; using System.Drawing; using ZXing.QrCode; using ZXing; using ZXing.Common; ...