. NET build Excel, and download

Source: Internet
Author: User
Tags urlencode

There are a number of ways to generate Excel, and here are two easiest to record:

1. Save the data as HTML, then output to the client, save as Excel file;

2. Generate a string from \t\n, and then output it to the client, saved as Excel.

Both of these principles are consistent, but the resulting string looks inconsistent. I use MVC here, so to download, directly generate a hyperlink to the current generated file action, with Ajax seems to have no way, but if the current hyperlink just request a file, the page will not refresh, For example a button click event: Direct windows.location.href= "/control/action", so you can download directly, the current page will not be refreshed.

First on the common code:

Httpresponsebase resp=Httpcontext.response;resp. ContentEncoding= System.Text.Encoding.GetEncoding ("GB2312");//set the encoding format to prevent Chinese garbled charactersResp. Appendheader ("content-disposition","attachment;filename="+ Httputility.urlencode ("Monthly report. xls", Encoding.UTF8));//troubleshoot generated file names in Chinese charactersResp. ContentType ="Application/ms-excel";

Then there is the process of generating the data, which is divided into two types:

1. Generating the HTML for a table

Here you need to get the data of the database, and then create the table in the format you want, you can set the table style, all the styles that are set on table will affect the generated Excel, you can also give table header, of course, if you want to add those

<TableBorder= ' 1 '>//Set border=1 the resulting table will have a black table box<TR>        <TD>1</TD><TD>2</TD><TD>3</TD><TD>4</TD>    </TR></Table>

2. Control Excel styles with \t\n

Here, each add a \ t will generate a box in Excel, each plus a \ n will be wrapped, this is the principle, so you can get the data, the data list loop, each data item is a row

Each column of data corresponds to a grid inserted into Excel, as shown in the following example:

foreach(varIteminchlist) {Ls_item+ = Item.agentname +"\ t"; Ls_item+ = Item.tourno +"\ t"; Ls_item+ = Item.travelername +"\ t"; Ls_item+ = Item.checkindate +"\ t"; Ls_item+ = Item.checkoutdate +"\ t"; Ls_item+ = Item.rooms +"\ t"; Ls_item+ = Item.nights +"\ t"; Ls_item+ = Item. Roomprice +"\ t"; Ls_item+ = Item.minortotal +"\t\n";//finally add a \ n to change the line}

The above code is the part of the data that is populated, and the next step is to output the data to the client, as long as two words:

Resp. Write (the string of content); resp. End ();

The above is the method of generating Excel, of course, here is the direct output to the client, so there is no extra files generated in the server, if not to generate files on the server, and then provide links to the client to download, in fact, the data with the file stream output, save as a file, and then return the generated file path to the client, Then let the client to request this file path, you can download, the example is as follows:

   New false, encoding.getencoding ("gb2312"));   Sw. Write (str); // Save File    SW. Close ();    return Content (".. /download/file. xls");//Do not forget the suffix. xls

The word file is generated in the same way, but there are some small places that need to be modified:

Resp. Appendheader ("content-disposition","attachment;filename=" +httputility.urlencode ("file.doc", Encoding.UTF8)); // suffix to Doc " Application/msword "; // file content is MSWord

Everything else is the same.

Of course, there are many ways to generate these files, and this should be the simplest one.

. NET build Excel, and download

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.