从sql的数据库中报表和表对应的列导入excell

SqlConnection conn = new SqlConnection();
conn.ConnectionString="initial catalog=pubs;data source=.;user id=sa;password=";
SqlCommand cmd = new SqlCommand();
cmd.CommandText=" select name from sysobjects where xtype='u'";
cmd.Connection=conn;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand=cmd;
DataSet ds = new DataSet();
da.Fill(ds,"tables");
DataTable dt = ds.Tables["tables"];
for(int i=0;i<dt.Rows.Count;i++)
{
cmd.CommandText="select * from "+Convert.ToString(dt.Rows[i][0]);
da.Fill(ds,(i+1).ToString());
}
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for(int i=0;i<ds.Tables["tables"].Rows.Count;i++)
{
sb.Append(dt.Rows[i][0].ToString()+"\r\n");
for(int j=0;j<ds.Tables[(i+1).ToString()].Columns.Count;j++)
{
sb.Append("\t"+ds.Tables[(i+1).ToString()].Columns[j].ColumnName);
sb.Append("\t\t");
sb.Append(ds.Tables[(i+1).ToString()].Columns[j].DataType);
sb.Append("\t\t");
sb.Append(ds.Tables[(i+1).ToString()].Columns[j].Ordinal);
sb.Append("\t\t");
sb.Append(ds.Tables[(i+1).ToString()].Columns[j].AutoIncrement);
sb.Append("\r\n");
}
}
StreamWriter sw = new StreamWriter(Server.MapPath("222.xls"),false,System.Text.Encoding.GetEncoding("gb2312"));
sw.Write(sb.ToString());
sw.Flush();
sw.Close();

public string filter(string str)
{
str = str.ToLower();
if(str=="system.string")
{
str="varchar";
}
else if(str=="system.boolean")
{
str="bit";
}
else if(str=="system.datetime")
{
str="datetime";
}

else if(str=="system.int32")
{
str="int";
}
else if(str=="system.int16")
{
str="smallint";
}
else if(str=="system.byte")
{
str="tinyint";
}
else if(str=="system.double")
{
str="float";
}
else if(str=="system.decimal")
{
str="decimal";
}
else
{

}
return str;
}

你可能感兴趣的:(sql,SQL Server,J#)