NPOI generates excel sheet
1. What is NPOI ? The definition given by Baidu Encyclopedia is: NPOI refers to a program built on the POI 3.x version. NPOI can read and write Word or Excel documents without installing Office .
Simply put, NPOI is a powerful class library for reading and writing excel and word documents.
<!--[if !supportLists]--> 2. 2. The basic process of NPOI generating EXCEL table: get data > create table > write data to table
<!--[if !supportLists]--> 3. 3. Code Example
Create two classes: student class and employee class
public class Student { public String no; public String name; } public class Worker { public String age; public String job; public String name; }
The header of the two sheet pages of the table
/// <summary> /// Get the student header /// </summary> /// <returns></returns> public List<String> GetStuHeaderList() { var stuHeaderList = new List<String>(); stuHeaderList.Add("学号"); stuHeaderList.Add("姓名"); return stuHeaderList; } /// <summary> /// Get the worker header /// </summary> /// <returns></returns> public List<String> GetWorHeaderList() { var worHeaderList = new List<String>(); worHeaderList.Add("姓名"); worHeaderList.Add("年龄"); worHeaderList.Add("Work"); return worHeaderList; }
Obtaining data: The project generally obtains data in the database. Here, for simplicity, add content directly to the list
public void GetData(List<Student> stuList, List<Worker> workList) { Student stu1 = new Student(); stu1.no = "1"; stu1.name = "小A"; stuList.Add(stu1); Student stu2 = new Student(); stu2.no = "2"; stu2.name = "小B"; stuList.Add(stu2); Worker work1 = new Worker(); work1.age = "20"; work1.job = "Engineer"; work1.name = "小C"; workList.Add(work1); Worker work2 = new Worker(); work2.age = "22"; work2.job = "Technician"; work2.name = "小D"; workList.Add(work2); }
Create EXCEL file and write data
public String CreateExcelFile(String filePath, String fileName) { String result = "S"; //add data to the list List<Student> stuList = new List<Student>(); List<Worker> workList = new List<Worker>(); this.GetData(stuList, workList); if (!string.IsNullOrEmpty(filePath)) { NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook(); NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("sheet1"); //set the title style ICellStyle style1 = book.CreateCellStyle(); style1.Alignment = HorizontalAlignment.Center; IFont font = book.CreateFont(); font.Boldweight = short.MaxValue; font.FontHeight = 280; style1.SetFont(font); //write title NPOI.SS.UserModel.IRow row1 = sheet.CreateRow(0); row1.Height = 420; sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 1));//merge cells ICell cell1 = row1.CreateCell(0); cell1.CellStyle = style1; cell1.SetCellValue("Student Information Table"); //set header font ICellStyle style2 = book.CreateCellStyle(); style2.Alignment = HorizontalAlignment.Center; IFont font3 = book.CreateFont(); font3.Boldweight = short.MaxValue; style2.SetFont(font3); style2.WrapText = true; style2.VerticalAlignment = VerticalAlignment.Center; //write file header NPOI.SS.UserModel.IRow row2 = sheet.CreateRow(1); List<String> headerList = this.GetStuHeaderList(); for (int i = 0; i < headerList.Count; i++) { ICell cell5 = row2.CreateCell(i); cell5.CellStyle = style2; cell5.SetCellValue(headerList[i]); } //write data for (int i = 0; i < stuList.Count; i++) { NPOI.SS.UserModel.IRow row3 = sheet.CreateRow(i + 2); row3.CreateCell(0).SetCellValue(stuList[i].no); row3.CreateCell(1).SetCellValue(stuList[i].name); } //create employee table book = WriteWorkerFileToExcel(book, filePath + @"\" + fileName + ".xls", workList); // write to client using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { book.Write(ms); using (FileStream fs = new FileStream(filePath +@"\" + fileName + ".xls", FileMode.Create, FileAccess.Write)) { byte[] data = ms.ToArray(); fs.Write(data, 0, data.Length); fs.Flush(); } book = null; } } return result; } /// <summary> /// Generate sheet2 /// </summary> /// <param name="book"></param> /// <param name="filePath"></param> /// <param name="workList"></param> /// <returns></returns> public NPOI.HSSF.UserModel.HSSFWorkbook WriteWorkerFileToExcel(NPOI.HSSF.UserModel.HSSFWorkbook book, String filePath,List<Worker> workList) { if (!string.IsNullOrEmpty(filePath)) { NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("sheet2"); List<String> headerList = this.GetWorHeaderList(); //set the title style ICellStyle style1 = book.CreateCellStyle(); style1.Alignment = HorizontalAlignment.Center; IFont font = book.CreateFont(); font.Boldweight = short.MaxValue; font.FontHeight = 280; style1.SetFont(font); //write title NPOI.SS.UserModel.IRow row1 = sheet.CreateRow(0); row1.Height = 420; sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 2)); ICell cell1 = row1.CreateCell(0); cell1.CellStyle = style1; cell1.SetCellValue("Employee Information Table"); //set header font ICellStyle style2 = book.CreateCellStyle(); style2.Alignment = HorizontalAlignment.Center; IFont font3 = book.CreateFont(); font3.Boldweight = short.MaxValue; style2.SetFont(font3); style2.WrapText = true; style2.VerticalAlignment = VerticalAlignment.Center; //write file header NPOI.SS.UserModel.IRow row2 = sheet.CreateRow(1); for (int i = 0; i < headerList.Count; i++) { ICell cell5 = row2.CreateCell(i); cell5.CellStyle = style2; cell5.SetCellValue(headerList[i]); } //write data for (int i = 0; i < workList.Count; i++) { NPOI.SS.UserModel.IRow row3 = sheet.CreateRow(i + 2); row3.CreateCell(0).SetCellValue(workList[i].name); row3.CreateCell(1).SetCellValue(workList[i].job); row3.CreateCell(2).SetCellValue(workList[i].age); } return book; } return null; }