Results 1 to 3 of 3

Thread: Convert the text file to Excel Format

  1. #1

    Convert the text file to Excel Format

    Convertion of txt file to xls file and convertion of data in grid to excel

    I have to convert the text file (comma seperated file ) to 97-2003 format xls. Can you Please provide me the code in asp.net?


  2. #2
    Moderator
    Join Date
    Dec 2010
    Answers
    40

    Re: Convert the text file to Excel Format

    Here's a sample of how you could convert a txt file to excel, assuming the txt file is delimited properly.

    Code:
           // Excel object references.
            private Excel.Application m_objExcel =  null;
            private Excel.Workbooks m_objBooks = null;
            private Excel._Workbook m_objBook = null;
            private Excel.Sheets m_objSheets = null;
            private Excel._Worksheet m_objSheet = null;
            private Excel.Range m_objRange =  null;
            private Excel.Font m_objFont = null;
            private Excel.QueryTables m_objQryTables = null;
            private Excel._QueryTable m_objQryTable = null;
    
            // Frequenty-used variable for optional arguments.
            private object m_objOpt = System.Reflection.Missing.Value;
    
            // Paths used by the sample code for accessing and storing data.
            private object m_strSampleFolder = "C:\\ExcelData\\";
    
    
        --------Code to export txt to excel-----
          // Open the text file in Excel. 
          m_objExcel = new Excel.Application();
          m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
          m_objBooks.OpenText(m_strSampleFolder + "Book6.txt", Excel.XlPlatform.xlWindows, 1, 
    	    Excel.XlTextParsingType.xlDelimited, Excel.XlTextQualifier.xlTextQualifierDoubleQuote,
    	    false, true, false, false, false, false, m_objOpt, m_objOpt, 
    	    m_objOpt, m_objOpt, m_objOpt);
    
          m_objBook = m_objExcel.ActiveWorkbook;
    
          // Save the text file in the typical workbook format and quit Excel.
          m_objBook.SaveAs(m_strSampleFolder + "Book6.xls", Excel.XlFileFormat.xlWorkbookNormal, 
    	  m_objOpt, m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange, m_objOpt, m_objOpt,
    	  m_objOpt, m_objOpt); 
         m_objBook.Close(false, m_objOpt, m_objOpt);
         m_objExcel.Quit();



  3. #3
    Moderator
    Join Date
    Dec 2010
    Answers
    40

    Re: Convert the text file to Excel Format

    Using asp.net, here's a way u could do it by using the RenderControl method of the DataGrid and setting the contenttype on the http response to "application/vnd.xls".

    You may want to put this part of the code on click of the Export button.

    Code:
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=MyXlFileName.xls");
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "application/vnd.xls";
    System.IO.StringWriter stringWriter = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
    myDataGrid.RenderControl(htmlWriter);
    Response.Write(stringWriter.ToString());
    Response.End();



  4.  Sponsored Ads
    Ad


     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact