求excel导入SQL完整源码例!

appleller 2008-10-28 10:17:53
求excel导入SQL完整源码例!
...全文
135 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
gdivan 2008-11-02
  • 打赏
  • 举报
回复
直接在EXCEL VAB里写小循环代码.在SQL里有个导入工具也可以
appleller 2008-10-30
  • 打赏
  • 举报
回复
?
herohlq 2008-10-29
  • 打赏
  • 举报
回复
有导出的要吗
appleller 2008-10-29
  • 打赏
  • 举报
回复
不是导出,而是EXCEL导入SQL的例子

做成与SQL表相匹配的EXCEL格式,填好数据,然后选择这个EXCEL表,批量导入SQL表中,未成功导入的记录错误回写EXCEL.
chinaping 2008-10-29
  • 打赏
  • 举报
回复
excel功能太强大了,完全可以将单元格的数据进行拼写成SQL的
finalfly 2008-10-28
  • 打赏
  • 举报
回复
直接引用microsoft.office.interop.excel类库,然后就和写VBA差不多
或者在excel的VBA中直接写代码
nlqtonglin2006 2008-10-28
  • 打赏
  • 举报
回复
这种方式的原理是,在某页面(任何页面比如a.htm),把要导出的内容或结果集,通过表单提交到一个b.aspx页面。
aspx页面中,生成一个具有唯一文件名的excel文件,然后自动输出到前台。由前台来保存。
1.
a.htm页面,只需要把表单提交到b.aspx中即可。至于要提交什么内容,放入表单中的hidden控件即可。
你甚至可以动态改变hidden控件的value的值(这个很有用)。
关键字:
<form method="post" action="b.aspx" target="_blank">
<input type="hidden" name="TextSql" value="要提交的内容" ID="TextSql"></input>
</form>

2.
在aspx页面后台关键代码:


using System.IO;
using System.Text;

// 接收表单的值
private void Page_Load(object sender, System.EventArgs e)
{

string destString = Request.Form["TextSql"].ToString();
ExportToFile(destString);

}


//存为excel文件
private void ExportToFile(string esql)
{
//new 一个 guid
string FileName = Guid.NewGuid().ToString() + ".xls"; //GUID生成唯一文件名
string FileType="application/ms-excel";
try
{
System.Web.HttpResponse httpResponse = Page.Response;
httpResponse.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8));
httpResponse.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
httpResponse.ContentType = FileType;
StringBuilder ckpw = new StringBuilder(esql);
System.IO.StringWriter tw = new System.IO.StringWriter(ckpw) ;
string filePath = Server.MapPath("..")+"\\"+FileName;
System.IO.StreamWriter sw = System.IO.File.CreateText(filePath);
sw.Write(tw.ToString());
sw.Close();

DownFile(httpResponse,FileName,filePath);
httpResponse.End();
}
catch (Exception ee)
{
throw ee;
}
}


//输出到前台
private bool DownFile(System.Web.HttpResponse Response,string fileName,string fullPath)
{
try
{
Response.ContentType = "application/octet-stream";

Response.AppendHeader("Content-Disposition","attachment;filename=" +
HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8) + ";charset=GB2312");
System.IO.FileStream fs= System.IO.File.OpenRead(fullPath);
long fLen=fs.Length;
int size=102400;//每100K同时下载数据
byte[] readData = new byte[size];//指定缓冲区的大小
if(size>fLen)size=Convert.ToInt32(fLen);
long fPos=0;
bool isEnd=false;
while (!isEnd)
{
if((fPos+size)>fLen)
{
size=Convert.ToInt32(fLen-fPos);
readData = new byte[size];
isEnd=true;
}
fs.Read(readData, 0, size);//读入一个压缩块
Response.BinaryWrite(readData);
fPos+=size;
}
fs.Close();
System.IO.File.Delete(fullPath);
return true;
}
catch
{
return false;
}
}

你看看,我找的还没整理呢!
tfnpghl 2008-10-28
  • 打赏
  • 举报
回复
是将excel中的记录复制到SQL表吗

16,717

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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