NPOI控件的使用导出excel文件和word文件

 1 public HttpResponseMessage GetReportRateOutput(DateTime? begin_time = null, DateTime? end_time = null, string type = "大浮标")
 2         {
 3             var dataList = _adapter.DataReportRate(type, begin_time, end_time).ToList();
 4
 5             NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
 6             NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("“" + type + "”到报率统计");
 7
 8             // 第一列
 9             NPOI.SS.UserModel.IRow row = sheet.CreateRow(0);
10             row.CreateCell(0).SetCellValue("站名");
11             row.CreateCell(1).SetCellValue("应到报数");
12             row.CreateCell(2).SetCellValue("叶绿素到报率");
13             row.CreateCell(3).SetCellValue("气压到报率");
14             row.CreateCell(4).SetCellValue("风速到报率");
15             row.CreateCell(5).SetCellValue("气温到报率");
16             row.CreateCell(6).SetCellValue("水温到报率");
17             row.CreateCell(7).SetCellValue("波高到报率");
18             row.CreateCell(8).SetCellValue("盐度到报率");
19
20             NPOI.SS.UserModel.ICellStyle style = book.CreateCellStyle();
21             style.Alignment = HorizontalAlignment.Center;
22             style.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.DarkBlue.Index;
23             style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.DarkBlue.Index;
24             row.GetCell(0).CellStyle = style;
25             row.GetCell(1).CellStyle = style;
26             row.GetCell(2).CellStyle = style;
27             row.GetCell(3).CellStyle = style;
28             row.GetCell(4).CellStyle = style;
29             row.GetCell(5).CellStyle = style;
30             row.GetCell(6).CellStyle = style;
31             row.GetCell(7).CellStyle = style;
32             row.GetCell(8).CellStyle = style;
33             sheet.SetColumnWidth(0, 15 * 256);
34             sheet.SetColumnWidth(1, 15 * 256);
35             sheet.SetColumnWidth(2, 15 * 256);
36             sheet.SetColumnWidth(3, 15 * 256);
37             sheet.SetColumnWidth(4, 15 * 256);
38             sheet.SetColumnWidth(5, 15 * 256);
39             sheet.SetColumnWidth(6, 15 * 256);
40             sheet.SetColumnWidth(7, 15 * 256);
41             sheet.SetColumnWidth(8, 15 * 256);
42
43
44             int index = 1;
45             foreach (DataTransferStatus dataInfo in dataList)
46             {
47                 // 第二列
48                 NPOI.SS.UserModel.IRow row2 = sheet.CreateRow(index);
49
50                 row2.CreateCell(0).SetCellValue(dataInfo.name);
51                 row2.CreateCell(1).SetCellValue(dataInfo.report_number);
52                 row2.CreateCell(2).SetCellValue(dataInfo.phyll_report_rate + "%");
53                 row2.CreateCell(3).SetCellValue(dataInfo.pressure_report_rate + "%");
54                 row2.CreateCell(4).SetCellValue(dataInfo.speed_report_rate + "%");
55                 row2.CreateCell(5).SetCellValue(dataInfo.temp_report_rate + "%");
56                 row2.CreateCell(6).SetCellValue(dataInfo.water_report_rate + "%");
57                 row2.CreateCell(7).SetCellValue(dataInfo.wave_report_rate + "%");
58                 row2.CreateCell(8).SetCellValue(dataInfo.salt_report_rate + "%");
59
60                 index++;
61             }
62             System.IO.MemoryStream stream = new System.IO.MemoryStream();
63             book.Write(stream);
64             stream.Seek(0, SeekOrigin.Begin);
65             book = null;
66             HttpResponseMessage mResult = new HttpResponseMessage(System.Net.HttpStatusCode.OK);
67             mResult.Content = new StreamContent(stream);
68             mResult.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
69             mResult.Content.Headers.ContentDisposition.FileName = type + "_到报率统计" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls";
70             mResult.Content.Headers.ContentType = new MediaTypeHeaderValue("application/ms-excel");
71
72             return mResult;
73         }

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;
 
时间: 2024-07-31 16:07:22

NPOI控件的使用导出excel文件和word文件的相关文章

BarcodeX条形码控件可以被导出为位图

