123456789101112131415DataSet ds = new DataSet()OleDbCommand excelCommand = new OleDbCommand()OleDbDataAdapter excelDataAdapter = new OleDbDataAdapter()string excelConnStr = "Provider=Microsoft.Jet.OLEDB.4.0Data Source=" + filelocation + "Extended Properties=""Excel 8.0HDR=YesIMEX=1"""OleDbConnection excelConn = new OleDbConnection(excelConnStr)excelConn.Open()DataTable dtPatterns = new DataTable()excelCommand = new OleDbCommand("SELECT * FROM [Sheet1$]", excelConn)excelDataAdapter.SelectCommand = excelCommandexcelDataAdapter.Fill(ds, "Customers")// Get a StreamWriter objectStreamWriter xmlDoc = new StreamWriter("Customers.xml")// Apply the WriteXml method to write an XML documentds.WriteXml(xmlDoc)xmlDoc.Close()
如果是Sql2005以上版本就简单了。SELECT TOP 10 * FROM dbo.UserInfo
FOR XML PATH('UserInfo'),ROOT ('UserList')
就会生成以下XML
PATH里面的字符串生成行标签
ROOT生成根标签
<UserList>
<UserInfo>
<UserName>administrator</UserName>
<Age>21</Age>
<NickName>小张</NickName>
......
</UserInfo>
<UserInfo>
<UserName>administrator</UserName>
<Age>21</Age>
<NickName>小张</NickName>
......
</UserInfo>
<UserInfo>
<UserName>administrator</UserName>
<Age>21</Age>
<NickName>小张</NickName>
......
</UserInfo>
......
</UserList>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)