一、asp如何导入excel内容
DataSet _Table = GetTableName(Server.MapPath(@"upload/" + Request.Cookies["sa"].Value + "/" + this.ListBox1.SelectedValue), this.DropDownList1.SelectedValue); //首先获取EXCEL文件路径
string[] list = Request["checkbox"].Split(",".ToCharArray()); 要导入的列,代码不贴了
string CompanyName = string.Empty, Person_in_charge = string.Empty, Mobile = string.Empty, Phone = string.Empty, Fax = string.Empty, E_mail = string.Empty, Address = string.Empty, IMQQ = string.Empty, AliWW = string.Empty, Website = string.Empty;
if (_Table.Tables[0].Rows.Count > 0)
{
for (int i = 0; i != _Table.Tables[0].Rows.Count; i++)/
{
for (int j = 0; j < list.Length; j++)/判断每个列的数据,然后处理
{
string[] arr = list[j].ToString().Split("|".ToCharArray());
if (Request[list[j].ToString()] == "CompanyName")
{
CompanyName = CompanyName + _Table.Tables[0].Rows[i][arr[1].ToString()].ToString();
}
if (Request[list[j].ToString()] == "Person_in_charge")
{
Person_in_charge = Person_in_charge + _Table.Tables[0].Rows[i][arr[1].ToString()].ToString();
}
if (Request[list[j].ToString()] == "Mobile")
{
Mobile = Mobile + _Table.Tables[0].Rows[i][arr[1].ToString()].ToString();
}
if (Request[list[j].ToString()] == "Phone")
{
Phone = Phone + _Table.Tables[0].Rows[i][arr[1].ToString()].ToString();
}
if (Request[list[j].ToString()] == "Fax")
{
Fax = Fax + _Table.Tables[0].Rows[i][arr[1].ToString()].ToString();
}
if (Request[list[j].ToString()] == "E-mail")
{
E_mail = E_mail + _Table.Tables[0].Rows[i][arr[1].ToString()].ToString();
}
if (Request[list[j].ToString()] == "Address")
{
Address = Address + _Table.Tables[0].Rows[i][arr[1].ToString()].ToString();
}
if (Request[list[j].ToString()] == "IMQQ")
{
IMQQ = IMQQ + _Table.Tables[0].Rows[i][arr[1].ToString()].ToString();
}
if (Request[list[j].ToString()] == "AliWW")
{
AliWW = AliWW + _Table.Tables[0].Rows[i][arr[1].ToString()].ToString();
}
if (Request[list[j].ToString()] == "Website")
{
Website = Website + _Table.Tables[0].Rows[i][arr[1].ToString()].ToString();
}
}
db.Update_Sql("INSERT INTO Customers([sid],[s_dep],[CompanyName],[Person_in_charge],[Mobile],[Phone],[Fax],[E-mail],[Address],[IMQQ],[AliWW],[Website],[Industry])VALUES('" + Request.Cookies["sa"].Value + "'," + Request.Cookies["dep"].Value + ",'" + CompanyName + "','" + Person_in_charge + "','" + Mobile + "','" + Phone + "','" + Fax + "','" + E_mail + "','" + Address + "','" + IMQQ + "','" + AliWW + "','" + Website + "'," + this.DropDownList2.SelectedValue + ")");
CompanyName = ""; Person_in_charge = ""; Mobile = ""; Phone = ""; Fax = ""; E_mail = ""; Address = ""; IMQQ = ""; AliWW = "";
this.Label3.Text = "导入成功!";
二、asp网页中如何导入excel
两种方法
1.拿execl当做数据库来读取
2.可以把excel另存为网页文件,就可以当做文本文件处理了,里面代码可以找到处理规律。
附上处理excel文档的asp代码(就是第一条的方法)
set conn2=CreateObject("ADODB.Connection")
conn2.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended properties=Excel 5.0;Data Source="&file_path&""
sql="select*FROM [student$]"
set rs=conn2.execute(sql)
do while not rs.eof
sql="insert into student([student_name],[student_nick],[student_password])values('"&fixsql(rs(0))&"','"&fixsql(rs(3))&"','"&fixsql(rs(8))&"')"
conn.execute(sql)
rs.movenext
loop
conn2.close
三、aspnet如何导入excel
用gridview
protected void Button1_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "学生成绩报表.xls");
}
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
//如果没有下面方法会报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内
public override void VerifyRenderingInServerForm(Control control)
{
}
还有由于是文件操作所以要引入名称空间IO和Text
后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Text;
public partial class Default7 : System.Web.UI.Page
{
SqlConnection sqlcon;
SqlCommand sqlcom;
string strCon = "Data Source=(local);Database=北ds;Uid=sa;Pwd=sa";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
public void bind()
{
string sqlstr = "select top 5 * from table1";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "table1");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "身份证号码" };
GridView1.DataBind();
sqlcon.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "学生成绩报表.xls");
}
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
}
前台:
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" >
读取Excel数据的代码:这个很简单的
private DataSet CreateDataSource()
{
string strCon;
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("excel.xls") + "; Extended Properties=Excel 8.0;";
OleDbConnection olecon = new OleDbConnection(strCon);
OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon);
DataSet myds = new DataSet();
myda.Fill(myds);
return myds;
}
protected void Button1_Click(object sender, EventArgs e)
{
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}