Using the Codesmith code generator to generate an XML report file

Source: Internet
Author: User
Tags count end generator string version tostring xmlns client
xml| Generate XML


Some time ago when you do a Web project, you receive a task and the report exports Excel. You cannot use a Third-party reporting tool, which can only be resolved by manipulating the Excel library.



The first is to call COM write Excel on the server side to the client; This is not feasible because of the number of users in the system, C # operations Excel (COM) is inefficient and consumes a lot of resources.



The second: it is through JScript to manipulate ActiveX, the principle is the same as the first, only the operation of data operations on the client.



Both of these methods are cumbersome to operate and maintain, and are not highly efficient.



The time is worrying suddenly think that Excel can open XML in the end? Try the discovery is OK, and then draw some tables in Excel to save as XML; I opened it and smiled. I can generate XML dynamically. Generate XML dynamically? The combination of those tags is annoying enough, that there will be direct operation COM Easy direct? Here's a tool. Codesmith, it is a template-based code generation, the template syntax and ASP, if you have written an ASP to know his principles. This tool here is not much introduction, interested friends can go to understand. (There are, of course, other code generation tools, the same principle)



Note: Excel must be an XP or a later version



Hfsoft.exports are all my own encapsulated classes



Let's take a look at the model Northwind data Customer Information template


