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

时间:2021-01-11 17:04:54
 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;