62,243
社区成员




public static string getFirsttableName(string excelFilepath)
{
string tableName = null;
if (File.Exists(excelFilepath))
{
using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet." +
"OLEDB.4.0;Extended Properties=\"Excel 8.0\";Data Source=" + excelFilepath))
{
conn.Open();
DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
tableName = dt.Rows[0][2].ToString().Trim();
}
}
return tableName;
}
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataSet ds = new DataSet();
OleDbDataAdapter odda = new OleDbDataAdapter("select * from [score$]", conn);
odda.Fill(ds, table);
return ds;
if (dr[i]["AA"].ToString() != null && dr[i]["AA"].ToString() != "")
{
cs.AA = Double.Parse(dr[i]["AA"].ToString());
}
public void ReadExcel(string sExcelFile)
{
string sConnectionString = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + sExcelFile + ";Extended Properties=Excel 8.0";
OleDbConnection connection = new OleDbConnection(sConnectionString);
OleDbDataAdapter adp = new OleDbDataAdapter("SELECT * FROM score", connection);
DataSet ds = new DataSet();
adp.Fill(ds);
connection.Close();
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExcelToSQL.aspx.cs" Inherits="Questionnaire_ExcelToSQL" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Btn_Input" runat="server" Text="导入Sql"
onclick="Btn_Input_Click" />
</div>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;
using System.Data.OleDb;
using System.Data.SqlClient;
using Model;
using DBUtility;
public partial class Questionnaire_ExcelToSQL : System.Web.UI.Page
{
Answer_ClientInfo a_client = new Answer_ClientInfo();
protected void Page_Load(object sender, EventArgs e)
{
}
//导入Sql
protected void Btn_Input_Click(object sender, EventArgs e)
{
int res = 0;
if (FileUpload1.HasFile == false)//HasFile用来检查FileUpload是否有指定文件
{
Response.Write("<script>alert('请您选择Excel文件')</script> ");
return;//当无文件时,返回
}
string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名
if (IsXls != ".xls")
{
Response.Write("<script>alert('只可以选择Excel文件')</script>");
return;//当选择的不是Excel文件时,返回
}
SqlConnection cn = new SqlConnection(SqlHelper.ConnectionString);
cn.Open();
string filename = DateTime.Now.ToString("yyyymmddhhMMss") + FileUpload1.FileName; //获取Execle文件名 DateTime日期函数
string savePath = Server.MapPath(("~\\upfiles\\") + filename);//Server.MapPath 获得虚拟服务器相对路径
FileUpload1.SaveAs(savePath); //SaveAs 将上传的文件内容保存在服务器上
DataSet ds = ExecleDs(savePath, filename); //调用自定义方法
DataRow[] dr = ds.Tables[0].Select(); //定义一个DataRow数组
int rowsnum = ds.Tables[0].Rows.Count;
if (rowsnum == 0)
{
Response.Write("<script>alert('Excel表为空表,无数据!')</script>"); //当Excel表为空时,对用户进行提示
}
else
{
for (int i = 0; i < dr.Length-1; i++)
{
a_client.Client_name = dr[i]["客户名称"].ToString();
a_client.Client_company = dr[i]["所属单位"].ToString();
a_client.Client_phone = dr[i]["个人客户手机"].ToString();
a_client.Client_email = dr[i]["电子邮件"].ToString();
a_client.Client_address = dr[i]["地址"].ToString();
a_client.Market_id = DAL.MarketDAL.SelIdByMarketName(dr[i]["细分市场"].ToString());
//string sqlcheck = "select count(*) from [answer_client] where hhaspx_rq='" + hhaspx_rq + "'And hhaspx_xm='" + hhaspx_xm + "'"; //检查用户是否存在
res = DAL.Answer_ClientDAL.AddClient(a_client);
}
}
}
/// <summary>
/// 查询EXCEL电子表格添加到DATASET
/// </summary>
/// <param name="filenameurl">服务器路径</param>
/// <param name="table">表名</param>
///
public DataSet ExecleDs(string filenameurl, string table)
{
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataSet ds = new DataSet();
OleDbDataAdapter odda = new OleDbDataAdapter("select * from [2010birtv客户$]", conn);
odda.Fill(ds, table);
return ds;
}
}
using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet." +
"OLEDB.4.0;Extended Properties=\"Excel 8.0\";Data Source=" + excelFilepath))
{
conn.Open();
string strCom = " SELECT " + columnName + " FROM [" + tableName + "]";///SQL操作语句,就是说:取得所有数据从Content
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, connExcel);
DataSet myDataSet = new DataSet();///建立新的数据集myDataSet
myCommand.Fill(myDataSet);///填充数据集
}
SELECT * into t1
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="d:\a.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...sheet1$