==========================================
<%--
Name:
Author:
Description:
%>
<%@ codetemplate language= "C #" targetlanguage= "Text" src= "inherits=" "debug=" False "description=" Template Description here. "%>
<%@ Assembly name= "System.Data"%>
<%@ Import namespace= "System.Data"%>
<%@ Assembly Name = "Hfsoft.exports"%>
<%@ Import namespace= "Hfsoft.exports"%>
<%@ Property Name= "DataSource" type= "System.Data.DataSet" optional= "False" category= "Strings" description= "the" is A sample string property. "%>
<%
OnInit ();
%>
<?xml version= "1.0" encoding= "GB2312"?>
<workbook xmlns= "Urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o= "Urn:schemas-microsoft-com:office:office"
xmlns:x= "Urn:schemas-microsoft-com:office:excel"
Xmlns:ss= "Urn:schemas-microsoft-com:office:spreadsheet"
Xmlns:html= "HTTP://WWW.W3.ORG/TR/REC-HTML40">
<documentproperties xmlns= "Urn:schemas-microsoft-com:office:office" >
<Author>HFSoft-Henry</Author>
<LastAuthor>HFSoft-Henry</LastAuthor>
<Created>2005-01-16T10:24:48Z</Created>
<Company>HFSoft</Company>
<Version>10.2625</Version>
</DocumentProperties>
<officedocumentsettings xmlns= "Urn:schemas-microsoft-com:office:office" >
<DownloadComponents/>
<locationofcomponents href= "file:///h:\office\officexp\officexp\ "/>
</OfficeDocumentSettings>
<excelworkbook xmlns= "Urn:schemas-microsoft-com:office:excel" >
<WindowHeight>9450</WindowHeight>
<WindowWidth>12780</WindowWidth>
<WindowTopX>480</WindowTopX>
<WindowTopY>15</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<style ss:id= "Default" ss:name= "Normal" >
<alignment ss:vertical= "Center"/>
<Borders/>
<font ss:fontname= "Song Body" x:charset= "134" ss:size= "/>"
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<style ss:id= "m15730010" >
<alignment ss:horizontal= "center" ss:vertical= "center"/>
<Borders>
<border ss:position= "Bottom" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "left" ss:linestyle= "continuous" ss:weight= "2"/>
<border ss:position= "Right" ss:linestyle= "continuous" ss:weight= "2"/>
<border ss:position= "Top" ss:linestyle= "continuous" ss:weight= "2"/>
</Borders>
<font ss:fontname= "Song Body" x:charset= "134" ss:size= "ss:color=" "#FFFFFF"
ss:bold= "1"/>
<interior ss:color= "#008080" ss:pattern= "Solid"/>
</Style>
<style ss:id= "m15730020" >
<alignment ss:horizontal= "left" ss:vertical= "Center"/>
<Borders>
<border ss:position= "Bottom" ss:linestyle= "continuous" ss:weight= "2"/>
<border ss:position= "left" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "Right" ss:linestyle= "continuous" ss:weight= "1"/>
</Borders>
</Style>
<style ss:id= "m15730030" >
<alignment ss:horizontal= "left" ss:vertical= "Center"/>
<Borders>
<border ss:position= "Bottom" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "left" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "Right" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "Top" ss:linestyle= "continuous" ss:weight= "1"/>
</Borders>
</Style>
<style ss:id= "m15741120" >
<alignment ss:horizontal= "left" ss:vertical= "Center"/>
<Borders>
<border ss:position= "Bottom" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "left" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "Right" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "Top" ss:linestyle= "continuous" ss:weight= "1"/>
</Borders>
</Style>
<style ss:id= "m15741130" >
<alignment ss:horizontal= "left" ss:vertical= "Center"/>
<Borders>
<border ss:position= "Bottom" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "left" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "Right" ss:linestyle= "continuous" ss:weight= "2"/>
<border ss:position= "Top" ss:linestyle= "continuous" ss:weight= "1"/>
</Borders>
</Style>
<style ss:id= "m15741140" >
<alignment ss:horizontal= "left" ss:vertical= "Center"/>
<Borders>
<border ss:position= "Bottom" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "left" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "Right" ss:linestyle= "continuous" ss:weight= "2"/>
<border ss:position= "Top" ss:linestyle= "continuous" ss:weight= "1"/>
</Borders>
</Style>
<style ss:id= "S21" >
<font ss:fontname= "Song Body" x:charset= "134" ss:size= "" ss:color= "#FF0000"/>
</Style>
<style ss:id= "S40" >
<Borders>
<border ss:position= "Bottom" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "left" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "Right" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "Top" ss:linestyle= "continuous" ss:weight= "1"/>
</Borders>
<interior ss:color= "#C0C0C0" ss:pattern= "Solid"/>
</Style>
<style ss:id= "S45" >
<Borders>
<border ss:position= "Bottom" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "left" ss:linestyle= "continuous" ss:weight= "2"/>
<border ss:position= "Right" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "Top" ss:linestyle= "continuous" ss:weight= "1"/>
</Borders>
<interior ss:color= "#C0C0C0" ss:pattern= "Solid"/>
</Style>
<style ss:id= "S48" >
<Borders>
<border ss:position= "Bottom" ss:linestyle= "continuous" ss:weight= "2"/>
<border ss:position= "left" ss:linestyle= "continuous" ss:weight= "2"/>
<border ss:position= "Right" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "Top" ss:linestyle= "continuous" ss:weight= "1"/>
</Borders>
<interior ss:color= "#C0C0C0" ss:pattern= "Solid"/>
</Style>
<style ss:id= "S50" >
<Borders>
<border ss:position= "Bottom" ss:linestyle= "continuous" ss:weight= "2"/>
<border ss:position= "left" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "Right" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "Top" ss:linestyle= "continuous" ss:weight= "1"/>
</Borders>
<interior ss:color= "#C0C0C0" ss:pattern= "Solid"/>
</Style>
<style ss:id= "S84" >
<alignment ss:horizontal= "left" ss:vertical= "Center"/>
<Borders>
<border ss:position= "Bottom" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "left" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "Right" ss:linestyle= "continuous" ss:weight= "2"/>
<border ss:position= "Top" ss:linestyle= "continuous" ss:weight= "1"/>
</Borders>
</Style>
<style ss:id= "S85" >
<alignment ss:horizontal= "left" ss:vertical= "Center"/>
<Borders>
<border ss:position= "Bottom" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "left" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "Right" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "Top" ss:linestyle= "continuous" ss:weight= "1"/>
</Borders>
</Style>
<style ss:id= "S88" >
<alignment ss:horizontal= "left" ss:vertical= "Center"/>
<Borders>
<border ss:position= "Bottom" ss:linestyle= "continuous" ss:weight= "2"/>
<border ss:position= "left" ss:linestyle= "continuous" ss:weight= "1"/>
<border ss:position= "Right" ss:linestyle= "continuous" ss:weight= "2"/>
<border ss:position= "Top" ss:linestyle= "continuous" ss:weight= "1"/>
</Borders>
</Style>
</Styles>
<worksheet ss:name= "Sheet1" >
<table ss:expandedcolumncount= "5" ss:expandedrowcount= "&LT;%=DM. Records.count*8%> "x:fullcolumns=" 1 "
x:fullrows= "1" ss:defaultcolumnwidth= "WU" ss:defaultrowheight= "14.25" >
<column ss:autofitwidth= "0" ss:width= "80.25"/>
<column ss:index= "4" ss:autofitwidth= "0" ss:width= "71.25"/>
<column ss:autofitwidth= "0" ss:width= "108"/>
<%
for (int i=0;i<dm. records.count;i++)
{
%>
<row ss:autofitheight= "0" ss:height= "24.75" >
<cell ss:mergeacross= "4" ss:styleid= "m15730010" ><data ss:type= "String" > Customer details </data></cell >
</Row>
<row ss:autofitheight= "0" ss:height= "20.0625" >
<cell ss:styleid= "S45" ><data ss:type= "String" > Company name </Data></Cell>
<cell ss:mergeacross= "3" ss:styleid= "m15741130" ><data "String" ss:type=. records[i]["CompanyName"]%></data></cell>
</Row>
<row ss:autofitheight= "0" ss:height= "20.0625" >
<cell ss:styleid= "S45" ><data ss:type= "String" > Contact person </Data></Cell>
<cell ss:mergeacross= "1" ss:styleid= "m15741120" ><data "String" ss:type=. records[i]["ContactName"]%></data></cell>
<cell ss:styleid= "S40" ><data ss:type= "String" > Contact Job </Data></Cell>
<cell ss:styleid= "s84" ><data ss:type= "String" &GT;&LT;%=DM. records[i]["ContactTitle"]%></data></cell>
</Row>
<row ss:autofitheight= "0" ss:height= "20.0625" >
<cell ss:styleid= "S45" ><data ss:type= "String" > Address </Data></Cell>
<cell ss:mergeacross= "3" ss:styleid= "m15741140" ><data "String" ss:type=. records[i]["Address"]%></data></cell>
</Row>
<row ss:autofitheight= "0" ss:height= "20.0625" >
<cell ss:styleid= "S45" ><data ss:type= "String" > Zip code </Data></Cell>
<cell ss:mergeacross= "1" ss:styleid= "S85" ><data "String" ss:type=. records[i]["PostalCode"]%></data></cell>
<cell ss:styleid= "S40" ><data ss:type= "String" > City </Data></Cell>
<cell ss:styleid= "s84" ><data ss:type= "String" &GT;&LT;%=DM. records[i]["City"]%></data></cell>
</Row>
<row ss:autofitheight= "0" ss:height= "20.0625" >
<cell ss:styleid= "S45" ><data ss:type= "String" > National </Data></Cell>
<cell ss:mergeacross= "1" ss:styleid= "m15730030" ><data "String" ss:type=. records[i]["Country"]%></data></cell>
<cell ss:styleid= "S40" ><data ss:type= "String" > Area </Data></Cell>
<cell ss:styleid= "s84" ><data ss:type= "String" &GT;&LT;%=DM. records[i]["Region"]%></data></cell>
</Row>
<row ss:autofitheight= "0" ss:height= "20.0625" >
<cell ss:styleid= "S48" ><data ss:type= "String" > Phone </Data></Cell>
<cell ss:mergeacross= "1" ss:styleid= "m15730020" ><data "String" ss:type=. records[i]["Phone"]%></data></cell>
<cell ss:styleid= "S50" ><data ss:type= "String" > Fax </Data></Cell>
<cell ss:styleid= "S88" ><data ss:type= "String" &GT;&LT;%=DM. records[i]["Fax"]%></data></cell>
</Row>
<row ss:height= ">"
<cell ss:styleid= "S21"/>
</Row>
<%}%>
</Table>
<worksheetoptions xmlns= "Urn:schemas-microsoft-com:office:excel" >
<Print>
<ValidPrinterInfo/>
<PaperSizeIndex>9</PaperSizeIndex>
<HorizontalResolution>300</HorizontalResolution>
<VerticalResolution>300</VerticalResolution>
</Print>
<Selected/>
<Panes>
<Pane>
<Number>3</Number>
<ActiveRow>8</ActiveRow>
<ActiveCol>2</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
<worksheet ss:name= "Sheet2" >
<table ss:expandedcolumncount= "0" ss:expandedrowcount= "0" x:fullcolumns= "1"
x:fullrows= "1" ss:defaultcolumnwidth= "WU" ss:defaultrowheight= "14.25"/>
<worksheetoptions xmlns= "Urn:schemas-microsoft-com:office:excel" >
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
<worksheet ss:name= "Sheet3" >
<table ss:expandedcolumncount= "0" ss:expandedrowcount= "0" x:fullcolumns= "1"
x:fullrows= "1" ss:defaultcolumnwidth= "WU" ss:defaultrowheight= "14.25"/>
<worksheetoptions xmlns= "Urn:schemas-microsoft-com:office:excel" >
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>


