乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > C#<em>asp.net</em> 将客户端的<em>Excel</em&g

C#<em>asp.net</em> 将客户端的<em>Excel</em&g

作者:乔山办公网日期:

返回目录:excel表格制作


比较简单,这个要zhidao用到微软的分布式查询,可以参考我之前写的: 《Excel导入SQL SERVER》 http://hi.baidu.com/44498/blog/item/404e364307380c1a72f05d3d.html 但是你要注意,先上传到服务器之后才能执行导入操作。

#region 连接Excel 读取Excel数据 并返回DataSet数据集合
/// <summary>
/// 连接Excel 读取Excel数据 并返回DataSet数据集合
/// </summary>
/// <param name="filepath">Excel服务器路径</param>
/// <param name="tableName">Excel表名称</param>
/// <returns></returns>
public static System.Data.DataSet ExcelSqlConnection(string filepath, string tableName)
{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
OleDbConnection ExcelConn = new OleDbConnection(strCon);
try
{
string strCom = string.Format("SELECT * FROM [Sheet1$]");
ExcelConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, ExcelConn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "[" + tableName + "$]");
ExcelConn.Close();
return ds;
}
catch
{

ExcelConn.Close();
return null;
}
}
#endregion

#region 导入的execl
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection cn = new BSqlDataProvider().GetSqlConnection();
cn.Open();
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文件时,返回
}
string filename = FileUpload1.FileName; //获取Execle文件名 DateTime日期函数
string savePath = Server.MapPath(("~\\upfiles\\") + filename);//Server.MapPath 获得虚拟服务器相对路径
FileUpload1.SaveAs(savePath); //SaveAs 将上传的文件内容保存在服务器上
DataSet ds = ExcelSqlConnection(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; i++)
{
string spdm = dr[i]["商品代码e799bee5baa6e997aee7ad94e78988e69d83337"].ToString();//日期 excel列名【名称不能变,否则就会出错】
string jijie = dr[i]["季节"].ToString();
string boduan = dr[i]["波段"].ToString();
string s_chan = dr[i]["生产商"].ToString();
string f_shi = dr[i]["方式"].ToString();
string c_ku = dr[i]["仓库"].ToString();
string insertstr = "insert into AA_ANSD values('"+spdm+"','"+jijie+"','"+boduan+"','"+s_chan+"','"+f_shi+"','"+c_ku+"')";
SqlCommand cmd = new SqlCommand(insertstr, cn);
try
{
cmd.ExecuteNonQuery();
}
catch (MembershipCreateUserException ex) //捕捉异常
{
Response.Write("<script>alert('导入内容:" + ex.Message + "')</script>");
}

}
Response.Write("<script>alert('Excle表导入成功!');location='CMT_Entry.aspx?CMD=0'</script>");
}

cn.Close();

}
#endregion

快给分 啊
/// <summary>
        /// 读Excel文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns>DataTable</returns>
        public static DataTable readCDEDataFile(string filePath,string condition)
        {
            DataTable dt = new DataTable();
            object missing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();//lauch excel application
            try
            {
                Microsoft.Office.Interop.Excel.Workbook xlsBook = excel.Workbooks.Open(filePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                if (excel == null)
                {
                    //Response.Write("<script>alert('Can't access excel')</script>");
                }
                else
                {
                    Microsoft.Office.Interop.Excel.Worksheet wsheet = (Microsoft.Office.Interop.Excel.Worksheet)xlsBook.Worksheets.get_Item(1);
                    string name = wsheet.Name;
                    xlsBook.Close(false, Type.Missing, Type.Missing);
                    excel.Quit();
                    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filePath + ";" + "Extended Properties=Excel 8.0;";
                    OleDbConnection oledbConn = new OleDbConnection(strConn);
                    oledbConn.Open();
                    string strExcel = "select " + condition + " from [" + name + "$]";
                    OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, strConn);
                    DataSet ds = new DataSet();
                    myCommand.Fill(dt);
                    oledbConn.Close();
                }
            }
            catch (Exception e)
            {
                Console.Write(e.Message.ToString());
                //SaveNote(e.ToString());
            }
            finally
            {
                if (excel != null) excel.Quit();
                GC.Collect();
            }
            if (excel != null) excel.Quit();
            return dt;
        }

 取得后 循环插e79fa5e98193e59b9ee7ad94364入就可以了



public static DataSet CreateDataSource()
{
OpenFileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() != DialogResult.OK)
return null;
string filename = dialog.FileName;
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + filename +
";Extended Properties=Excel 8.0;";
try
{
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [数据$]", strConn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet);
return myDataSet;
}
catch (Exception ex )
{
MessageBox.Show("打开文件时出错:" + ex.Message);
return null;
}

}

//sql 语句说明 "SELECT * FROM [数据$]",其中 “数据”表示excel中sheet的名称,
如:excel文件中有个叫"Sheet1"的标签页,SQL语句为 SELECT * FROM [Sheet1$]",excel的列会自e799bee5baa6e997aee7ad94e59b9ee7ad94339动转化成DataTable的列,支持中文,会自动判断数据行。
每次只能打开一个sheet。
有了上面的数据,你再foreach 插入到SQL中去

相关阅读

  • C#<em>asp.net</em> 将客户端的<em>Excel</em&g

  • 乔山办公网excel表格制作
  • 比较简单,这个要zhidao用到微软的分布式查询,可以参考我之回前写的: 《Excel导入SQL SERVER》 http://hi.baidu.com/44498/blog/item/404e364307380c1a72f05d3d.html 但是你要注意,先上传到服务器之后才能
  • -asp.net excel导入sql,excel导入到sql

  • 乔山办公网excel表格制作
  • 不管你是那e799bee5baa6e79fa5e98193e58685e5aeb9364个数据库,将Excel里面的数据导入到数据库中的原理就是将Excel里面的数据存储到一个dataTable中,然后将数据一行一行添加到数据库的表里。Sql
  • <em>Delphi</em>中如何将<em>excel</em>中的数

  • 乔山办公网excel表格制作
  • a和b的字段类型是不是有Double类型?请检查Excel中是不是出现了除数字外的其他字符所以导致你插入数据库时会出现错误提示(string类型不能转换成double类型)delphi中怎么实现EXCEL导入导出到
关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网