加入收藏 设为首页 帮助中心
 
论坛首页
用户登录 | 用户注册 | 最新悬赏 | 最新贴子 | 会员中心 | 贴子搜索 | 网站地图 | 帮助中心 | 联系我们
站内搜索:
现在位置 > 维库电子开发网论坛 > 资料专区 > Excel只能本机导入?
帖子主题: Excel只能本机导入?
你还没有登录,无法发表回复,请首先 登录.. [注 册]
积分:732 海边
发表于:2007-12-20 11:59:00 楼主
级别:五星
积分:732分
注册:2007年11月21日
string   fileName   =   this.FileUpload1.PostedFile.FileName.ToString();//得到上传文件的物理路径
                string   oleDBConnString   =   String.Empty;
                oleDBConnString   =   "Provider=Microsoft.Jet.OLEDB.4.0;";
                oleDBConnString   +=   "Data   Source=";
                oleDBConnString   +=   fileName;
                oleDBConnString   +=   ";Extended   Properties=Excel   8.0;";
                OleDbConnection   oleDBConn   =   new   OleDbConnection(oleDBConnString);
                oleDBConn.Open();
                DataTable   m_tableName   =   new   DataTable();
                m_tableName   =   oleDBConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,   null);
                string   sqlMaster   =   "   SELECT   *     FROM   [Sheet1$]";//指定某列开始读取:Sheet1$B1:B5
                OleDbDataAdapter   oleAdMaster   =   new   OleDbDataAdapter(sqlMaster,   oleDBConn);
                        DataSet   ds   =   new   DataSet();
                        oleAdMaster.Fill(ds,   "m_tableName");
                        //以上是把EXCEL读出来
                        for   (int   i   =   0;   i   <   ds.Tables[0].Rows.Count;   i++)
                        {
                                Student   st=   new   Student();
                                st.AddNew();  
                                st.SName   =   ds.Tables[0].Rows[i][1].ToString();
                                st.Save();                 //此处是自己写的方法,就是向Student表插入数据,
                        }

-----------------------------------------------------------------------------
以上代码是正确的,并且本地可以插入到Student表中去,可其他非本机的局域网用户却无法导入数据,不知道是都哪里设置的问题?   请赐教,谢谢。
 
积分:746 JJLK
发表于:2007-12-20 12:00:00 1 楼
级别:五星
积分:746分
注册:2007年11月15日
1.检查文件路径是否正确.
2.检查权限问题
 
积分:510 peng云
发表于:2007-12-20 12:01:00 2 楼
级别:五星
积分:510分
注册:2007年11月14日
你必需上传到服务器.

在本例中,没有上传到服务器中.

你在本机.文件本身就在服务器上.(因为你本机就是服务器)

1)上传到服务器
2)导入数据到DB
3)删除临时文件(可选)
 
积分:732 海边
发表于:2007-12-20 12:02:00 3 楼
级别:五星
积分:732分
注册:2007年11月21日
现在看应该是这样的问题了

可否搞个大概的例子的参考下呢

手头事太多......   谢谢
 
积分:710 bbs
发表于:2007-12-20 12:03:00 4 楼
级别:五星
积分:710分
注册:2007年11月12日
是这个意思  
就相当于普通用户把自己的EXCEL表导入到我本地的Student表  
请问如何解决?  
lispo:文件路径本地是可以的;权限怎么搞?请明示。

-----------------------------
我以为是他已经把文件传入服务器上面了.在服务器不能读写应该是权限问题(能传上去就有文件写的权限了,是不是数据库权限就不晓得了).判断错误,不好意思哈.

其实一般的做法是把excel上传到服务器上导入数据的.同时建议LZ采用预缆的方式.
 
积分:732 海边
发表于:2007-12-20 12:04:00 5 楼
级别:五星
积分:732分
注册:2007年11月21日
谢谢bbs

数据库权限改了   依然不行

应该要先传上去了  
有没什么代码   参考下啊   谢谢
 
