Home > front end >  Using NPOI data export
Using NPOI data export

Time:12-01

1, export data
Case 1: no template
Front desk: in a new window to open the download path, use the browser's file download function realization of export effect
Background: (1) query out the need to export the data
(2) create workbook, work table
1. Reference NPOI library file
2. Using NPOI create workbook (Excel spreadsheet)
3. Create worksheet through workbook, at the same time, set with a table name
(3) to create a header row
1. Create the first line through the worksheet (title), at the same time set the line height
2. Create a cell through line
3. Set the value of the cell (text, namely the title)
4. Create cell style through workbook (horizontal/vertical center)
5. Create a cell through workbook font (color, bold, size)
6. Set the font to the cell style
7. According to the merge of data cell, constitute a header row (title line has only one line, and it is a cell)
8. Set the cell style (i.e., set the header row style)
(4) to create a header row
1. Create the second line through the worksheet (header row), at the same time set the line height
2. Create cell style through workbook (horizontal/vertical center, borders, background color)
3. Create a cell through workbook font (bold, size)
4. Set the font to the cell style
5. Create a cell through line, set the value of cell
6. Traverse line header cell in a set style for the cell
(5) the content of the filling form
1. Create cell style through workbook (horizontal/vertical center, border)
2. Traverse the query to the data, note: step 3/4/5 operation is in the loop
3. Create lines through the worksheet, due to the existing header row with a header, so when it create the
Note row index should be: 2 + current traverse index)
4. Create a cell through line, at the same time, set the value of the cell (current traversal of the corresponding data)
5. Through the current line in the cell, set the style for the cell
6. Set the column width to adaptive
7) write to create good workbook inflow and returned to the browser
1. Set the download file filename
2. Create a memory stream
3. The memory stream to create good Excel spreadsheet write to
4. Output before calling the Seek to ensure that the output from the beginning of the file location, avoid file damaged
Is
5. Return the file
Situation 2: a template
Front desk: in a new window to open the download path, use the browser's file download function realization of export effect
Background: (1) the query need to export the data
(2) to read template files
1. The file path
2. To determine whether a file exists
(3) using NPOI open template (Excel file)
1. Reference NPOI library file
2. Through open the template file flow
3. Using NPOI file conversion into workbook
(4) open the template in the first worksheet, whether to need to set the title (according to actual condition decision)
(5) to populate the data in the template
1. Create cell style through workbook (vertical/horizontal center, border)
2. Traverse the query to the data, note: step 3/4/5 operation is in the loop
3. Create lines through the worksheet, due to the existing header row with a header, so when it create the
Note row index should be: 2 + current traverse index)
4. Create a cell through line, at the same time, set the value of the cell (current traversal of the corresponding data)
5. Through the current line in the cell, set the style for the cell
6. Take the good template in the form of flow back to the browser
1. Set the download file filename
2. Create a memory stream
3. Write good filling data template inflow
4. Output before calling the Seek to ensure that the output from the beginning of the file location, avoid file damaged
Is
5. Return the file
  • Related