<script runat= "Template" >
My methods is here.
Public HFSoft.Exports.DataManager DM = new HFSoft.Exports.DataManager ();
public void OnInit ()
{
Dm. DataSource = DataSource;
HFSoft.Exports.DataAnalyse analyse = new HFSoft.Exports.DataAnalyse ();
Dm. Execute (analyse);
}
</script>



============================================



The principle of <%%> tag is the same as that of ASP.



God, how can you write such a complicated XML? Let's not forget Excel, which can be used to export XML with Excel templates. The HFSoft.Exports.DataManager in the template is a data management class that I write myself for  and grouping. Template has come out, how do I invoke him to generate XML files?



Here are some of the calling codes for WebForm.



private void (string Template,system.data.dataset ds)
  {
    HFSoft.Exports.ExportAdapter export = new HFSoft.Exports.ExportAdapter ();
   export. DataSource = ds;
   string filename = Guid.NewGuid (). ToString (). Replace ("-", "") + ". xml";
   export. SaveFile =request.physicalapplicationpath +@ "reports\ " + filename;
   export. Template =request.physicalapplicationpath +@ "reports\ " + Template;
   hfsoft.exports.webexcelreportcontrol Excel = new HFSoft.Exports.WebExcelReportControl (export );
   excel. Httpfile = "http://" + request.servervariables["SERVER_NAME"]+ Request.applicationpath + "/Reports/" + filename;
   excel. Execute ();
   this. RegisterStartupScript (Guid.NewGuid (). ToString (), Excel. Reportjscript);
  }



private void Cmdcustomelist_click (object sender, System.EventArgs e)
{
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter ("SELECT * from Customers", ConnectionString);
System.Data.DataSet myds = new DataSet ();
Da. Fill (myds);
("CUSTOMERLIST.CST", myds);
}



This is the end of the example of exporting excle with XML. The principle is very simple is that data combined with XML to generate the final XML document to open to Excel, the implementation of this function is also a lot of practice.



For yourself it is certainly the most convenient way to choose your own feeling to achieve. I choose the third tool for convenience to do the most cumbersome events:




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.