jehovahx 2017-04-05 16:25 采纳率: 0%
浏览 4540

C#读excel出现异常 (异常来自 HRESULT:0x80010105 (RPC_E_SERVERFAULT))

C#操作excel出错:(异常来自 HRESULT:0x80010105 (RPC_E_SERVERFAULT))A03EC

 private void OpenExcel(string strFileName)
        {
            object missing = System.Reflection.Missing.Value;
            Application excel = new Application();

            if (excel == null)
            {
                Response.Write("<script>alert('Can't access excel')</script>");
            }
            else
            {
                excel.Visible = false;
                excel.UserControl = true;
                // 以只读的形式打开EXCEL文件
                    Workbook wb = excel.Application.Workbooks.Open(strFileName, missing, true, missing, missing, missing,
                     missing, missing, missing, true, missing, missing, missing, missing, missing);


                //取得第一个工作薄
                Worksheet ws = (Worksheet)wb.Worksheets.get_Item(1);
                //取得总记录行数   (包括标题列)
                int rowsint = ws.UsedRange.Cells.Rows.Count; //得到行数
                //int columnsint = mySheet.UsedRange.Cells.Columns.Count;//得到列数
                //取得数据范围区域 (不包括标题列)
                Range rng1 = ws.Cells.get_Range("A1", "A" + rowsint);   //item
                Range rng2 = ws.Cells.get_Range("B1", "B" + rowsint); //Customer
                object[,] arryItem = (object[,])rng1.Value2;   //get range's value
                object[,] arryCus = (object[,])rng2.Value2;
                //将新值赋给一个数组
                string[] arryA = new string[rowsint];
                string[] arryB = new string[rowsint];
                for (int i = 1; i <= rowsint; i++)
                {
                    //Item_Code列
                    arryA[i - 1] = arryItem[i, 1].ToString();
                    //Customer_Name列
                    arryB[i - 1] = arryCus[i, 1].ToString();
                }
                //Response.Write(arry[0, 0] + " / " + arry[0, 1] + "#" + arry[rowsint - 2, 0] + " / " + arry[rowsint - 2, 1]);

            }
            excel.Quit(); excel = null;
            Process[] procs = Process.GetProcessesByName("excel");
            foreach (Process pro in procs)
            {
                pro.Kill();//没有更好的方法,只有杀掉进程
            }
            GC.Collect();
        }

当 excel,visible = true 时可以正常运行,求解决。每次弹出来一个excel表接受不了呀=。=

  • 写回答

3条回答 默认 最新

  • ????-baby 2017-04-05 16:52
    关注

    如何读取?封装一些常用的方法

    评论

报告相同问题?

悬赏问题

  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题