c# .net将SQL数据表导入excel

认真学编程 2011-09-20 10:01:17
利用c#在web中实现将SQL数据表导入excel中,该怎么实现啊!第一次接触此类编程,请教啦!
...全文
56 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
认真学编程 2011-09-20
  • 打赏
  • 举报
回复
是不是要创建个EXCEL表格,再把数据导进去?还是有其他方法?
cjh200102 2011-09-20
  • 打赏
  • 举报
回复
去51ASPX去下载
_三皮_ 2011-09-20
  • 打赏
  • 举报
回复
protected void btnDataAdapter_Click(object sender, EventArgs e)
{
////select top 10 username,address,zipcode,Telephone,email,convert(char(10),applydate,101) as applydate,Moneystr from tb_Trial_Centre order by applydate desc
//SqlConnection con = new SqlConnection("Persist Security Info=False;database=ghh_GHHDISCUZ_V3;server=s259090sg8ew06;user id=wghh_dbo;password=BR$24tYX;Current Language=English;Connection Timeout=240;");
////string sqlselect = @"select top 10 username,address,zipcode,Telephone,email,convert(char(10),applydate,101) as applydate,Moneystr,applydate applydate2 from tb_Trial_Centre where status=1 order by applydate desc";
//string sqlselect = @"select username,address,zipcode,Telephone,email,applydate,Moneystr from tb_Trial_Centre where status=1 order by applydate desc";


SqlConnection con = new SqlConnection(sql);
string sqlselect = @"" + this.txtSqlCommand.Text.Trim() + "";


SqlDataAdapter sda = new SqlDataAdapter(sqlselect, con);
sda.SelectCommand.CommandTimeout = 240;
con.Open();
DataSet ds = new DataSet();
sda.Fill(ds);
if (ds != null && ds.Tables.Count > 0)
{
GridView gv = new GridView();
gv.DataSource = ds.Tables[0];
gv.DataBind();
Response.Buffer = true;
Response.Charset = "utf-8";
gv.EnableViewState = false;
Response.ContentType = "application/ms-excel";
Response.Write("<meta http-equiv=Content-Type; content=text/html;charset=utf-8>");
Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToShortDateString() + ".xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
//System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("en-US", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
gv.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
con.Close();
}
认真学编程 2011-09-20
  • 打赏
  • 举报
回复
问题解决了,自己找了一段,不过还是谢谢!
//创建excel表格
Microsoft.Office.Interop.Excel.Application myexcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
myexcel.Visible = true;
Microsoft.Office.Interop.Excel.Workbooks myworkbooks = myexcel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook myworkbook = myworkbooks.Add(System.Reflection.Missing.Value);
Microsoft.Office.Interop.Excel.Worksheet myworksheet = (Microsoft.Office.Interop.Excel.Worksheet)myworkbook.Worksheets[1];
Microsoft.Office.Interop.Excel.Range myrange = myworksheet.get_Range("A1", "G1");
object[] myheader ={"", "", ""};//表头
myrange.Value2 = myheader;
SqlConnection con = new SqlConnection(connectstring);
con.Open();
DataSet set = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(selectstring,con);
sda.Fill(set, "table1");
for (int i = 1; i <= set.Tables[0].Rows.Count; i++)
{

for (int j = 0; j < set.Tables[0].Columns.Count; j++)
{
int k = i + 1;
int n = j + 1;
myworksheet.Cells[k, n] = set.Tables[0].Rows[i - 1][j].ToString();
}
}

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