目次
データベース
サンプル5 データセットをXSLTを使って簡易的なCSV出力
データセットのテーブルをXSLTを使って簡単なCSV出力をします。
本来は項目に応じて文字列は””で区切ったりしなければいけないのですが、項目に応じてカスタマイズするのは実装する時に各自やってください。
こんなデータセットが。
dataSet11["部品"] テーブル(スキーマdataSet1)
レコード
製品名 部品 型番
A A1 A1-01
A A2 A2-01
こんなCSVファイルになります。
製品名,部品名,型番
A ,A1 ,A1-01
A ,A2 ,A2-01
ソースです。
ボタンを押した時にCSVファイルを作成します。
XSLTを変更、ボタン押下を繰り返せるようにしています。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
namespace CsvOutPut
{
public class Form1 : System.Windows.Forms.Form
{
private XmlDataDocument xmlDoc ;
private System.Data.SqlClient.SqlConnection sqlConnection1;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
private CsvOutPut.DataSet1 dataSet11;
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
sqlDataAdapter1.Fill(dataSet11) ;
xmlDoc = new XmlDataDocument(dataSet11);
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows フォーム デザイナで生成されたコード
private void InitializeComponent()
{
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.dataSet11 = new CsvOutPut.DataSet1();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
this.SuspendLayout();
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=\"DELL-XEON\";packet size=4096;integrated security=SSPI;data source=" +
"\"(local)\";persist security info=False;initial catalog=pubs";
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "部品", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("製品名", "製品名"),
new System.Data.Common.DataColumnMapping("部品名", "部品名"),
new System.Data.Common.DataColumnMapping("型番", "型番")})});
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT 製品名, 部品名, 型番 FROM 部品";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// dataSet11
//
this.dataSet11.DataSetName = "DataSet1";
this.dataSet11.Locale = new System.Globalization.CultureInfo("ja-JP");
//
// button1
//
this.button1.Location = new System.Drawing.Point(64, 48);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(200, 150);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
try
{
XslTransform xslTran = new XslTransform();
xslTran.Load(Application.StartupPath + @"\XSLTFile1.xslt");
XmlTextWriter writer = new XmlTextWriter(Application.StartupPath + @"\output.csv", System.Text.Encoding.UTF8);
xslTran.Transform(xmlDoc, null, writer, null);
writer.Close();
}
catch ( Exception ex )
{
string s = ex.Message ;
}
}
}
}
XSLTFile1.xslt XSLTファイルです。
実行ファイルと同じ場所においてください。
<xsl:stylesheet version="1.0" xmlns:mstns="http://www.tempuri.org/DataSet1.xsd" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:text>製品名,部品名,型番
</xsl:text>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="/mstns:DataSet1/mstns:部品">
<xsl:for-each select="mstns:*">
<!-- 全ての列を列挙 -->
<xsl:value-of select="text()"/>
<!-- 列と列の間にカンマを挿入 -->
<xsl:if test="position()!=last()">,</xsl:if>
<!-- 最後の列の後には改行を入れる -->
<xsl:if test="position()=last()"><xsl:text>
</xsl:text></xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
XSLTFile1.xslt こちらのXSLTファイルのように項目名を列挙してもいいです。
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:mstns="http://www.tempuri.org/DataSet1.xsd" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:text>製品名,部品名,型番
</xsl:text>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="/mstns:DataSet1/mstns:部品">
<xsl:value-of select="mstns:製品名"/>,<xsl:value-of select="mstns:部品名"/>,<xsl:value-of select="mstns:型番"/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>



