public partial class _Default : System.Web.UI.Page
{
private void setBind() {
DirectoryInfo info = new DirectoryInfo(Server.MapPath("images"))
this.DataListpic.DataSource = info.GetFiles()
this.DataListpic.DataBind()
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
setBind()
}
}
protected void btn_delete_Click(object sender, EventArgs e)
{
foreach (DataListItem it in this.DataListpic.Items)
{
CheckBox cb = it.FindControl("cbx_delete") as CheckBox
if (cb != null) {
if (cb.Checked) {
File.Delete(Server.MapPath("images/")+cb.Text)
}
}
}
setBind()
}
protected void btn_upload_Click(object sender, EventArgs e) {
if (this.FileUpload1.HasFile)
{
string name = this.FileUpload1.FileName
this.FileUpload1.SaveAs(Server.MapPath("images/" + name))
Response.Write("上传成功!!")
setBind()
}
else
{
Response.Write("没有选中文件!!")
}
}
protected void DataListpic_SelectedIndexChanged(object sender, EventArgs e)
{
setBind()
}
}
参考代码如下:把Excel中的数据导入gridView显示,再导入数据库
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.OleDb
//创建一个临时DataTable,为了把Excel中的数据导入gridView后再导入数据库。
private static DataTable dtTemp
/// <summary>
/// 把Excel导入Gridview,首先把文件上传到服务器
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnImport_Click(object sender, EventArgs e)
{
//导入Excel文件
//检查文件是否存在
//HasFile用来检查上轿拆传文件控件FileUpload是否有指定文件
if (FileUpload1.HasFile == false)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('请您选择Excel文件! ')</script>")
return//当无文件时,返回
}
string filename = DateTime.Now.ToString("yyyymmddhhMMss") + FileUpload1.FileName
//获取Execle文件名 DateTime日期函数
string savePath = Server.MapPath(("../Doc/") + filename)//Server.MapPath 获得虚拟服务器相差大对路径
//如果已经存在就清空
ClearFile(Server.MapPath("../Doc/"))
FileUpload1.SaveAs(savePath) //SaveAs 将上传的文件内容保存在服务器上
DataTable dt = createDataSource(savePath)
if (dtTemp == null)
{
dtTemp = new DataTable()
dtTemp = dt
}
GridView1.DataSource = dt
GridView1.DataBind()
}
/// <summary>
/// 从gridview导入数据库
/// </summary>闭庆枣
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSubmit_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable()
dt = dtTemp
for (int i = 0i <dt.Rows.Counti++)
{
//导入数据库的代码省略
}
Response.Write("<script>alert('添加成功!!')</script>")
}
/// <summary>
/// 将路径下的Excel文件转换为DataTable类型的数据源
/// </summary>
/// <param name="strPath">Excel路径</param>
/// <returns></returns>
private DataTable createDataSource(string strPath)
{
string strCon
strCon = "Provider=Microsoft.Jet.OLEDB.4.0Data Source=" + strPath + "Extended Properties=Excel 8.0"
OleDbConnection con = new OleDbConnection(strCon)
OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con)
DataTable dt = new DataTable()
da.Fill(dt)
return dt
}
/// <summary>
/// 将Excel文件暂存到服务器端的一个文件夹中,用这个方法删除掉
/// </summary>
/// <param name="FilePath">Excel路径</param>
private void ClearFile(string FilePath)
{
String[] files = System.IO.Directory.GetFiles(FilePath)
if (files.Length >5)
{
for (int i = 0i <5i++)
{
try
{
System.IO.File.Delete(files[i])
}
catch
{
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)