BarcodeX条形码控件是一个功能齐全的ActiveX控件,可以生成任何类型的条码.它可以被绑定到一个数据源上,用于数据库报表中. 条形码可以被导出为位图,图元文件,剪贴板 具体功能: 用在Windows95/98/NT/2000/XP系统中的32位ActiveX控件 (OCX) 可在VisualStudio.Net, ASP, ASP.NET, VBScript等中使用 包含VB.Net示例程序 ASP页的频外发讯流条码图片 对于条码下方的可选文本行,所有WINDOWS字体均可用 前景和背景

可视化webpart基础开发——TreeView控件读取文档库中的所有文件夹和文件(递归方法读取)

可视化webpart基础开发——TreeView控件读取文档库中的所有文件夹和文件(递归方法读取) 分类: SharePoint2011-12-23 14:44 1584人阅读 评论(0) 收藏 举报 文档sharepointurl测试stringforms 可视化webpart基础开发——TreeView控件读取文档库中的所有文件夹和文件(递归方法读取) 1.在部署的sharepoint网站中新建一个名为“测试文档库”的文档库,并添加各级的子文件夹和子文件,用于测试 2.在VS2010中新建空

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

如何在CRichEditCtrl控件中直接读如RTF格式的文件 Inserting an RTF string using StreamIn -------------------------------------------------------------------------------- When inserting Rich Text Formatted text into the control there are two approaches you can take. In

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

之前有写过多篇关于使用Apose.Cell控件制作自定义模板报表和通用的导出Excel表格数据的操作,对这个控件的功能还是比较满意,而且也比较便利.忽然有一天,一个朋友说:你已经有生成基于自定义模板报表了,可是我每个单位都导出一张相同的报表的话,我岂不是要生成很多文件,而且对比查看也不方便,有没有更好的办法合并他们到一个文件里面呢?这样我看报表就方便很多了.本文主要介绍如何实现基于一个自定义报表模式,生成多个类似报表合并在一个文件中具体操作. 查询Apose.Cell控件的使用介绍,WorkBo

DevExpress ASPxHtmlEditor控件格式化并导出Word

在前台页面中先插入一个ASPxHtmlEditor控件,名为ASPxHtmlEditor1. 我用的Dev版本为14.1 格式化文本 在后台插入如下代码 1     const string css = "style='text-align:justify;"         //两端对齐  2         + "text-justify:inter-ideograph;"  3         + "text-indent: 2em;"  

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

使用NPOI进行导出Excel表格大家基本都会,我在网上却很少找到导出Excel表格并提示下载的 简单的代码如下 1 //mvc项目可以传多个id以逗号相隔的字符串 2 public ActionResult execl(string ids) 3 { 4 List<PayLog> list = new List<PayLog>(); 5 string[] idsstring = ids.Split(new char[] { ',' }, StringSplitOptions.Re

Webform之FileUpload(上传按钮控件)简单介绍及下载、上传文件时图片预览

1.FileUpload上传控件:(原文:http://www.cnblogs.com/hide0511/archive/2006/09/24/513201.html) FileUpload 控件显示一个文本框控件和一个浏览按钮,使用户可以选择客户端上的文件并将它上载到 Web 服务器.用户通过在控件的文本框中输入本地计算机上文件的完整路径(例如,C:\MyFiles\TestFile.txt)来指定要上载的文件.用户也可以通过单击“浏览”按钮,然后在“选择文件”对话框中定位文件来选择文件.  

VC++图片控件(Picture Control)显示资源位图(BMP)、文件位图(BMP)、其它格式文件图片(JPG\PNG\BMP)的方法

在VC++ MFC编程中,我们常使用Picture Control图片控件来显示图像.下面简单归纳几种显示不同的方式: (实例可在我的CSDN资源中下载:http://download.csdn.net/detail/margin1988/8341525) 第一种.资源位图方式显示BMP图片 如果要显示的是一张BMP位图,则可以采用资源位图方式,具体步骤如下: (1)将BMP文件拷贝到工程的res目录下: (2)在对话框中添加一个Picture Control控件,例如:ID为IDC_STATI

BOLB转word文件,和word文件转换BOLB

1.BOLB转word文件 import java.io.*; import java.sql.*; public class Test { Connection con = null; Statement stmt = null; ResultSet rs = null; private ResultSet getResultSet() { try { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); St