此段代码是针对DataTable 对xml、excel、csv 对文件的导入和导出功能,记录一下,以供以后使用。
一定要导入excel 并添加引用Microsoft.Office.Interop.Excel 11.0版本。
Default.aspx.cs文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
using System.Data.OleDb;
using System.Data.Odbc;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
namespace fantest
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bind();
}
protected void Bind()
{
this.GridView1.DataSource = this.GetDataTable();
this.GridView1.DataBind();
}
private DataTable GetDataTable()
{
DataSet ds = new DataSet();
using (SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=123456;database=test"))
{
string sql = "select * from InfoTable where 1=1";
SqlDataAdapter dap = new SqlDataAdapter(sql, conn);
dap.Fill(ds,"InfoTable");
}
return ds.Tables["InfoTable"];
}
//TO XML
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = this.GetDataTable();
StringBuilder sb = new StringBuilder();
sb.Append("<" + dt.TableName + ">");
foreach (DataRow row in dt.Rows)
{
sb.Append("<item>");
for (int i = 0; i < dt.Columns.Count; i++)
{