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. 疯狂学习java web4(jsp)

    JSP与PHP.ASP.ASP.NET等语言类似,运行在服务端的语言. JSP(全称Java Server Pages)是由Sun Microsystems公司倡导和许多公司参与共同创建的一种使软件开 ...

  2. 深入浅出理解QTimeLine类

    网上找了下QTimeLIne类的介绍,要么就是代码一贴自己看去,要么就是说不到重点,正巧自己项目遇到这个类,在这里写一下,给需要的同学看下. 因为我最近需要有动画方面配合时间间隔触发QGraphics ...

  3. javascript 键值对

    <script type="text/javascript"> var arr = new Array(); arr['cn'] = '中国'; arr['usa'] ...

  4. Yii2的相关学习记录,初始化Yii2(二)

    前面已经将Yii2下载下来了,那我们就需要能实际的使用. 一.初始化,因为我都是在windows系统下,所以用cmd命令打开下载下来的Yii2的根目录.然后运行下面命令: init 会提示选择0为开发 ...

  5. jquery获取元素方式

    1 从集合中通过指定的序号获取元素 html: <div> <p>0</p> <p>1</p> <p>2</p> & ...

  6. python序列化之pickle

    来自引用: 1.什么东西能用pickle模块存储? 所有Python支持的 原生类型 : 布尔, 整数, 浮点数, 复数, 字符串, bytes(字节串)对象, 字节数组, 以及 None. 由任何原 ...

  7. Keil C51中变量的使用

    引言 8051内核单片机是一种通用单片机,在国内占有较大的市场份额.在将C语言用于51内核单片机的研究方面,Keil公司做得最为成功.由于51内核单片机的存储结构的特殊性,Keil C51中变量的使用 ...

  8. 使用MFC开发有十多年了,结合自身的体会,随便说几句(不能样样都依赖别人,C体系的人,绝对不怕人踢馆)

    挺长时间了吧,这个帖子还没沉下去,使用MFC开发有十多年了,结合自身的体会,随便说几句:1.MFC是一个C++的基础类库,封装了绝大多数的API函数,主要是用来创建带UI的应用程序,服务端程序或着不带 ...

  9. java签名证书

    import java.io.FileInputStream; import java.security.KeyStore; import java.security.PrivateKey; impo ...

  10. LaTeX排版工具使用

    专业的论文,都是用Latex.CTex等相关的工具.那么,用word写论文,缺点在哪? latex 写的东西,最终要编译成pdf格式的.里面的格式,尤其是数学类符号等,比较漂亮.这是word不能比的. ...