Use Aspose.cells to export the queried data to excel in ASP. NET MVC and download it in the browser.

Source: Internet
Author: User

The nagging before the chase

I was just out of work soon cabbage A, technology is very general, will always encounter some very simple problems but do not know how to do, these problems may have been resolved before. Find this problem, think about upgrading their skills, some of the new ' good ' things to record, one is to deepen the impression, the second is to be used as a reference; third, I hope Bo friends can put forward deficiencies and can optimize the place, together to discuss.

This is my trip to a company not long, let me do the small function, mainly export Excel and download it down in the browser. But there will be different nuances of demand.

First blog, there is no clear description of the place also please forgive me, I hope you have a lot of guidance.

Go to the Chase

Simple description of requirements

Will query out some columns of data that have been converted to list<t> xxx, import into Excel, and download them in the browser. (Replace with student here)

Start implementing

Shown in the GCA of the 1.student table

2. Dll:Aspose.Cells.dll (5.3.1.0) used.

3. Partial implementation code:

Style and table header

Workbook Workbook =NewWorkbook (); Worksheet Worksheet= Workbook. worksheets[0]; Cells Cells=worksheet.            Cells; Cells. InsertRow (0); Aspose.Cells.Style Style= Workbook. Styles[workbook. Styles.add ()];//New StyleStyle. HorizontalAlignment = Textalignmenttype.center;//Center TextStyle. Font.Size = One;//Text SizeStyle. Font.isbold =true;//Bold BodyCells. Setrowheight (0, -);//Set Row height

list<string> Listhead =Newlist<string>(); Listhead.add ("Student Number"); Listhead.add ("Student Name"); Listhead.add ("Student Age"); Listhead.add ("Telephone"); for(inti =0; i < Listhead.count; i++) {cells[0, I]. Putvalue (Listhead[i]); cells[0, I]. SetStyle (style); Cells. Setcolumnwidth (i, -); }

Write Content section

for (int i = 1; I <= List1. Count; i++)           {                cells[i, 0]. Putvalue (List1[i-1]. ID);                Cells[i, 1]. Putvalue (List1[i-1]. Name);                Cells[i, 2]. Putvalue (List1[i-1]. Age);                Cells[i, 3]. Putvalue (List1[i-1]. Tel);            }

It started with this cumbersome way to write content, but it would traverse a lot of times, and if the data volume was very slow, the one that looked at the document, found an easy way

Write content Partial optimizations

Worksheet. Cells.importcustomobjects (list, new string[] {"Id", "Name", "Age", "Tel"}, False, 1, 0, list. Count, True, "", false);

Follow up to see each parameter

public int importcustomobjects (ICollection list, string[] propertynames, bool ispropertynameshown, int firstrow, int firs Tcolumn, int rowNumber, bool insertrows, string dateformatstring, bool Convertstringtonumber)

See the parameter names should be clearer. is the list that needs to be imported, the column to write, whether to display the name of the property, to write to the beginning of the Excel line, to start the column ...

This is more concise, more efficient and much faster than before.

Save Excel

Workbook. Save (System.Web.HttpContext.Current.Response, "Student information. xls", Contentdisposition.attachment, New Xlssaveoptions ( SAVEFORMAT.XLSX));

Also explain not clear here, probably means to write the Excel information, respose to the foreground.

Call from the foreground

<script>        $ (function () {            $ (". Export_excel"). Click (function () {                downexcel ('/aspose/exportexcel ') ;            });        });        function Downexcel (iframeurl) {            if (!iframeurl) {                return;            }            var BODY = document.getElementsByTagName ("body") [0];            var downloadiframe = document.createelement ("iframe");            Downloadiframe.setattribute ("src", iframeurl);            Downloadiframe.setattribute ("Style", "display:none;")            Body.appendchild (Downloadiframe);        }    </script>

Here is open with Ifram. You can also use Windows.open (' URL ') directly to implement

$ (function () {            $ (". Export_excel"). Click (function () {                window.open ('/aspose/exportexcel ');            });        });

Last Excel Effect 

Conclusion

The effect is basically meet the demand, the back demand will change, this way can not fully meet the demand, because this time is too late, next time to write, thank you, please give more advice, everyone goodnight!

  

Use Aspose.cells to export the queried data to excel in ASP. NET MVC and download it in the browser.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.