求用上传控件把把EXECL导入到GridView的代码,要详细,确实好用的,急~谢~
解决方案 »
- dataset的问题
- 请教一个关于利用HttpWebRequest访问其他域名的页面写cookie问题,急!谢谢
- JS脚本与苹果浏览器兼容的问题
- cmd.Parameters.Add("@uid",SqlDbType.NVarChar,50);这句什么意思?
- Attributes.Add怎么设置字体颜色
- (大家多支持)请教:web.config文件中配置<authorization>的角色的问题。
- 两个关于treeview的问题
- 学习C#和ASP.NET编写网站,需要学习javascript吗?(不会浪费你太多时间的)
- 根据两个dropdrowlist选择的内容写入SQL
- [讨论]能否取得某控件的发往服务器端的JScript?即:"javascript:_dopostback(……)"
- WIN2003server SP2中 IE7的安全级别怎么改不了啊?按钮是灰色的
- ASP.NET(C#) 如何定时执行一段代码?
private const string strConn = "自己写连接字符串";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataToGridView();
}
} /// <summary>
/// 绑定
/// </summary>
private void BindDataToGridView()
{
SqlConnection conn = new SqlConnection(strConn);
string strSQL = "select * from [User]";
SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
DataSet ds = new DataSet();
da.Fill(ds, "[User]");
this.GridView1.DataSource = ds;
this.GridView1.DataKeyNames = new string[] { "UserId" };
this.GridView1.DataBind();
} /// <summary>
/// 导出DataSet到Execl
/// </summary>
private void OutExecl()
{
Excel.Application myExcel = new Excel.Application();
myExcel.Visible = true; if (myExcel == null)
{
Page.RegisterStartupScript("", "<script>alert('EXCEL无法启动');</script>");
} Workbook work = myExcel.Application.Workbooks.Add(Type.Missing);
Worksheet sheet = (Worksheet)work.Worksheets[1]; int rowCount = 0;
int columnCount = 0; columnCount = this.GridView1.Columns.Count;
rowCount = this.GridView1.Rows.Count; rowCount--; for (int m = 1; m < columnCount; m++)
{
sheet.Cells[1, m] = this.GridView1.Columns[m].HeaderText;//得到列标题文本
} for (int i = 0; i <= rowCount; i++) //二维表填充从每一行开始
{
for (int j = 1; j < columnCount; j++) //填充每一行第j列单元格,循环添加行列数据
{
sheet.Cells[i + 2, j] = this.GridView1.Rows[i].Cells[j].Text;
}
}
Page.RegisterStartupScript("", "<script>alert('成功导出!');</script>");
} /// <summary>
/// 导出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnOut_Click(object sender, EventArgs e)
{
OutExecl();
} /// <summary>
/// 导入Excel到DataSet
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
private DataSet InExecl(string filePath)
{
DataSet ds = new DataSet();
string connStr = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\""; OleDbConnection myConn = new OleDbConnection(connStr);
string strSQL = " SELECT * FROM [Sheet1$]";
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strSQL, myConn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet, "[Sheet1$]");
myConn.Close(); return myDataSet;
}
/// <summary>
/// 导入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnIn_Click(object sender, EventArgs e)
{
DataSet ds = InExecl(this.File1.PostedFile.FileName);
this.GridView2.DataSource = ds;
this.GridView2.DataBind();
}
}这个是导入到dataset里然后你绑定到GridView就行
{
DataSet dsE = new DataSet();
string MyConnectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + @System.Web.HttpContext.Current.Server.MapPath("ExcelTemp") + @"\" + ExcelName+ ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1'";
string str = "SELECT * FROM [" + Sheet + "$]";
OleDbConnection myconn = new OleDbConnection(MyConnectionstring);
myconn.Open();
OleDbDataAdapter adp = new OleDbDataAdapter(str, myconn);
adp.Fill(dsExcel, "ExcelTemp"); myconn.Close();
return dsE;
}
参考
http://www.cnblogs.com/shiningrise/archive/2007/05/18/751391.html
//上传代码
//fileUpload是FileUpload控件
protected void btnUpload_Click(object sender, EventArgs e)
{
//判断是否上传了文件
if (fileUpload.HasFile)
{
//指定上传文件在服务器上的保存路径
string savePath = Server.MapPath("~/upload/");
//检查服务器上是否存在这个物理路径,如果不存在则创建
if (!System.IO.Directory.Exists(savePath))
{
System.IO.Directory.CreateDirectory(savePath);
}
string fileName = savePath + "\\" + fileUpload.FileName;
fileUpload.SaveAs(fileName);//保存文件
//不过需要注意的是,在客户端访问却需要指定的是URL地址,而不是在服务器上的物理地址
GridView1.DataSource = BindData(savePath,fileName,'sheet1');
GridView1.DataBind();
}
} //excelPath 路径
//excelName 文件名
//sheet excel表名
private static DataView BindData(string excelPath, string excelName, string sheet)
{
DataSet ds = new DataSet();
string connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + @System.Web.HttpContext.Current.Server.MapPath(excelPath) + @"\" + excelName+ ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1'";
string str = "SELECT * FROM [" + sheet + "$]";
OleDbConnection myconn = new OleDbConnection(connectionstring);
myconn.Open();
OleDbDataAdapter adp = new OleDbDataAdapter(str, myconn);
adp.Fill(ds, sheet);
myconn.Close();
return ds.Table[0].DefaultView;
}
{
//判断是否上传了文件
if (fileUpload.HasFile)
{
//指定上传文件在服务器上的保存路径
string savePath = Server.MapPath("~/upload/");
//检查服务器上是否存在这个物理路径,如果不存在则创建
if (!System.IO.Directory.Exists(savePath))
{
System.IO.Directory.CreateDirectory(savePath);
}
string fileName = savePath + "\\" + fileUpload.FileName;
fileUpload.SaveAs(fileName);//保存文件
//不过需要注意的是,在客户端访问却需要指定的是URL地址,而不是在服务器上的物理地址
GridView1.DataSource = BindData(savePath,fileName,'sheet1');
GridView1.DataBind();
}
} //excelPath 路径
//excelName 文件名
//sheet excel表名
private static DataView BindData(string excelPath, string excelName, string sheet)
{
DataSet ds = new DataSet();
string connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + @System.Web.HttpContext.Current.Server.MapPath(excelPath) + @"\" + excelName+ ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1'";
string str = "SELECT * FROM [" + sheet + "$]";
OleDbConnection myconn = new OleDbConnection(connectionstring);
myconn.Open();
OleDbDataAdapter adp = new OleDbDataAdapter(str, myconn);
adp.Fill(ds, sheet);
myconn.Close();
return ds.Table[0].DefaultView;
}
(this.File1.PostedFile.FileName);file1??是什么意思 是控件 还是需要引用的额?????