excel库的使用问题

来源:互联网 发布:中学生编程大赛 编辑:程序博客网 时间:2024/06/05 17:10

System.UnauthorizedAccessException: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 拒绝访问。 (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

using Microsoft.Office.Interop.Excel;程序使用了这一程序集, 结果程序调试的时候没有问题, 但是放到iis上出了上述问题.

解决方法:

开始-》运行:dcomcnfg -> 选择:micrsoft Excel 应用程序 -》属性-> 安全性-》使用自定义用户权限--> 编辑--》添加用户--》everyone -> 确定, 


参考博客:

http://www.cnblogs.com/Mr_JinRui/archive/2011/08/26/2153952.html


using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Excel;
using XianHe.DAL;
using XianHe.Model;


namespace XianHe.BLL
{
    public class StyleBLL
    {
        Microsoft.Office.Interop.Excel.Application m_objExcel = null;
        private Workbooks m_objBooks = null;
        private _Workbook m_objBook = null;
        private Sheets m_objSheets = null;
        private _Worksheet m_objSheet = null;
        private object m_objOpt = System.Reflection.Missing.Value;
        public void InitExcel()
        {
            m_objExcel = new Microsoft.Office.Interop.Excel.Application();
            m_objExcel.Workbooks.Add(m_objOpt);
            m_objBooks = (Workbooks)m_objExcel.Workbooks;


            m_objBook = (_Workbook)(m_objBooks.Add(m_objOpt));
            m_objSheets = (Sheets)m_objBook.Worksheets;
            m_objSheet = (_Worksheet)(m_objSheets.get_Item(1));
            m_objExcel.Visible = false;
        }
        /// <summary>
        /// 导入单款数据
        /// </summary>
        /// <param name="filename"></param>
        public void CLImport(string filename)
        {
            try
            {
                m_objExcel = new Microsoft.Office.Interop.Excel.Application();
                m_objBook = m_objExcel.Workbooks.Open(filename);
                m_objSheets = m_objBook.Worksheets;
                m_objSheet = (_Worksheet)m_objSheets.get_Item(1);
                int rowcount = m_objSheet.UsedRange.Rows.Count;
                if (rowcount > 0)
                {
                    StyleDAL sdal = new StyleDAL();
                    CostumeDAL cdal = new CostumeDAL();
                    for (int i = 2; i <= rowcount; i++)
                    {
                        Range style = (Range)m_objSheet.Cells[i, 1];
                        Range price = (Range)m_objSheet.Cells[i, 2];
                        Range colors = (Range)m_objSheet.Cells[i, 3];
                        Range theme = (Range)m_objSheet.Cells[i, 4];
                        Range desc = (Range)m_objSheet.Cells[i, 5];
                        Range material = (Range)m_objSheet.Cells[i, 6];
                        Range maintain = (Range)m_objSheet.Cells[i, 7];
                        string strcolors = Convert.ToString(colors.Value);
                        string[] arrcolors = strcolors.Split(' ');
                        string strstyle = Convert.ToString(style.Value);
                        //插入数据到款式表
                        StyleInfo info = new StyleInfo();
                        info.BrandNo = strstyle[0].ToString();
                        info.YearNo = strstyle.Substring(1,2);
                        info.CategoryNo = strstyle[3].ToString();
                        info.SeasonNo = strstyle[4].ToString();
                        info.FlowNo = strstyle.Substring(4, 3);
                        info.ColorsNo = Convert.ToString(colors.Value);
                        info.Price = Convert.ToDecimal(price.Value);
                        info.StyleNo = Convert.ToString(style.Value);
                        info.RecommandInfo = Convert.ToString(desc.Value);
                        info.ThemeID = Convert.ToString(theme.Value);
                        info.Material = Convert.ToString(material.Value);
                        info.Maintain = Convert.ToString(maintain.Value);


                        int styleid = sdal.Insert(info);
                        if (styleid < 0) throw new Exception("单款数据导入错误, 数据导入到style表。");
                        //初始化单品表
                        foreach (string item in arrcolors)
                        {
                            if (String.IsNullOrEmpty(item)) continue;
                            //if (cdal.GetByStyleID_ColorNo(styleid,item) != null) continue;
                            CostumeInfo cinfo = new CostumeInfo();
                            cinfo.ColorNo = item;
                            cinfo.StyleID = styleid;
                            cdal.Insert(cinfo);
                        }
                    }
                    m_objBook.Close();


                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message+"\n"+filename);
                m_objBook.Close();


            }
        }


    }
}

原创粉丝点击