0

I will try to read excel from ASP.Net web server

The first error occurred. Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005.

So I gave permission to IUSR, Network Service, and Users in DCOMCNFG-> Microsoft Excel Application.

And the second error occurred. Exception Message: HRESULT: 0x800A03EC. I know that this error occurs when the Excel file contents are wrong. So I tried an empty Excel file, but the same error occurred.

Could you give me advice?

    public HttpResponseMessage ExcelRead()
    {
        ExcelHelper helper = new ExcelHelper();

        String FolderName = HttpContext.Current.Server.MapPath("~/file");
        var FullFileNameList = new List<string>();
        System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(FolderName);
        if (di == null)
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }
        foreach (System.IO.FileInfo File in di.GetFiles())
        {
            if (File.Extension.ToLower().CompareTo(".xlsx") == 0)
            {
                FullFileNameList.Add(File.FullName);
            }
        }
        if (FullFileNameList.Count <= 0)
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }

        helper.ReadExcelData(FullFileNameList[0]);

        if (helper.data == null)
            return Request.CreateResponse(HttpStatusCode.BadRequest);


        for (int i = 0; i < FullFileNameList.Count; ++i)
        {
            System.IO.FileInfo fi = new System.IO.FileInfo(FullFileNameList[i]);
            try
            {
                fi.Delete();
            }
            catch (System.IO.IOException e)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, e);
            }
        }

        List<Member> memberList = new List<Member>();
        for (int r = 3; r <= helper.data.GetLength(0); r++)
        {
            if (helper.data[r, 1] == null)
            {
                continue;
            }

            Member member = new Member
            {
                Id = helper.data[r, 1].ToString(),
                Name = helper.data[r, 2].ToString(),
            };

            GenderType gender = 0;
            if (helper.data[r, 5] == null)
                gender = GenderType.Male;
            else
            {
                switch (helper.data[r, 5].ToString())
                {
                    case "남":
                    case "남자":
                    case "남성":
                        {
                            gender = GenderType.Male;
                            break;
                        }
                    case "여":
                    case "여자":
                    case "여성":
                        {
                            gender = GenderType.Female;
                            break;
                        }
                    default:
                        {
                            gender = GenderType.Male;
                            break;
                        }
                }
            }

            member.Major = (helper.data[r, 3] == null) ? null : helper.data[r, 3].ToString();
            member.Professor = (helper.data[r, 4] == null) ? null : helper.data[r, 4].ToString();
            member.Gender = gender;
            member.AvatarBody = (short)gender;
            member.AvatarHead = (short)gender;

            memberList.Add(member);
        }


    }
banana
  • 37
  • 8
  • does the filename have the correct extension for whichever method you're using? (there are several) – ashleedawg Aug 14 '18 at 13:24
  • Where is your code and a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve)? Without it helping will be difficult. – VDWWD Aug 14 '18 at 13:26
  • I have just come home and have no code to show. I can show you 10 hours later. I'm sorry. – banana Aug 14 '18 at 14:22

1 Answers1

0

I tried to read the Excel file using Microsoft.Office.Interop.Excel. But I knew now. The server does not use interop. So I used openxml. If you are experiencing the same problem as me, check out the link below. https://social.technet.microsoft.com/wiki/contents/articles/35010.read-excel-files-using-open-xml-sdk-in-asp-net-c.aspx open xml reading from excel file

banana
  • 37
  • 8