如何实现从sql中导出数据到excel,excel导入sql

nyh800201002 2003-06-05 04:24:20
RT
...全文
45 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
cloud_scorpion 2003-06-10
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/1809/1809946.xml?temp=.4796106
killerwc 2003-06-10
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/1897/1897063.xml?temp=.5572473
lzx123 2003-06-10
  • 打赏
  • 举报
回复
up
nyh800201002 2003-06-10
  • 打赏
  • 举报
回复
up
gengwei80 2003-06-10
  • 打赏
  • 举报
回复
用工具导出
nyh800201002 2003-06-10
  • 打赏
  • 举报
回复
将datagrid的内容导入sql除了循环外还有没有其方法?
谢谢以上各位的解答
OnlyExpect 2003-06-09
  • 打赏
  • 举报
回复
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OWC;

namespace cominterop
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private SqlCommand sql;
protected System.Web.UI.WebControls.Button export2excel;
protected System.Web.UI.WebControls.TextBox xlfile;
private SqlConnection cnn;

private void Page_Load(object sender, System.EventArgs e)
{
this.BindDataGrid();
}

private void BindDataGrid() {
cnn = new SqlConnection("Initial Catalog=Northwind;Data Source=localhost;uid=sa;pwd=");
sql = new SqlCommand("select * from products",cnn);
cnn.Open();
SqlDataReader reader = sql.ExecuteReader();
this.DataGrid1.DataSource = reader;
this.DataGrid1.DataBind();
reader.Close();
cnn.Close();
}

private void WriteDataGrid2Excel() {
SpreadsheetClass xlsheet = new SpreadsheetClass();
cnn.Open();
SqlDataReader reader = this.sql.ExecuteReader();
int numbercols = reader.FieldCount;
int row=1;
while (reader.Read()) {
for (int i=0;i<numbercols;i++)
{
xlsheet.ActiveSheet.Cells[row,i+1] = reader.GetValue(i).ToString();
}
row++;
}
reader.Close();
cnn.Close();
xlsheet.ActiveSheet.Export(Server.MapPath(".")+"\\"+this.xlfile.Text,OWC.SheetExportActionEnum.ssExportActionNone);
}

private void export2excel_Click(object sender, System.EventArgs e)
{
if (this.xlfile.Text.Trim()!="")
{
this.WriteDataGrid2Excel();
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.export2excel.Click += new System.EventHandler(this.export2excel_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

}
}
leedeqiang 2003-06-09
  • 打赏
  • 举报
回复
出现没有可安装的isam,不知这是什么错误。
nyh800201002 2003-06-09
  • 打赏
  • 举报
回复
这只是把excel表导入数据库,那把数据库中的表如何导成excel呢?
nyh800201002 2003-06-09
  • 打赏
  • 举报
回复
to: OnlyExpect(冻冻)
using OWC;我该如何引用?
nyh800201002 2003-06-09
  • 打赏
  • 举报
回复
把excel表导入数据库时除拉读入DataSet,然后循环导入,还有其他方法吗?
mmkk 2003-06-05
  • 打赏
  • 举报
回复
public static DataSet GetExcelContent(string filepath)
{
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = "+filepath+";Extended Properties='Excel 8.0;IMEX=1'" ;
System.Data.OleDb.OleDbConnection myConn = new System.Data.OleDb.OleDbConnection (strCon) ;
string strCom = " SELECT * FROM [Sheet1$] " ;
myConn.Open( ) ;
System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom,myConn ) ;
//创建一个 DataSet对象
DataSet myDataSet = new DataSet( );
//得到自己的DataSet对象
myCommand.Fill (myDataSet , "[Sheet1$]" ) ;
//关闭此数据链接
myConn.Close();
return myDataSet;
}
nyh800201002 2003-06-05
  • 打赏
  • 举报
回复
up
nyh800201002 2003-06-05
  • 打赏
  • 举报
回复
说清楚点行吗?
mmkk 2003-06-05
  • 打赏
  • 举报
回复
读入DataSet,然后循环导入

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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