string execelConnectionStr = "Provider=Microsoft.Jet.Oledb.4.0;data source=" + path + @";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";  
这段代码只能导入不带密码的excel,加了密码不行了,加了User ID=Admin;password=; 这样还是不行?快疯了,大家帮帮忙,谢谢了! 在这里发了第四份贴了,郁闷

解决方案 »

  1.   

    string execelConnectionStr = "Provider=Microsoft.Jet.Oledb.4.0;data source=" + path + @";uid=加密人的ID;psw=密码;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
    同数据库连接一样的把userid和密码写进去就可以了   
      

  2.   


                    string execelConnectionStr = "Provider=Microsoft.Jet.Oledb.4.0;data source=" + path + @";User ID=admin;password=ab;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";  
      

  3.   

    asp.net对应的用户要对excel应用程序拥有相对应的权限
      

  4.   

    那个你应该把EXCEL文件关闭了才运行,EXCEL文件打开的话就会出这个问题
      

  5.   

    楼上对的,已经打开的Excel通过C#程序是不能再打开的,要么你写一个判断,判断文件是否被打开
      

  6.   

    实在不行你就让C#先打开EXCEL文档解密然后另存为新的文件
    然后导入这个解密的文件
    删除另存的文件就好了
    反正用户看不见你后台怎么操作的
      

  7.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.Office.Interop.Excel;
    using System.Reflection;
    using System.Runtime.InteropServices;namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();            xlApp.DisplayAlerts = false;            //789就是密码
                xlApp.Workbooks.Open("D:\\123.xlsx", Missing.Value, Missing.Value, Missing.Value, "789", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                Workbook xlBook = xlApp.Workbooks[1];
                if (xlBook.HasPassword)
                {
                    xlBook.Unprotect(Missing.Value);
                }
                //第三个参数是密码 直接给个null 就是没有密码
                xlBook.SaveAs("D:\\没有密码.xlsx", Missing.Value,null, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);            xlApp.ActiveWorkbook.Close(false, Type.Missing, Type.Missing);            xlApp.Quit();            IntPtr t = new IntPtr(xlApp.Hwnd);
                int k = 0;
                GetWindowThreadProcessId(t, out k);
                System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
                p.Kill();
            }        [DllImport("User32.dll", CharSet = CharSet.Auto)]
            private static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
        }
    }