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 ...
随机推荐
- TalkingData游戏版本在Cocos2d-x 3.0使用
Cocos2dx在3.0的版本中改动确实不少啊,所以导致原来可以在Cocos2.x版本上的demo都不能直接用,所以不得不重要写一个新的demo 但是TalkingData的库一直都是可以用的,只是之 ...
- Nginx源码研究五:NGINX的配置信息管理
配置信息是nginx系统重要的组成部分,配置信息的使用,实际上包含两层,一层是用户针对参数定义了值,例如下面nginx参数文件中的 keepalive_timeout 65,还有一部分是用户没有定义值 ...
- 小tip:我是如何初体验uglifyjs压缩JS的
by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=2946 一.故事总有其 ...
- Flask学习记录之Flask-Login
Flask-Loging 可以方便的管理用户会话,保护路由只让认证用户访问 http://flask-login.readthedocs.org/en/latest/ 一.初始化Flask-Login ...
- SQLServer -- 递归查询树结构表
需求 查询树结构表(如菜单类型表,文件夹类型表等)中一个数据节点下所有的子节点数据 声明临时表,并写入数据如下 DECLARE @tbl TABLE ( Id INT ,ParentId INT ,N ...
- bzoj4160: [Neerc2009]Exclusive Access 2
Description 给出 N 个点M 条边的无向图,定向得到有向无环图,使得最长路最短. N ≤ 15, M ≤ 100 Input 第一行一个数M (1≤M≤100). 接下来M行,每行两个大写 ...
- mongose排序查询
Kc.find({bjid:req.params.bjid}).sort({'_id':1}).exec(function(err,kcs){ if(err){ res.json({no:0,msg: ...
- hibernate之CRUD操作
CRUD是指在做计算处理时的增加(Create).读取(Retrieve)(重新得到数据).更新(Update)和删除(Delete)几个单词的首字母简写. 下面列举实例来讲解这几个操作: 实体类: ...
- -_-#【Canvas】FPS
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 【4】JAVA---地址App小软件(UpdatePanel.class)(表现层)
修改地址信息的一个表现层类. 必须选中地址,才能修改,否则会弹出窗口提示, 修改地址界面: /* * UpdatePanel.java * */ package cn.hncu.addr.ui; im ...