积分:480 春风
发表于:2007-12-20 12:05:00 6 楼
级别:四星
积分:480分
注册:2007年11月14日
这个应该有用了,太多了,都不记得是哪个部分的代码了
<DL class=code> <DT>HTML code <DD>
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> &lt;%@ Page Language='VB' AutoEventWireup='false' CodeFile='addexcel.aspx.vb' Inherits='addexcel' %&gt; &lt;!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'&gt; &lt;html xmlns='http://www.w3.org/1999/xhtml' &gt; &lt;head runat='server'&gt; &lt;title&gt;ECXEL文件上傳&lt;/title&gt; &lt;link href='tab.CSS' rel='stylesheet' type='text/css' /&gt; &lt;link href='datagrid.css' rel='stylesheet' type='text/css' /&gt; &lt;/head&gt; &lt;body style='text-align: center'&gt; &lt;form id='form1' runat='server'&gt; &lt;asp:Panel ID='Panel1' runat='server' Height='94px' Width='587px'&gt; &lt;asp:Label ID='Label1' runat='server' Font-Bold='True' Text='临时工EXCEL表单上传' Width='180px'&gt;&lt;/asp:Label&gt;&lt;br /&gt; &lt;br /&gt; &lt;asp:Label ID='Label2' runat='server' ForeColor='#000033' Text='请选择上传文件'&gt;&lt;/asp:Label&gt;&lt;asp:FileUpload ID='FileUpload1' runat='server' /&gt;&lt;br /&gt; &lt;asp:Button ID='Button1' runat='server' Text='上传' Width='45px' /&gt; &lt;/asp:Panel&gt; &lt;asp:Panel ID='Panel2' runat='server' Height='146px' Visible='False' Width='100%'&gt; &lt;asp:GridView ID='GridView1' runat='server' BackColor='White' BorderColor='#CCCCCC' BorderStyle='None' BorderWidth='1px' CellPadding='3'&gt; &lt;FooterStyle BackColor='White' ForeColor='#000066' /&gt; &lt;RowStyle ForeColor='#000066' /&gt; &lt;SelectedRowStyle BackColor='#669999' Font-Bold='True' ForeColor='White' /&gt; &lt;PagerStyle BackColor='White' ForeColor='#000066' HorizontalAlign='Left' /&gt; &lt;HeaderStyle BackColor='#006699' Font-Bold='True' ForeColor='White' /&gt; &lt;/asp:GridView&gt; &lt;br /&gt; &lt;asp:Button ID='Button2' runat='server' Text='导入并统计临时工数据' Font-Bold='True' Width='159px' /&gt; &lt;/asp:Panel&gt; &lt;asp:Panel ID='Panel3' runat='server' Height='146px' Visible='False' Width='100%'&gt; &lt;strong&gt;&lt;span style='font-size: 14pt; color: #000099'&gt;短期工所在部门信息统计报表&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt; &lt;asp:GridView ID='GridView2' runat='server' AutoGenerateColumns='False' BackColor='White' BorderColor='#CCCCCC' BorderStyle='None' BorderWidth='1px' CellPadding='3' DataKeyNames='ID' DataSourceID='AccessDataSource1'&gt; &lt;FooterStyle BackColor='White' ForeColor='#000066' /&gt; &lt;Columns&gt; &lt;asp:BoundField DataField='ID' HeaderText='系统编号' InsertVisible='False' ReadOnly='True' SortExpression='ID' /&gt; &lt;asp:BoundField DataField='chu' HeaderText='处级' SortExpression='chu' /&gt; &lt;asp:BoundField DataField='bu' HeaderText='部级' SortExpression='bu' /&gt; &lt;asp:BoundField DataField='ke' HeaderText='课级' SortExpression='ke' /&gt; &lt;asp:BoundField DataField='zu' HeaderText='组级' SortExpression='zu' /&gt; &lt;asp:BoundField DataField='iszuke' HeaderText='显示组' SortExpression='iszuke' /&gt; &lt;asp:BoundField DataField='shortna' HeaderText='短期工(男)' SortExpression='shortna' /&gt; &lt;asp:BoundField DataField='shortnv' HeaderText='短期工(女)' SortExpression='shortnv' /&gt; &lt;asp:BoundField DataField='szshortnan' HeaderText='上周短期工(男)' SortExpression='szshortnan' /&gt; &lt;asp:BoundField DataField='szshortnv' HeaderText='上周短期工(女)' SortExpression='szshortnv' /&gt; &lt;asp:BoundField DataField='last_update_time' HeaderText='更新日期' HtmlEncode='False' SortExpression='last_update_time' /&gt; &lt;/Columns&gt; &lt;RowStyle ForeColor='#000066' /&gt; &lt;SelectedRowStyle BackColor='#669999' Font-Bold='True' ForeColor='White' /&gt; &lt;PagerStyle BackColor='White' ForeColor='#000066' HorizontalAlign='Left' /&gt; &lt;HeaderStyle BackColor='#006699' Font-Bold='True' ForeColor='White' /&gt; &lt;/asp:GridView&gt; &lt;asp:Button ID='Button3' runat='server' Text='确认统计' /&gt;&lt;asp:AccessDataSource ID='AccessDataSource1' runat='server' DataFile='~/App_Data/renzi.mdb' SelectCommand='SELECT * FROM [depinfo]'&gt;&lt;/asp:AccessDataSource&gt; &lt;span style='font-size: 10pt'&gt;如果发现与实际不相符的部分,请检查统计资料是否完整,红色表示与上周人数不同&lt;/span&gt;&lt;/asp:Panel&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;
</DD></DL>
 
