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文件的更多相关文章

  1. MVC无刷新查询,PagedList分页控件使用,导出Excel

    使用MVC开发也有一段时间了,总结下无刷新部分视图的使用.PagedList分页控件的使用. @using PagedList @model StaticPagedList<T> < ...

  2. 如何在CRichEditCtrl控件中直接读如RTF格式的文件(这个是通过流的方式来读取文件)

    如何在CRichEditCtrl控件中直接读如RTF格式的文件   Inserting an RTF string using StreamIn   ------------------------- ...

  3. 使用input:file控件在微信内置浏览器上传文件返回未显示选择的文件

    使用input:file控件在微信内置浏览器上传文件返回未显示选择的文件 原来的写法: <input type="file" accept="image/x-png ...

  4. C#仪器数据文件解析-Word文件(doc、docx)

    不少仪器数据报告输出为Word格式文件,同Excel文件,Word文件doc和docx的存储格式是不同的,相应的解析Word文件的方式也类似,主要有以下方式: 1.通过MS Word应用程序的DCOM ...

  5. 基于C#语言MVC框架NPOI控件导出Excel表数据

    控件bin文件下载地址:https://download.csdn.net/download/u012949335/10610726@{ ViewBag.Title = "dcxx" ...

  6. 使用Aspose.Cell控件实现多个Excel文件的合并

    之前有写过多篇关于使用Apose.Cell控件制作自定义模板报表和通用的导出Excel表格数据的操作,对这个控件的功能还是比较满意,而且也比较便利.忽然有一天,一个朋友说:你已经有生成基于自定义模板报 ...

  7. 将C#datagridview控件的数据导出到Excel中

    1.添加引用Microsoft.Office.Interop.Excel. 2.程序代码引用using Excel = Microsoft.Office.Interop.Excel; 3.控件事件代码 ...

  8. DevExpress ASPxHtmlEditor控件格式化并导出Word (修复中文字体导出丢失)

    在前台页面中先插入一个ASPxHtmlEditor控件,名为ASPxHtmlEditor1. 我用的Dev版本为14.1 格式化文本 在后台插入如下代码  1     const string css ...

  9. 使用NPOI将数据库里信息导出Excel表格并提示用户下载

    使用NPOI进行导出Excel表格大家基本都会,我在网上却很少找到导出Excel表格并提示下载的 简单的代码如下 //mvc项目可以传多个id以逗号相隔的字符串 public ActionResult ...

随机推荐

  1. jquey复选框三级分类关联一二级分类

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Category_Manag ...

  2. localstorage || globalStorage || userData

    globalStorage 这个也是html5中提出来,在浏览器关闭以后,使用globalStorage存储的信息仍能够保留下来,并且存储容量比IE的userdata大得多,一个域下面是5120k.和 ...

  3. Nginx源码研究八:nginx监听socket实现流程

    前面描述了nginx系统分析nginx的配置文件,初始化模块相关参数的过程,这里利用nginx监听socket的实现过程,做一次完整的回顾 1.首先,nginx启动的main函数中,会先初始化cycl ...

  4. linux修改环境变量

    /etc/profile 系统全局环境变量设定,所有用户共享,修改后,需要重启系统才能生效 ~/.bash_profile,~/.bashrc 用户目录下的私有环境变量设定,常用来个性化定制功能,修改 ...

  5. hdu 1241 Oil Deposits(DFS求连通块)

    HDU 1241  Oil Deposits L -DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & ...

  6. TCP回射客户程序:str_cli函数

    str_cli函数完成客户处理循环: 从标准输入读入一行文本,写到服务器上,读回服务器对该行的回射,并把回射行写到标准输出上 读入一行,写到服务器 fgets读入一行文本,writen把该行发送给服务 ...

  7. libcurl 下载上传

    近来一个新的项目需要使用到http. 本来用socket来写一个的,后来发现功能实在太简单,有点捉襟见肘. 于是改用libcur来做. 首先下载libcur的源码,然后配置: ./configure ...

  8. Xamarin Studio –Project not built in active configuration

    当我们加载项目以后如果出现以下项目提示 处理方式如下: 解决方案右键->options 配置->configuration mappings->勾选构建的ios项目 项目右键-> ...

  9. redis 扩展安装使用

    Redis的php客户端库非常之多, Redis推荐客户端链接是:http://redis.io/clients 推荐用phpredis,下载地址:https://github.com/nicolas ...

  10. 转:Yii实战中8个必备常用的扩展,模块和widget

    转载自:http://www.yiiframework.com/wiki/180/yii8/ 在经过畅K网的实战后,总结一下在Yii的项目中会经常用到的组件和一些基本的使用方法,分享给大家,同时也给自 ...