NPOI controls use to export excel files and word files, npoiexcel
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 + "to rate statistics"); 7 8 // The first column 9 NPOI. SS. userModel. IRow row = sheet. createRow (0); 10 row. createCell (0 ). setCellValue ("site name"); 11 row. createCell (1 ). setCellValue (""); 12 row. createCell (2 ). setCellValue ("chlorophyll to report rate"); 13 row. createCell (3 ). setCellValue ("Pressure to report rate"); 14 row. createCell (4 ). setCellValue ("wind speed to report rate"); 15 row. createCell (5 ). setCellValue ("temperature to report rate"); 16 row. createCell (6 ). setCellValue ("temperature to report rate"); 17 row. createCell (7 ). setCellValue ("wave height to report rate"); 18 row. createCell (8 ). setCellValue ("salinity to reporting rate"); 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 // The second column 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(72.16.setcellvalue(datainfo.wav e_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 + "_ to report rate statistics" + DateTime. now. toString ("yyyy-MM-dd") + ". xls "; 70 mResult. content. headers. contentType = new MediaTypeHeaderValue ("application/ms-excel"); 71 72 return mResult; 73}
1. First import NPOI dll, which has many self-downloaded resources on the Internet. Go to the official website: http://npoi.codeplex.com/download dll (you can select. net2.0 or. net4.0 dll) and add references to the website.
2. Reference The namespace using NPOI. SS. UserModel
3. (here we will use exporting excel as an example) We need to understand which part of a complete excel file is made up!
(1) A Workbook, a worksheet sheet, and a ROW, Column, and Cell
4.
Create each part separately
NPOI. HSSF. userModel. HSSFWorkbook book = new NPOI. HSSF. userModel. HSSFWorkbook (); // NPOI. SS. userModel. ISheet sheet = book. createSheet ("" + type + "to rate statistics"); NPOI. SS. userModel. IRow row = sheet. createRow (0 );
Set the style of an excel file
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;