*********怎么实现把SQLSERVER数据库数据导出到excel***********

fhuibo 2008-03-20 04:38:26
问题是这样的:
数据库服务器 192.168.1.78 (win2000+sql server2000 再没有其他的程序,也没有OFFICE和IIS)
应用程序服务器 192.168.1.151 (win2003,iis,office和.net框架 没有任何数据库)

客户PC 192.168.1.8(xp,没有数据库和IIS,有OFFICE) 在访问程序的时候怎么把SQLSERVER里的数据导出到客户端的EXCEL中。
请教解决的方法和思路,能给出代码最好!!!谢谢!!!
...全文
130 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
贫僧又回来了 2008-03-20
  • 打赏
  • 举报
回复
exec master..xp_cmdshell 'bcp " + this.comboBox2.SelectedItem.ToString() + ".dbo." + this.comboBox3.SelectedItem.ToString() + " out " + this.saveFileDialog1.FileName.ToString() + " -c -q -S" + this.comboBox1.SelectedItem.ToString() + " -U" + this.textBox2.Text.ToString() + " -P" + this.textBox3.Text.ToString() + "'
ksmark 2008-03-20
  • 打赏
  • 举报
回复
string strTop;
strTop = "用户ID\t用户姓名\t密码\t部门\t电话\t电子邮件\t注册日期";
strSql = "Select UserID,UserName,Pwd,Department,Tel,Email,RegDate From Users";
ToExcel UserToExcel = new ToExcel();
StringWriter strWriteUser=UserToExcel.DgToExcel(ref strTop, ref strSql);
Response.AddHeader("Content-Disposition", "attachment;filename=User.xls");
Response.ContentType = "application/vnd.ms-Excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.Write(strWriteUser);
Response.End();



public StringWriter DgToExcel(ref string strTop,ref string strSql)
{
//
// TODO: 在此处添加构造函数逻辑
//

StringWriter StrWrite = new StringWriter();
//导出为Excel
StrWrite.WriteLine(strTop);
//导出为csv
//StrWrite.WriteLine("abc,bcd");

ExecuteSql execExcel = new ExecuteSql();
DataTable DTExcel = execExcel.ExecSqlBMS(ref strSql);
foreach (DataRow iRow in DTExcel.Rows)
{
//导出为csv
//StrWrite.WriteLine(iRow.ItemArray[1].ToString()+","+iRow.ItemArray[2].ToString());
//导出为Excel
string strCols = "";
for (int iCol = 0; iCol < DTExcel.Columns.Count;iCol++ )
{
strCols = strCols + iRow.ItemArray[iCol].ToString() + "\t";

}
StrWrite.WriteLine(strCols);
//StrWrite.WriteLine(iRow.ItemArray[0].ToString() + "\t" + iRow.ItemArray[1].ToString()
//+ "\t" + iRow.ItemArray[2].ToString() + "\t" + iRow.ItemArray[3].ToString()
//+ "\t" + iRow.ItemArray[4].ToString() + "\t" + iRow.ItemArray[5].ToString()
//+ "\t" + iRow.ItemArray[6].ToString());
}
StrWrite.Close();
return StrWrite;

}
marey_marey111 2008-03-20
  • 打赏
  • 举报
回复
直接到入就可以。。C#里面有把表导成EXCEL的接口。。

110,526

社区成员

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

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

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