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 BaseClass;
using System.Data.OleDb;
public partial class freekpiselect_uploadCIexcel : System.Web.UI.Page
{
string uploadFolder = "../upload"; // 上传文件夹
protected void Page_Load(object sender, EventArgs e)
{
HttpFileCollection files = Request.Files;
if (files.Count == 0)
{
Response.Write("<mce:script type="text/javascript"><!--
parent.callbackForUpload('false')
// --></mce:script>");
Response.End();
}
/// Response.Write("请勿直接访问本文件");
string path = Server.MapPath(uploadFolder);
// 只取第 1 个文件
HttpPostedFile file = files[0];
try
{
if (file != null && file.ContentLength > 0)
{
string fileName=file.FileName;//获取文件名
if (fileName.Substring(fileName.Length - 4).ToUpper() != ".XLS")
{
Response.Write("<mce:script type="text/javascript"><!--
parent.callbackForUpload('notxls')
// --></mce:script>");
}
else
{
string savePath = path + "/" + "tempCI.xls";
if (File.Exists(savePath))
{
File.Delete(savePath);
}
file.SaveAs(savePath);
DataSet sXmlds = ExcelToXml(savePath); //Global.ExcelHelper.ExcelToDataSet(savePath);//
DataTable dtAll = sXmlds.Tables[0];
string sCI = ",";
for (int i = 0; i < dtAll.Rows.Count; i++)
{
if (dtAll.Rows[i][0] != null && dtAll.Rows[i][0].ToString() != "")
{
sCI += "," + dtAll.Rows[i][0];
}
}
sCI = sCI.Substring(1);
Response.Write("<mce:script type="text/javascript"><!--
parent.callbackForUpload('" + sCI + "')
// --></mce:script>");
}
Response.End();
}
else
{
Response.Write("<mce:script type="text/javascript"><!--
parent.callbackForUpload('nofiles')
// --></mce:script>");
}
}
catch (Exception ex)
{
}
}
public DataSet ExcelToXml(string sPath)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + sPath + ";" + "Extended Properties=Excel 8.0;";
// OleDbConnection conn = new OleDbConnection(strConn);
DataSet ds = null;
using (OleDbConnection conn = new OleDbConnection(strConn))
{
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = "select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommand.Fill(ds, "table1");
conn.Close();
}
File.Delete(sPath);
return ds;
}
}
C# 获取HTML表单文件上传
最新推荐文章于 2023-05-20 10:42:40 发布