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 );
}