using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI.HSSF.UserModel;
using System.IO;
using System.Windows.Forms;
using Model;
using NPOI.XSSF.UserModel;//调用NPOI引用
DAL.Helper namespace
{
public class PrintHelper
{
public void PrintMsg (StudentModel objStudentModel)
{
// Open Excle pre-written template, the current permission is read-only
FileStream theExcel = new FileStream (@ " E: \ rcmeng \ source \ Test Program \ AppOfStudents \ AppOfStudents \ bin \ Debug \ StudentInfo.xlsx ", FileMode.Open, FileAccess.Read);
// call Excle template, and create a new workbook, if your computer Excle version is 2007 or less, compared with HSSFWorkbook, if it is 2007 or more , compared XSSFWorkbook
XSSFWorkbook theStudentInfo = new new XSSFWorkbook (theExcel);
// create or acquire a worksheet, as has been previously written a good template, you can directly call the template worksheet, worksheet name being called here "info"
XSSFSheet theStudentInfoSheet = (XSSFSheet) theStudentInfo.GetSheet ( "Info");
// NPOI operation, the encapsulated data (objStudentModel) are successively filled into the Excle
XSSFCell cellId = (XSSFCell) theStudentInfoSheet.GetRow (. 3) .GetCell (. 3);
cellId.SetCellValue (objStudentModel.StudentId);
XSSFCell cellName = ( XSSFCell) theStudentInfoSheet.GetRow (. 3) .GetCell (. 5);
cellName.SetCellValue (objStudentModel.StudentName);
XSSFCell cellGender = (XSSFCell) theStudentInfoSheet.GetRow (. 3) .GetCell (. 7);
cellGender.SetCellValue (objStudentModel.Gender);
cellClassName = XSSFCell (XSSFCell) theStudentInfoSheet.GetRow (. 5) .GetCell (. 3);
cellClassName.SetCellValue (objStudentModel.ClassName);
XSSFCell cellPhoneNumber = (XSSFCell)theStudentInfoSheet.GetRow(5).GetCell(5);
cellPhoneNumber.SetCellValue(objStudentModel.PhoneNumber);
XSSFCell cellStudentAddress = (XSSFCell)theStudentInfoSheet.GetRow(7).GetCell(3);
cellStudentAddress.SetCellValue(objStudentModel.StudentAddress);
Save the file //
// prompt the user to save the file location (IO extension is referenced file)
SaveFileDialog saveFile = new new SaveFileDialog ();
// new default file name extension, Excle as .xlsx files or .xls
saveFile.DefaultExt = ".xlsx";
// save as type settings (lazy, so just write a set item)
saveFile.Filter = "Excel files | * .xlsx";
// new file name default setting is "student information"
String fileName = "student information";
// will have set a good default name assigned
saveFile.FileName = fileName;
// pop-up dialog box
saveFile.ShowDialog ();
// filename can be modified and saved
fileName = saveFile.FileName;
/ / click cancel
if (fileName.IndexOf ( ":") <0)
{
return;
}
// Click OK
IF (fileName = ""!)
{
// create a new file stream, create a new file
the FileStream = new new newFile the FileStream (saveFile.FileName, FileMode.Create);
theStudentInfo.Write (newFile);
// Close file stream
newFile.Close ();
// the GC garbage collection
the GC.SuppressFinalize (the this);
}
}
}
}