关于excel导入数据库的问题,请帮忙。
1、请教一下各位,这样的excel表,怎样用程序实现手动往数据库里导呀。
河北 承德 兴隆 18C1890236 - - - - 集团客户
河北 承德 隆化 18C1890247 - - - - 集团客户
2、我用toad以文本的方式导了一下,提示“-”错误,如果把-换成0就能导进去了。
------解决方案--------------------office 组件导入就行了啊,读取excle,插入到数据库 。你是怎么导入的
------解决方案--------------------3、 //Excel导入DtataSet操作。
public DataSet ExecleToDataSet(string filenameurl, string table)
{
DataSet ds = new DataSet();
try
{
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter odda = new OleDbDataAdapter("select * from [Sheet1$]", conn);
odda.Fill(ds, table);
}
catch (Exception)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('Excel文件数据有误!');</script>");
}
return ds;
}
4、导入
//确定导入
protected void but_tmp_Import_Click(object sender, EventArgs e)
{
if (this.FileUpload1.FileName.ToString() == "")
{
Response.Write("<script language='javascript'>alert('导入文件不能为空,请先上传!');</script>");
}
if (this.FileUpload1.HasFile)
{
string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
if (fileExtension != ".xls")
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('导入仅支持.xls文件,请重新选择!');</script>");
return;
}
else
{
string path = Server.MapPath("~/Excel/");
string filetime = DateTime.Now.ToString("yyyyMMddHHmmss");//时间字段区分文件(同名文件覆盖)
string fiepath = path + "\\" + filetime + "\\" + FileUpload1.FileName;
//string fiepath = path + "\\" + fu_Excel.FileName;
if (!System.IO.Directory.Exists(path + "\\" + filetime))
{
System.IO.Directory.CreateDirectory(path + "\\" + filetime);
}
FileUpload1.PostedFile.SaveAs(fiepath);
//上传操作。
DataSet ds = ExecleToDataSet(fiepath, FileUpload1.FileName);
if (ds.Tables.Count == 0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>Show('Excel表为空表,无数据!');</script>");
return;
}
DataRow[] dr = ds.Tables[0].Select();
int rowsnum = ds.Tables[0].Rows.Count;
if (rowsnum == 0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>Show('Excel表为空表,无数据!');</script>");
return;
}
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
workbook.Open(fiepath);
Aspose.Cells.Worksheets wsts = workbook.Worksheets;
if (wsts.Count > 0)
{