Export data to Excel

Development tools and key technologies: Visual Studio and NPOI

Author: Huang Can

Write Time: 2019.5.12

To export employee information materials, for example

Data and more time, do not want all of the data export Excel, I want to export data that meets the conditions I need to export Excel, this time we want to filter data data again and then screened to export Excel. Conditional screening data must be written at the time of the screening method to query the previous query data. Jacascript to export data to Excel function to get the current data filtering criteria and data query method, like the previous screening, filtering criteria is based on the department ID and jobs as well as employee ID details (details of staff is the employee number and employee name) screening, judge screening data sector ID job ID is empty or undefined, undefined, or if it is empty, then the department ID and job ID assigned zero, number and name is undefined whether, if it is the number and names assigned to undefined zero . Ask whether you want to export the data in the current form, is based on the department ID and job ID number or name or access to the controller, turn off inquiry after inquiry box

Here Insert Picture Description

Methods controller

Receiving javascript parameter passing over the department ID and jobs as well as employee ID details

Excel spreadsheet to export data to an Excel spreadsheet, you need to re-query over data, queries to the data on the export of which export to Excel query methods and data returned by the query to the page is displayed to see the query method exactly the same screening the same is true on the condition and also to write data filtering and query returns a page to show up looking exactly the same query method of screening data

Here Insert Picture Description
Here Insert Picture Description

After the re-screening data query again, it is to create an Excel workbook, create a worksheet, create a header row, create workbooks, worksheets also need to reference the header row NPOI.dll
Here Insert Picture Description

Create a working blog use NPOI in HSSFWoekbook, and then create a worksheet using NPOI in ISheet and to the worksheet name a name, and then create the header using NPOI in IRow row index (0) represents the first worksheet row

Here Insert Picture Description

Setting the header, the need to export the attribute data set to the first row in the table, property 8 8, the index starting from 0 to 7, provided the value of the attribute header row

Here Insert Picture Description

Adding data to Excel, the exported data is not only one data can not be set as the header row as adding data needed for loop that add data, the data can not add more than check out the data, you need to create a row in a for loop , carrying data is added, the index line is added (i + 1), each row of data is added in the original data row + 1, the first row of data is needed to add data row + 1, because the header has been created row, the first row of the index table in the first row is 0, the first row of data needs to +1 on the index header row, the first row is the second row of data. Adding attribute data need to follow up the column to add data, the index is zero-based, the worksheet is not added in the first column, there is no need +1, and these data are added to them a fixed.

Here Insert Picture Description

After the data is saved to add data, save the worksheet workbook, the workbook name next to a name, in order to prevent the export of data each time the workbook name are the same, so the name of the workbook format is: current workbook name + export time + Excel suffix. Examples of the MemoryStream stream of a memory, the memory writes the workbook flow stream into the memory

Here Insert Picture Description

Seek call before the output (offset, the cursor position) to read the file pointer to a specified location, Seek (0, Seek.begin) indicates the relative position of the first parameter, the second parameter represents the reference position, the read write memory stream workbook

Returns a file, the file is written to the memory stream workbook, "application / vnd.ms-excel" is a MIME file type, file type MIME (Multipurpose Internet Mail
Extensions) Multipurpose Internet Mail Extensions type, the workbook name returned with
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_44542088/article/details/90241179