asp.net导入Excel出错============有图
导入Excel excel --------------------编程问答-------------------- 用npoi吧 --------------------编程问答--------------------
//导入Excel文件
protected void ImportExcel_Click1(object sender, EventArgs e)
{
string filePath = "";
string getErrorMsg = "";
DataSet ds = new DataSet();
if (!fuFile.HasFile)
{
Response.Write("<script>alert('请选择你要导入的Excel文件');</script>");
return;
}
//获取文件的后缀名
string fileExt = System.IO.Path.GetExtension(fuFile.FileName);
if (fileExt != ".xls")
{
Response.Write("<script>alert('文件类型错误!');</script>");
return;
}
//获取绝对路径
filePath = fuFile.PostedFile.FileName;
//string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"dBASE IV;HDR=Yes;IMEX=1\";Data Source = " + filePath + "";
string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";Data Source=" + filePath;
OleDbConnection excelCon = new OleDbConnection(conn);
//output是Excel文件里面工作表名 默认为Sheet1,后面需要加上$符号
//OleDbDataAdapter odda = new OleDbDataAdapter("SELECT * FROM [output$]", excelCon);
OleDbDataAdapter odda = new OleDbDataAdapter("SELECT * FROM [Tab_info$]", excelCon);
try
{
odda.Fill(ds, "Props_Type");
}
catch (Exception ex)
{
Response.Write(ex.Message);
Response.Write("<script>alert('" + ex.Message + "!')</script>");
}
finally
{
excelCon.Close();
excelCon.Dispose();
}
//将数据写到数据库里面
if (ds.Tables[0].Rows.Count != 0)
{
string sql = "";
//SqlConnection con = DBHelper.Connection;
SqlConnection con = new SqlConnection(connectionString);
SqlCommand sqlCmd = con.CreateCommand();
SqlTransaction sqlTran = con.BeginTransaction();
sqlCmd.Transaction = sqlTran;
try
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
//道具类型名称为output工作表里面的字段
sql = "INSERT INTO Tab_info VALUES('" + ds.Tables[0].Rows[i]["anum"] + "')";
sqlCmd.CommandText = sql;
sqlCmd.ExecuteNonQuery();
}
sqlTran.Commit();
}
catch (Exception ex)
{
getErrorMsg = ex.Message;
Response.Write(ex.Message);
sqlTran.Rollback();
}
finally
{
con.Close();
con.Dispose();
}
if (getErrorMsg == "")
{
Response.Write("<script>alert('导入Excel文件成功!')</script>");
}
else { Response.Write("<script>alert('导入Excel文件失败!')</script>"); }
}
}
来demo啊 --------------------编程问答--------------------
--------------------编程问答--------------------
/// <summary>
/// 获取Excel的数据集
/// </summary>
/// <param name="uploadFile"></param>
/// <returns></returns>
protected DataSet ExcelInsert(HttpPostedFile uploadFile)
{
string fileName, name;
//fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + FileUpload1.PostedFile.FileName;
//fileName = System.Web.HttpContext.Current.Server.MapPath("~/") + fileName;
name = System.IO.Path.GetFileName(uploadFile.FileName);
fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + name;
fileName = System.Web.HttpContext.Current.Server.MapPath("~/") + "files//ExportImport//" + fileName;
FileUpload1.SaveAs(fileName);
string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + fileName + ";" +
"Extended Properties=Excel 8.0;";
//建立EXCEL的连接
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [专线包裹信息导出$]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet objDataset1 = new DataSet();
objAdapter1.Fill(objDataset1, "XLData");
return objDataset1;
}
大神 你这是 --------------------编程问答--------------------
给我来个 导出 导入的 demo 好不 --------------------编程问答-------------------- 你稍微改一下 不就行了吗
HttpPostedFile uploadFile = this.Page.Request.Files[0];
DataSet ds = new DataSet();
ds = ExcelInsert(uploadFile); --------------------编程问答--------------------
http://npoi.codeplex.com/这是官网.自己去下载一个 --------------------编程问答--------------------
你稍微改一下 不就行了吗
HttpPostedFile uploadFile = this.Page.Request.Files[0];
DataSet ds = new DataSet();
ds = ExcelInsert(uploadFile);
您说的是 在您的代码改 还是在我的代码改啊 没有明白 晕的啊 --------------------编程问答--------------------
你稍微改一下 不就行了吗
HttpPostedFile uploadFile = this.Page.Request.Files[0];
DataSet ds = new DataSet();
ds = ExcelInsert(uploadFile);
您说的是 在您的代码改 还是在我的代码改啊 没有明白 晕的啊
在我的基础上改一下 就方法里面的查询语句改一下就行了
SELECT * FROM [专线包裹信息导出$]改成你要查询的表
--------------------编程问答-------------------- 你给的是导出吧 我是导出 导入都要啊 --------------------编程问答--------------------
你稍微改一下 不就行了吗
HttpPostedFile uploadFile = this.Page.Request.Files[0];
DataSet ds = new DataSet();
ds = ExcelInsert(uploadFile);
您说的是 在您的代码改 还是在我的代码改啊 没有明白 晕的啊
在我的基础上改一下 就方法里面的查询语句改一下就行了
SELECT * FROM [专线包裹信息导出$]改成你要查询的表
我是导出 导入 都要 啊 --------------------编程问答-------------------- asp.net导出Excel
补充:.NET技术 , ASP.NET