You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
5.4 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Data.OleDb;
using System.Data.SqlClient;
namespace DSWeb
{
public partial class ImportDemo : System.Web.UI.Page
{
private HttpFileCollection files;
private int fileCount;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_import_Click(object sender, EventArgs e)
{
string Extension = "";
string SavedName = "";
string upname = "";
string savedpath = "";
string realname = "";
string virtualname = "";
string path = "";
string OppPath = "TempFiles";
OppPath += "/";
string AbsPath = Server.MapPath(OppPath);
files = HttpContext.Current.Request.Files;
if (!Directory.Exists(Server.MapPath(OppPath)))
{
Directory.CreateDirectory(Server.MapPath(OppPath));
}
for (fileCount = 0; fileCount < files.Count; fileCount++)
{
HttpPostedFile postedFile = files[fileCount];
string fileName;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != string.Empty)
{
Extension = System.IO.Path.GetExtension(fileName);
////取上传文件的文件名
upname = System.IO.Path.GetFileName(fileName);
//系统自动产生的日期型文件名
SavedName = string.Format("{0:yyyyMMddHHmmss}", System.DateTime.Now);
////把文件保存到UploadFile文件中
dvResult.InnerHtml += AbsPath + SavedName + upname;
postedFile.SaveAs(AbsPath + SavedName + upname);
dvResult.InnerHtml += "&nbsp;true";
virtualname += OppPath + SavedName + upname + ",";
realname += upname + ",";
path = savedpath;
string strOldDb = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=";
dvResult.InnerHtml += strOldDb + AbsPath + SavedName + upname;
OleDbConnection OleCon = new OleDbConnection(strOldDb+AbsPath + SavedName + upname);
OleDbDataAdapter OleDAp = new OleDbDataAdapter("SELECT * FROM [Sheet1$] ", OleCon);
DataSet ds = new DataSet();
OleDAp.Fill(ds);
dvResult.InnerHtml += "&nbsp;true";
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
}
}
protected void btn_in_Click(object sender, EventArgs e)
{
//若此处未加逻辑判断如果excel文件不存在则程序将会报异常
if (System.IO.File.Exists(MapPath("TempFile")))
{
//OleDbConnection OleCon = new OleDbConnection(ExcelStr + MapPath("TempFile"));
//OleDbDataAdapter OleDAp = new OleDbDataAdapter("SELECT prgName,PlayTime,prgColumn FROM [Sheet1$] ", OleCon);
//DataSet ds = new DataSet();
//OleDAp.Fill(ds);
//string prgName, playTime, prgColumn, sSQL;
//SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["SqlString"]);
//conn.Open();
//SqlCommand cmd;
//try
//{
// foreach (DataRow dr in ds.Tables[0].Rows)
// {
// prgName = dr["prgName"].ToString();
// playTime = dr["PlayTime"].ToString();
// prgColumn = dr["prgColumn"].ToString();
// //sSQL = "Insert Into Ax_Program (prgName,PlayTime,prgColumn,prgComment) Values ('" + prgName + "','" + playTime + "','" + prgColumn + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm") + "Excel导入')";
// cmd = new SqlCommand(sSQL, conn);
// cmd.CommandType = CommandType.Text;
// cmd.ExecuteNonQuery();
// cmd.Dispose();
// }
//}
//catch (Exception)
//{
// Response.Write("<script language='javascript'>window.alert('导入失败')</script>;");
// return;
//}
//finally
//{
// conn.Close();
// conn.Dispose();
//}
//Response.Write("<script language='javascript'>window.alert('导入成功')</script>;");
//ExlDataGrid.Visible = false;
//OpenAndBindNew();
//lbWarning.Visible = false;
//lbWarningS.Visible = true;
//lbWarningS.Text = "本此操作导入的节目信息";
}
else
{
Response.Write("<script language='javascript'>window.alert('Excel文件不存在!')</script>;");
}
}
}
}