How to export an Excel report in C #

Source: Internet
Author: User

In the previous article, I mentioned how to execute an Excel macro template in C #. This article describes how to export a template and generate a report.

The simple sample code in winform is as follows:

Public exporttextreport ()

{

String strtempreportpath = "xxxxreport.xls"; // report export path
String strtemplatepath = "xxxxtemplate.xls"; // Report Template path

Fileinfo Fi = new fileinfo (strtempreportpath );

Exceltest. exeltemplate. fillcontent (strtemplatepath, strtempreportpath, dsdata );

If (file. exists (strtempreportpath ))
{
System. Diagnostics. process. Start ("excel.exe", strtempreportpath); // open Excel
}

}

In Asp.net, we can create a report export page to export the report. The Code is as follows:

<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "attachprint. aspx. cs" inherits = "common. attachprint" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> untitled page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>

</Div>
</Form>
</Body>
</Html>

Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. IO;

Namespace common
{
Public partial class attachprint: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! String. isnullorempty (request. querystring ["printfile"])
{
String realfullfilename = server. htmldecode (request. querystring ["printfile"]);
Fileinfo finfo = new fileinfo (realfullfilename );
Response. Clear ();
Response. charset = "UTF-8 ";
Response. Buffer = true;
This. enableviewstate = false;
Response. contentencoding = system. Text. encoding. utf8;

Response. appendheader ("content-disposition", "attachment; filename ="
+ Httputility. urlencode (finfo. Name, system. Text. encoding. utf8 ));
Response. writefile (realfullfilename );
Response. Flush ();
Response. Close ();
Response. End ();
}
}
}
}

 

The following is an example of how to export a report on other pages:

Private exporttest ()

{

String strtempreportpath = "xxxxreport.xls"; // report export path
String strtemplatepath = "xxxxtemplate.xls"; // Report Template path

Fileinfo Fi = new fileinfo (strtempreportpath );

Exceltest. exeltemplate. fillcontent (strtemplatepath, strtempreportpath, dsdata );

String strexportpath = server. htmlencode (strtempreportpath );
Response. Redirect (string. Format ("attachprint. aspx? Printfile = {0} ", strexportpath), true );

 

}

 

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.