NPOI控件的使用导出excel文件和word文件
public HttpResponseMessage GetReportRateOutput(DateTime? begin_time = null, DateTime? end_time = null, string type = "大浮标")
{
var dataList = _adapter.DataReportRate(type, begin_time, end_time).ToList(); NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("“" + type + "”到报率统计"); // 第一列
NPOI.SS.UserModel.IRow row = sheet.CreateRow();
row.CreateCell().SetCellValue("站名");
row.CreateCell().SetCellValue("应到报数");
row.CreateCell().SetCellValue("叶绿素到报率");
row.CreateCell().SetCellValue("气压到报率");
row.CreateCell().SetCellValue("风速到报率");
row.CreateCell().SetCellValue("气温到报率");
row.CreateCell().SetCellValue("水温到报率");
row.CreateCell().SetCellValue("波高到报率");
row.CreateCell().SetCellValue("盐度到报率"); NPOI.SS.UserModel.ICellStyle style = book.CreateCellStyle();
style.Alignment = HorizontalAlignment.Center;
style.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.DarkBlue.Index;
style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.DarkBlue.Index;
row.GetCell().CellStyle = style;
row.GetCell().CellStyle = style;
row.GetCell().CellStyle = style;
row.GetCell().CellStyle = style;
row.GetCell().CellStyle = style;
row.GetCell().CellStyle = style;
row.GetCell().CellStyle = style;
row.GetCell().CellStyle = style;
row.GetCell().CellStyle = style;
sheet.SetColumnWidth(, * );
sheet.SetColumnWidth(, * );
sheet.SetColumnWidth(, * );
sheet.SetColumnWidth(, * );
sheet.SetColumnWidth(, * );
sheet.SetColumnWidth(, * );
sheet.SetColumnWidth(, * );
sheet.SetColumnWidth(, * );
sheet.SetColumnWidth(, * ); int index = ;
foreach (DataTransferStatus dataInfo in dataList)
{
// 第二列
NPOI.SS.UserModel.IRow row2 = sheet.CreateRow(index); row2.CreateCell().SetCellValue(dataInfo.name);
row2.CreateCell().SetCellValue(dataInfo.report_number);
row2.CreateCell().SetCellValue(dataInfo.phyll_report_rate + "%");
row2.CreateCell().SetCellValue(dataInfo.pressure_report_rate + "%");
row2.CreateCell().SetCellValue(dataInfo.speed_report_rate + "%");
row2.CreateCell().SetCellValue(dataInfo.temp_report_rate + "%");
row2.CreateCell().SetCellValue(dataInfo.water_report_rate + "%");
row2.CreateCell().SetCellValue(dataInfo.wave_report_rate + "%");
row2.CreateCell().SetCellValue(dataInfo.salt_report_rate + "%"); index++;
}
System.IO.MemoryStream stream = new System.IO.MemoryStream();
book.Write(stream);
stream.Seek(, SeekOrigin.Begin);
book = null;
HttpResponseMessage mResult = new HttpResponseMessage(System.Net.HttpStatusCode.OK);
mResult.Content = new StreamContent(stream);
mResult.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
mResult.Content.Headers.ContentDisposition.FileName = type + "_到报率统计" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls";
mResult.Content.Headers.ContentType = new MediaTypeHeaderValue("application/ms-excel"); return mResult;
}
1.首先导入NPOI的dll,这个在网上有很多自行下载。先去官网:http://npoi.codeplex.com/下载需要引入dll(可以选择.net2.0或者.net4.0的dll),然后在网站中添加引用。
2.引用命名空间 using NPOI.SS.UserModel
3.(此处以导出excel为例)我们要明白一个完整的excel文件是由哪几部分组成的!
(1)一张工作薄BOOK,一张工作表sheet,然后包括ROW行,Column列,Cell单元格
4.
分别创建出每一部分
NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();//
NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("“" + type + "”到报率统计");
NPOI.SS.UserModel.IRow row = sheet.CreateRow(0); 设置excel文件的样式
NPOI.SS.UserModel.ICellStyle style = book.CreateCellStyle();
style.Alignment = HorizontalAlignment.Center;
style.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.DarkBlue.Index;
style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.DarkBlue.Index;
NPOI控件的使用导出excel文件和word文件的更多相关文章
- MVC无刷新查询,PagedList分页控件使用,导出Excel
使用MVC开发也有一段时间了,总结下无刷新部分视图的使用.PagedList分页控件的使用. @using PagedList @model StaticPagedList<T> < ...
- 如何在CRichEditCtrl控件中直接读如RTF格式的文件(这个是通过流的方式来读取文件)
如何在CRichEditCtrl控件中直接读如RTF格式的文件 Inserting an RTF string using StreamIn ------------------------- ...
- 使用input:file控件在微信内置浏览器上传文件返回未显示选择的文件
使用input:file控件在微信内置浏览器上传文件返回未显示选择的文件 原来的写法: <input type="file" accept="image/x-png ...
- C#仪器数据文件解析-Word文件(doc、docx)
不少仪器数据报告输出为Word格式文件,同Excel文件,Word文件doc和docx的存储格式是不同的,相应的解析Word文件的方式也类似,主要有以下方式: 1.通过MS Word应用程序的DCOM ...
- 基于C#语言MVC框架NPOI控件导出Excel表数据
控件bin文件下载地址:https://download.csdn.net/download/u012949335/10610726@{ ViewBag.Title = "dcxx" ...
- 使用Aspose.Cell控件实现多个Excel文件的合并
之前有写过多篇关于使用Apose.Cell控件制作自定义模板报表和通用的导出Excel表格数据的操作,对这个控件的功能还是比较满意,而且也比较便利.忽然有一天,一个朋友说:你已经有生成基于自定义模板报 ...
- 将C#datagridview控件的数据导出到Excel中
1.添加引用Microsoft.Office.Interop.Excel. 2.程序代码引用using Excel = Microsoft.Office.Interop.Excel; 3.控件事件代码 ...
- DevExpress ASPxHtmlEditor控件格式化并导出Word (修复中文字体导出丢失)
在前台页面中先插入一个ASPxHtmlEditor控件,名为ASPxHtmlEditor1. 我用的Dev版本为14.1 格式化文本 在后台插入如下代码 1 const string css ...
- 使用NPOI将数据库里信息导出Excel表格并提示用户下载
使用NPOI进行导出Excel表格大家基本都会,我在网上却很少找到导出Excel表格并提示下载的 简单的代码如下 //mvc项目可以传多个id以逗号相隔的字符串 public ActionResult ...
随机推荐
- MVC3中 ViewBag、ViewData和TempData的使用和区别(转发:汴蓝)
MVC3中 ViewBag.ViewData和TempData的使用和区别 在MVC3开始,视图数据可以通过ViewBag属性访问,在MVC2中则是使用ViewData.MVC3中保留了ViewD ...
- mysql中explain优化分析
效率比较 range >index > all
- Jquery中$.post()与$.get()区别
1:GET访问 浏览器 认为 是等幂的 就是 一个相同的URL 只有一个结果[相同是指 整个URL字符串完全匹配] 所以 第二次访问的时候 如果 URL字符串没变化 浏览器是 直接拿出了第一次访问的结 ...
- Python自动化运维之18、Python操作 MySQL、pymysql、SQLAchemy
一.MySQL 1.概述 什么是数据库 ? 答:数据的仓库,和Excel表中的行和列是差不多的,只是有各种约束和不同数据类型的表格 什么是 MySQL.Oracle.SQLite.Access.MS ...
- Python Django开始
1.创建工程 C:\procedure\projects>django-admin startproject mysite 2.同步数据库 C:\procedure\projects\mysit ...
- bzoj2534: Uva10829L-gap字符串
Description 有一种形如uvu形式的字符串,其中u是非空字符串,且V的长度正好为L,那么称这个字符串为L-Gap字符串 给出一个字符串S,以及一个正整数L,问S中有多少个L-Gap子串. I ...
- android.support.design.widget.AppBarLayout 在android5.0+底部显示空白条问题
在最外层使用 RelativeLayout作为根节点,同时设置 android:fitsSystemWindows="true"问题解决. <?xml version=&qu ...
- Shortest Prefixes
poj2001:http://poj.org/problem?id=2001 题意:给你一些单词,然后让你寻找每个单词的一个前缀,这个前缀能够唯一表示这个单词,并且是最短的. 题解:直接用trie树来 ...
- Ubiquitous Religions
http://poj.org/problem?id=2524 这道题就是并查集. #include<cstdio> #include<cstring> #include< ...
- Linux&shell之显示数据
写在前面:案例.常用.归类.解释说明.(By Jim) 2>将STDEER输入到一个文件1>将STDOUT输入到一个文件&>将STDEER和STDOUT输入到同一个文件 在脚 ...