Read from excel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • VanZandt
    New Member
    • Oct 2006
    • 5

    Read from excel

    Hello,
    I am trying to read data from excel file and copy it to DB at run time for web. First of all Is it possible to do that? Second here is what I have so far and i'm gettting an access denied on file read.

    string StrFileType = "";
    string StrFileName = "";

    StrFileType = File.PostedFile .ContentType;
    StrFileName = File.PostedFile .FileName; //.Substring(File .PostedFile.Fil eName.LastIndex Of("\\") + 1) ;
    if (StrFileType.To String() == "applicatio n/vnd.ms-excel")
    {
    StreamReader logFileStream = new StreamReader(ne w FileStream(StrF ileName, FileMode.Open, FileAccess.Read , FileShare.ReadW rite));
    string test = logFileStream.R eadLine();
    }

    I get the following error:
    Access to the path "C:\Documen ts and Settings\xyz\De sktop\abc.xls" is denied.
  • mady1380
    New Member
    • Sep 2006
    • 18

    #2
    hi

    First please give Read and write rights for the Asp User in ur folder where ur project is in wwwroot. Then even dont forget to put the line <identity impersonate="tr ue" /> in your webconfig file.

    then try this code.

    Dim MyExcel As Microsoft.Offic e.Interop.Excel .Application
    Dim MyWorkbook As Microsoft.Offic e.Interop.Excel .Workbook
    Dim MySheet As Microsoft.Offic e.Interop.Excel .Worksheet

    MyExcel = New Microsoft.Offic e.Interop.Excel .Application

    MyWorkbook = MyExcel.Workboo ks.Open("C:\Ine tpub\wwwroot\Ad dNewPromotion\E xcel1.xls")
    MySheet = MyWorkbook.Work sheets(1)
    MySheet.Range(" C1").Value = "JIM"
    MySheet.Range(" B1").Value = "Harry"


    Label1.Text = MySheet.Range(" A1").Value
    MyWorkbook.Save ()
    MyWorkbook.Clos e()


    MyExcel = Nothing
    MyWorkbook = Nothing
    MySheet = Nothing

    i hope it works for u...cheers

    Comment

    • VanZandt
      New Member
      • Oct 2006
      • 5

      #3
      thanks mady1380,
      I'll certainly try that. Is there a possibility to just directly read the file from memory as the user selects the file to input and copy the contents of excel into DB instead of giving user read/write access to project folder?

      thanks
      Van

      Comment

      • Amit Singla
        New Member
        • Oct 2006
        • 2

        #4
        DataSet ds=null;
        try
        {
        if(File1.Value! ="")
        {
        string strFile=Convert .ToString(Syste m.IO.Path.GetFi leName(File1.Po stedFile.FileNa me));
        string[] file ;
        file = strFile.Split(' .');
        if(file[1].ToLower() =="xls")
        {
        if(!System.IO.F ile.Exists(Serv er.MapPath (@"../XLSFiles/"+strFile)) )
        {
        File1.PostedFil e.SaveAs(Server .MapPath(@"../XLSFiles/"+strFile)) ;
        ds=commonFuncti ons.readExcelFi le(Server.MapPa th(@"../XLSFiles/"+strFile)) ;
        m_objProgramSch edule= new ProgramSchedule ();
        foreach(DataRow dr in ds.Tables["tbl_new"].Rows )
        {
        m_typProgramSch edule.lngProgra mID=0;
        m_typProgramSch edule.strName=d r["name"].ToString();
        m_typProgramSch edule.strStartT ime=dr["StartTime"].ToString();
        m_typProgramSch edule.strEndTim e=dr["EndTime"].ToString();
        m_typProgramSch edule.strActors =dr["Actors"].ToString();
        m_typProgramSch edule.dtProgram Date=Convert.To DateTime(dr["Programdat e"]);
        m_typProgramSch edule.strStatus =dr["Status"].ToString();

        m_objProgramSch edule.addProgra mSchedule(ref m_typProgramSch edule);

        }

        Comment

        Working...