积分:340 卷尺
发表于:2007-12-20 12:06:00 7 楼
级别:四星
积分:340分
注册:2007年11月14日
<DT>VB.NET code <DD>
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> Imports System.Data Imports System.Data.SqlClient Partial Class addexcel Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click If FileUpload1.PostedFile.ContentLength = 0 Then Label1.Text = '&lt;FONT COLOR=RED&gt;上传失败或文件不存在!&lt;/FONT&gt;' ElseIf Right(FileUpload1.PostedFile.FileName, 4) &lt;&gt; '.xls' Then Label1.Text = '&lt;FONT COLOR=RED&gt;文件格式错误!&lt;/FONT&gt;' Else ' 获取文件名() Dim Temp() As String = Split(FileUpload1.PostedFile.FileName, '\') Dim FileName As String = Temp(Temp.Length - 1) '保存文件 FileUpload1.PostedFile.SaveAs(Server.MapPath('') &amp; '/excel/lishi.xls') '显示上传结果 Label1.Text = '文件上传成功!&lt;br&gt;上传文件:' &amp; FileName End If Dim sql As String = 'select * from [Sheet1$]' Dim conn As String = ' Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =' + Server.MapPath('') + '/excel/lishi.xls;Extended Properties=Excel 8.0' Dim thisconnection As OleDb.OleDbConnection = New OleDb.OleDbConnection(conn) thisconnection.Open() Dim mycommand As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sql, thisconnection) Dim ds As New DataSet mycommand.Fill(ds, '[Sheet1$]') ds.Tables('[Sheet1$]').Columns.RemoveAt(12) ds.Tables('[Sheet1$]').Columns.RemoveAt(11) ds.Tables('[Sheet1$]').Columns.RemoveAt(10) ds.Tables('[Sheet1$]').Columns.RemoveAt(9) ds.Tables('[Sheet1$]').Columns.RemoveAt(8) ds.Tables('[Sheet1$]').Columns.RemoveAt(7) ds.Tables('[Sheet1$]').Columns.RemoveAt(6) ds.Tables('[Sheet1$]').Columns.RemoveAt(5) ds.Tables('[Sheet1$]').Columns.RemoveAt(4) ds.Tables('[Sheet1$]').Columns.RemoveAt(2) ds.Tables('[Sheet1$]').Columns.Add('部级核对') ds.Tables('[Sheet1$]').Columns.Add('课级核对') ds.Tables('[Sheet1$]').Columns.Add('组级核对') thisconnection.Close() GridView1.DataSource = ds GridView1.DataBind() Panel1.Visible = False Panel2.Visible = True End Sub Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound Dim kk As New renzibll If e.Row.RowIndex &gt; -1 Then If e.Row.Cells(3).Text &lt;&gt; '' Then e.Row.Cells(6).Text = kk.Getbu(e.Row.Cells(3).Text) End If If e.Row.Cells(4).Text &lt;&gt; '' Then e.Row.Cells(7).Text = kk.GetKe(Replace(e.Row.Cells(4).Text, '', '')) End If If e.Row.Cells(5).Text &lt;&gt; '' Then e.Row.Cells(8).Text = kk.GetZu(e.Row.Cells(5).Text) End If If e.Row.Cells(6).Text = '' Or e.Row.Cells(7).Text = '' Or e.Row.Cells(8).Text = '' Then Button2.Enabled = False Button2.Text = '请修正核对部门' End If End If End Sub Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click Dim sql As String = 'select * from [Sheet1$]' Dim conn As String = ' Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =' + Server.MapPath('') + '/excel/lishi.xls;Extended Properties=Excel 8.0' Dim thisconnection As OleDb.OleDbConnection = New OleDb.OleDbConnection(conn) thisconnection.Open() Dim mycommand As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sql, thisconnection) Dim ds As New DataSet mycommand.Fill(ds, '[Sheet1$]') thisconnection.Close() Dim i = 0, rowcount As Integer = ds.Tables('[Sheet1$]').Rows.Count Dim tt As New renzibll Try tt.ini() For i = 0 To rowcount - 1 If GridView1.Rows(i).Cells(8).Text &lt;&gt; '' Then tt.zu_add(GridView1.Rows(i).Cells(8).Text, GridView1.Rows(i).Cells(3).Text) End If Next Catch ex As Exception Response.Write(ex) End Try Dim ll As New linshi ll.IsUpdate() Panel2.Visible = False Panel3.Visible = True End Sub Protected Sub GridView2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2.RowDataBound If e.Row.RowIndex &gt; -1 Then e.Row.Cells(10).Text = Format(Convert.ToDateTime(e.Row.Cells(10).Text), 'yyyy年MM月dd日') If e.Row.Cells(5).Text = 1 Then e.Row.Cells(5).Text = '' Else