分享
 
 
 

运用CodeSmith代码生引擎生成XML报表文件。

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

前段时间做WEB项目时接到一个任务,报表都导出EXCEL。

不能用第三方的报表工具,那只能操作EXCEL Library来解决了。

第一种:在服务端调用COM写EXCEL发关到客户端;

这样不太可行因为系统的用户数量也很多,C#操作Excel(COM)效率不高,还很消耗服务器资源。

第二种:就是通过JSCRIPT来操作ActiveX,原理和第一种一样;只是操作数据操作放到客户端进行。

这两种方法操作和维护都很烦琐,而且效率也不高。

正在烦恼的时间突然想到了Excel到底能不能打开XML呢?

试了一下发现是可以,然后在Excel画了一些表格另存为XML;

打开后看了一下,笑了我可以通过动态生成XML不就可以啦。

动态生成XML?就那些标记的组合就够烦,那会有直接操作COM来得方便直接?

这里就要提一个工具了CodeSmith,它是一个基于模板的代码生成,

模板的语法和ASP差不多,如果写过ASP一看就知道他的原理。

这个工具在这里就不多介绍,有兴趣的朋友可以去了解一下。(当然还有其他代码生成工具,原理一样)

注意:Excel必须是XP或更高版本

HFSoft.Exports都是我自己封装的类

先来看下模NorthWind数据客户信息模板吧

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

<%--

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="This 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:officeOfficeXPOfficeXP"/>

</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="宋体" x:CharSet="134" ss:Size="12"/>

<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="宋体" x:CharSet="134" ss:Size="14" 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="宋体" x:CharSet="134" ss:Size="12" 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="<%=DM.Records.Count*8%>" x:FullColumns="1"

x:FullRows="1" ss:DefaultColumnWidth="54" 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">客户明细信息</Data></Cell>

</Row>

<Row ss:AutoFitHeight="0" ss:Height="20.0625">

<Cell ss:StyleID="s45"><Data ss:Type="String">公司名称</Data></Cell>

<Cell ss:MergeAcross="3" ss:StyleID="m15741130"><Data ss:Type="String"><%=DM.Records[i]["CompanyName"]%></Data></Cell>

</Row>

<Row ss:AutoFitHeight="0" ss:Height="20.0625">

<Cell ss:StyleID="s45"><Data ss:Type="String">联系人</Data></Cell>

<Cell ss:MergeAcross="1" ss:StyleID="m15741120"><Data ss:Type="String"><%=DM.Records[i]["ContactName"]%></Data></Cell>

<Cell ss:StyleID="s40"><Data ss:Type="String">联系人职务</Data></Cell>

<Cell ss:StyleID="s84"><Data ss:Type="String"><%=DM.Records[i]["ContactTitle"]%></Data></Cell>

</Row>

<Row ss:AutoFitHeight="0" ss:Height="20.0625">

<Cell ss:StyleID="s45"><Data ss:Type="String">地址</Data></Cell>

<Cell ss:MergeAcross="3" ss:StyleID="m15741140"><Data ss:Type="String"><%=DM.Records[i]["Address"]%></Data></Cell>

</Row>

<Row ss:AutoFitHeight="0" ss:Height="20.0625">

<Cell ss:StyleID="s45"><Data ss:Type="String">邮政编码</Data></Cell>

<Cell ss:MergeAcross="1" ss:StyleID="s85"><Data ss:Type="String"><%=DM.Records[i]["PostalCode"]%></Data></Cell>

<Cell ss:StyleID="s40"><Data ss:Type="String">城市</Data></Cell>

<Cell ss:StyleID="s84"><Data ss:Type="String"><%=DM.Records[i]["City"]%></Data></Cell>

</Row>

<Row ss:AutoFitHeight="0" ss:Height="20.0625">

<Cell ss:StyleID="s45"><Data ss:Type="String">国家</Data></Cell>

<Cell ss:MergeAcross="1" ss:StyleID="m15730030"><Data ss:Type="String"><%=DM.Records[i]["Country"]%></Data></Cell>

<Cell ss:StyleID="s40"><Data ss:Type="String">地区</Data></Cell>

<Cell ss:StyleID="s84"><Data ss:Type="String"><%=DM.Records[i]["Region"]%></Data></Cell>

</Row>

<Row ss:AutoFitHeight="0" ss:Height="20.0625">

<Cell ss:StyleID="s48"><Data ss:Type="String">电话</Data></Cell>

<Cell ss:MergeAcross="1" ss:StyleID="m15730020"><Data ss:Type="String"><%=DM.Records[i]["Phone"]%></Data></Cell>

<Cell ss:StyleID="s50"><Data ss:Type="String">传真</Data></Cell>

<Cell ss:StyleID="s88"><Data ss:Type="String"><%=DM.Records[i]["Fax"]%></Data></Cell>

</Row>

<Row ss:Height="20">

<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="54" 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="54" 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 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>

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

<%%>标记的原理和ASP是一样的。

天啊这么复杂的XML自己如何写啊?我们不要忘了Excel,完全可以用Excel做模板导出XML的。

模板里的HFSoft.Exports.DataManager是我自己写的一个数据管理类,用于数据获取和分组的。

模板已经出来的,那我如何调用他来生成XML文件呢?

以下是WEBFORM的一些调用代码。

private void Report(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);

Report("CustomerList.cst",myDS);

}

这样用XML导出EXCLE的事例就结束了。

其原理很简单就是数据结合XML生成最终XML文档给EXCEL打开,实现这样功能的做法也有很多。

对自己来说当然是选取自己感觉最方便的方法去实现。

我为了方便选用了第三工具做最烦琐的事件:)

这里不可以上传附件,如果想要具体例子代码或交流可以发邮件或MSN:henryfan@msn.com;

打印效果图:

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有