Use Aspose. Cells to export Excel (C #) using a template #),
Preface
With the popularity of the Internet, web projects gradually become the mainstream. I believe that most developers write the upload and export functions of Excel and Word in their development projects. This article only discusses exporting Excel. C # many third-party components support exporting Excel files, such as npoi?aspose.cellsand spire.xls. Here we use Aspose. Cells and the templates we have written.
I. Prepare a database
I created a very simple table and added some data. Structure:
Ii. Create an Excel template
Create an excel file, the first row title, and the second row add content. The second row is in the format of & = [data source table name]. Column name. The "data source table name" is the name of the DataTable returned by the background, and the "column name" is the name of the corresponding title column in the database. The details are as follows:
Iii. How to export an Excel file in the background
The Code is as follows:
Then, write a method in the Controller and call ExportExcel. This method is called by frontend js.
4. Call in view
Use js to call methods in the Controller. Be sure not to use ajax. Only window. location. href can be used.
V. Implementation results
Click "Export" on the page to bring up the file storage dialog box. The effect is as follows:
Open Excel and check the content:
The data in the database table is filled in Excel. Achieve the desired effect.
Vi. Conclusion
This sharing ends now. If this article is helpful to you, please comment or recommend it!
Project Source: https://github.com/liuyang-1990/ExportDemo
Author: NaYoung
Source: http://www.cnblogs.com/liuyoung/p/7736811.html
You are welcome to repost it, but you must keep the complete article for any repost. The original Article and the original article link are displayed in the prominent area. If you have any questions or authorization negotiation, please leave a message for me