Page export data to Excel

Remember to apply NPOI in a reference
// Create the workbook excel
HSSFWorkbook excelBook = new new HSSFWorkbook ();
HSSFWorkbook: create a function of excel workbook
// create worksheets and named
NPOI.SS.UserModel.ISheet sheet1 = excelBook.CreateSheet ( " candidates information ");
NPOI.SS: NPOI own method keyword UserModel references: create a worksheet method Isheet: worksheet sheet1: first worksheet excelBook.CreateSheet: set the name of the worksheet.
// to Excel plus the time to ensure that the file will not be repeated
var fileName = "candidate information" + DateTime.Now.ToString ( "the mM-YYYY-mm-dd-HH-SS-FFFF") + "* .xls";
the DateTime: object represents the time a certain moment .Now: Gets a DateTime object to obtain the current date and time computer
ToString: convert the value of the specified format DateTime object as a valid string;
// write the file excelBook bookStream worksheet
excelBook.Write ( bookStream);
the write: write the file stream read
call Seek (offsets, the cursor position) before the 0 // output location to the beginning position
bookStream.Seek (0, SeekOrigin.Begin);
string filePath = Server.MapPath ( "~ / Document / Template / candidate information into the template .xls"); acquisition file server
Server: Server MapPath: Project current location
// file into a stream
if (System.IO.File.Exists ( filePath))
{
name // file acquired
String strFileName = Path.GetFileName (filePath)
// At this time because the flow path so to obtain the file name of the file
path: strin examples include a file or path operates
GetFileName: returns specify the file name and path string name to expand the
return file (new new FileStream (filePath, FileMode.Open), "the Application / OCTET-Stream", strFileName);
FileStream: path using the specified mode initialization and create a new instance of the FileStream class
FileMode: specified operating system for opening file
open: Specifies the operating system a file open an existing file, if there is no abnormality no
}
String fileExtension = Path.GetExtension (file.FileName); // get the file extension
GetExtension: returns the specified file path name expansion.
Get the full name of the file: FileName
// binary stream memory array into
the MemoryStream = new new excelFileStream the MemoryStream (fileBytes);
the MemoryStream: keyword storage region for the memory support abbreviated flow stream memory
// stream into the memory workbook
NPOI.SS.UserModel.IWorkbook workbook = HSSFWorkbook new new (excelFileStream);
Iworkbook: thin jobs
// create a worksheet and name
NPOI.SS.UserModel.ISheet sheet1 = excelBook.CreateSheet ( "candidates information");
Isheet: keyword worksheet
// create the first line
NPOI.SS.UserModel.IRow ROW1 = sheet1.CreateRow (0);
iRow: keyword line
IF ( "* .xls" .Equals (fileExtension) || ".XLS" .Equals (fileExtension))
Equals: OK examples have the same value as another string object
// Create DataTable row
the dataRow dtRow dtExcel.NewRow = ();
the dataRow: a row of data in the data table
NewRow: Create a new datarow has the same frame with the table is row
dtExcel. Rows.Add (dtRow);
Rows: obtaining a collection belonging to the table row
Add: DataRow added to the specified object is DataRowCollection row to the table
myModels.Entry (dbStudent) .State = System.Data.Entity.EntityState.Modified
to System.Data.Entity : using the same reference

Guess you like

Origin blog.csdn.net/weixin_44552168/article/details/90292533