关于Excel 上传到服务器,再导入数据库的问题!

iloveyou19890403 2011-06-08 09:47:51
RT,就是通过 FileUpload 控件把Excel 文件先上传到服务器,然后通过 Button 按钮导入到 SQL Server 2008的的 数据库中。
谁有类似实现这样功能比较全的代码的,发上来看一下,不甚感激!因为没接触过,所以不太会。
还有就是 Excel 中的字段比数据库中的少几个,还有值为空的字段, 导入的时候又该怎么处理?
...全文
146 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
iloveyou19890403 2011-06-09
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 wxr0323 的回复:]

导入 其实就是读取

上传组件
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.……
[/Quote]
子夜__ 2011-06-08
  • 打赏
  • 举报
回复
导入 其实就是读取

上传组件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
string name = FileUpload1.FileName;//上传文件名字
string size = FileUpload1.PostedFile.ContentLength.ToString();
string type = FileUpload1.PostedFile.ContentType;
string type2 = name.Substring(name.LastIndexOf(".") + 1);
string ipath = Server.MapPath("upimg") + "\\" + name;
string fpath = Server.MapPath("upfile") + "\\" + name;
string path="F:\\aaa\\"+FileUpload1.FileName;
string wpath = "upimg\\" + name;
if (type2 == "jpg" || type2 == "gif" || type2 == "bmp" || type2 == "png")
{
FileUpload1.SaveAs("F:\\aaa\\"+FileUpload1.FileName);
// Image1.ImageUrl="F:\\aaa\\"+FileUpload1.FileName;
Label1.Text = "你传图片的名字是" + name + "<br>文件大小为" + size + "<br>文件类型为" + type2 + "<br>文件路径为" + ipath;
}

SqlConnection cn = new SqlConnection("server=.;database=Northwind;uid=sa;pwd=sa");
SqlCommand cmd = new SqlCommand("insert into Image(imageName,imagepath) values('" + name + "','" + path + "')", cn);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{ SqlConnection cn = new SqlConnection("server=.;database=Northwind;uid=sa;pwd=sa");
SqlCommand cmd = new SqlCommand("select imageName from Image where imageID='" + Convert.ToInt32(TextBox1.Text) + "'", cn);
cn.Open();
string a = cmd.ExecuteScalar().ToString();
cn.Close();
Image1.ImageUrl = "F:\\aaa\\" + a;
}
}



读取EXCEL

 DataTable Excel_UserInfo = new DataTable();
string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileInfo.FullName + ";" + "Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1;\"";
string strExcel = "select * from [sheet1$]";

using (OleDbDataAdapter adaptor = new OleDbDataAdapter(strExcel, strConn))
{
DataSet ds = new DataSet();
adaptor.Fill(ds);
Excel_UserInfo = ds.Tables[0];
}
net5354 2011-06-08
  • 打赏
  • 举报
回复
把Excel读到数据集中,循环数据集逐一插入就可以了
liangliangcai 2011-06-08
  • 打赏
  • 举报
回复
讲得不错
iloveyou19890403 2011-06-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zmcici 的回复:]

C# code

string name = "QA_Text_Template";
string fileName = name + fileUp.FileName.Substring(fileUp.FileName.IndexOf("."));
fileUp.SaveAs(Server.MapPath("../Upload/" + fileN……
[/Quote]
ExcelToDataTable 是一个方法吗?是的话,给出具体的哦~
自由_ 2011-06-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zmcici 的回复:]

C# code

string name = "QA_Text_Template";
string fileName = name + fileUp.FileName.Substring(fileUp.FileName.IndexOf("."));
fileUp.SaveAs(Server.MapPath("../Upload/" + fileN……
[/Quote]
+1
我也是这样做的
iloveyou19890403 2011-06-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 porschev 的回复:]
参考。。。网上很多这类的例子。。
[/Quote]
恩,那个 Save函数里面具体的方法是什么的?
chenhongjun0624 2011-06-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 porschev 的回复:]
参考。。。网上很多这类的例子。。
[/Quote]

这个不错。
Lyulf 2011-06-08
  • 打赏
  • 举报
回复
楼上的方法不错
骑猪看海 2011-06-08
  • 打赏
  • 举报
回复

string name = "QA_Text_Template";
string fileName = name + fileUp.FileName.Substring(fileUp.FileName.IndexOf("."));
fileUp.SaveAs(Server.MapPath("../Upload/" + fileName));
ViewState["tbExcel"] = ExcelToDataTable(Server.MapPath("../Upload/" + fileName), "Sheet1");
string result = string.Empty;
DataTable dtExcel = ViewState["tbExcel"] as DataTable;

string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strExcelFileName + ";" + "Extended Properties=Excel 5.0;";
string strExcel = string.Format("select * from [{0}$]", strSheetName);
DataSet ds = new DataSet();
using (OleDbConnection conn = new OleDbConnection(strConn))
{
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
adapter.Fill(ds, strSheetName);
conn.Close();
}
return ds.Tables[strSheetName];
  • 打赏
  • 举报
回复
上传不是问题
Button 按钮 直接导入到 SQL Server 2008的的 数据库中。这是个问题

等高人。。

62,041

社区成员

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

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

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

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