当前位置:网站首页>Export of data
Export of data
2022-04-23 06:24:00 【Wood acridine】
Data export
Define the export method in the page
Get query criteria , Pass the query conditions to the controller , Query the data to be exported according to the criteria
Serialize the search criteria section
Derived data
window . open(url+ Serialize some of the search criteria );
You can export the data to be exported according to the filter criteria
First query out the data to be exported
Define a method in the controller first , Method to receive some parameters to query
Associated table query data , Then filter the conditions
Write the data to be exported to Excel in
structure Excel
- Create Workbook
IWorkbook workbook = new HSSFWorkbook();
- Create sheet
ISheet sheet = workbook.CreateSheet(" Worksheet name ");
- Title Line
- Create a title line CreateRow( The index value of the row )
IRow rowTitle = sheet.CreateRow(0);
- Create cells
ICell cell0 = rowTitle.CreateCell(0);
- Set cell contents
cell0.SetCellValue(" Content ");
- Set the header row to display across columns ( Coordinates )
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(, , ,));
example :(0, 0, 0, 9) Says from the first 1 Xing di 1 The columns begin to merge into the 1 Xing di 10 End of column
- Set the style ( It can be set but not set )
After setting the style, you need to assign the style to the cell
Cell name . CellStyle= Style name
example :cell0.CellStyle = cellStyle_Title;
- Header row
- Create header row
NPOI.SS.UserModel.IRow rowHeader = sheet.CreateRow( The index value of the row );
Set fields
CreateCell Create cells SetCellValue Set the value
example :rowHeader.CreateCell(0).SetCellValue(" Serial number ");
rowHeader.CreateCell(1).SetCellValue(" Student number ");
rowHeader.CreateCell(2).SetCellValue(" full name ");
- Set header row style
Set the style for the header row cells Loop to each cell
example :
- data row
- style
- Traversal data Put the queried data one by one into Excel In the table
example :for (int i = 0; i < listStudent.Count; i++){
explain :i < listStudent.Count You can traverse as many data as you query
Create a row every time you traverse
IRow row = sheet.CreateRow(2 + i); because The title and header already occupy two lines , therefore 2+i
row.HeightInPoints = 22; HeightInPoints Set row height
Create column And set the values
example :row.CreateCell(0).SetCellValue(i + 1);// Serial number
row.CreateCell(1).SetCellValue(listStudent[i].StudentNumber)
row.CreateCell(2).SetCellValue(listStudent[i].StudentName);
explain :listStudent[i] The first i From the data StudentNumber
Add a style to each cell
}
- Set the column width to auto fit

Create a good Excel Output to browser
1、 Define the name of the output file
string fileName = " Candidates information " + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff") + ".xls";
- hold Excel Convert to stream output
- Define the flow
MemoryStream BookStream = new MemoryStream();
- Write workbook to stream
workbook.Write(BookStream);
- The output is called before Seek( Offset , Cursor position ) Sets the position in the current stream to the specified value
BookStream.Seek(0, SeekOrigin.Begin);
- Finally, the data is returned
example :return File(BookStream, "application/vnd.ms-excel", fileName);
explain : For returning files File( file , file type , File name )
Finally, call the exported method for the Export button
Click the Export button to generate a Excel surface

Click on Excel The table can be opened to see the exported data

Style settings
Create cell styles , Style a cell first , Recycle to each cell
You need to declare a style first
example :ICellStyle cellStyle_header = workbook.CreateCellStyle();
Horizontal center
example :cellStyle_header.Alignment = HorizontalAlignment.CENTER;
Vertical center
example :cellStyle_header.VerticalAlignment = VerticalAlignment.CENTER;
Set the background fill effect
example :cellStyle_header.FillPattern = FillPatternType.SOLID_FOREGROUND;
Set the background fill color
example :cellStyle_header.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.AQUA.index;
Set border lines
example :cellStyle_header.BorderLeft = CellBorderType.THIN; The left margin
cellStyle_header.BorderTop = CellBorderType.THIN; On the border
cellStyle_header.BorderRight = CellBorderType.THIN; Right margin
cellStyle_header.BorderBottom = CellBorderType.THIN; Under the frame
Set the font
First declare the font
IFont font_header = workbook.CreateFont();
font size
example :font_header.FontHeightInPoints = 10;
In bold
example :font_Title.Boldweight = (short)FontBoldWeight.BOLD;
Add font color
example :font_title.Color = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
版权声明
本文为[Wood acridine]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210616405432.html
边栏推荐
- Understanding and installing MySQL
- Pyqy5 learning (2): qmainwindow + QWidget + qlabel
- St table template
- SQL injection
- 去噪论文——[Noise2Void,CVPR19]Noise2Void-Learning Denoising from Single Noisy Images
- Framework analysis 1 Introduction to system architecture
- Code neat way to learn
- 线代第四章-向量组的线性相关
- [leetcode 202] happy number
- Programming training
猜你喜欢

Techniques et principes de détection

Generate excel template (drop-down selection, multi-level linkage)

自动控制原理知识点整合归纳(韩敏版)

A general U-shaped transformer for image restoration

RPC must know and know

Chapter 4 of line generation - linear correlation of vector systems

Pytorch learning record (V): back propagation + gradient based optimizer (SGD, adagrad, rmsporp, Adam)

線性代數第一章-行列式

IO multiplexing of 09 redis

MySQL advanced query
随机推荐
Pytorch notes - get familiar with the network construction method by building RESNET (complete code)
Collection and map thread safety problem solving
Substring Inversion (Easy Version)
Programming record - picture rotation function SciPy ndimage. Simple use and effect observation of rotate()
Pyqy5 learning (2): qmainwindow + QWidget + qlabel
List segmentation best practices
Pyqt5 learning (I): Layout Management + signal and slot association + menu bar and toolbar + packaging resource package
H. Are You Safe? Convex hull naked problem
How does MySQL convert stored seconds into dates
线性代数第二章-矩阵及其运算
Formation à la programmation
Protected (members modified by protected are visible to this package and its subclasses)
Calculation (enter the calculation formula to get the result)
Installation and usage skills of idea
[leetcode 202] happy number
MySQL table constraints and table design
Best practices for MySQL storage time
container
PyTorch笔记——实现线性回归完整代码&手动或自动计算梯度代码对比
[leetcode 401] binary Watch