Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7196967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:52:48+00:00 2026-05-28T20:52:48+00:00

I am trying to convert a DataTable into XLS file. Code below Snippet1 Response.Clear();

  • 0

I am trying to convert a DataTable into XLS file.
Code below

Snippet1

Response.Clear();
        Response.Buffer = true;

        Response.AddHeader("content-disposition","attachment;filename=New.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";

        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);

        for (int i = 1; i < gvGridView.HeaderRow.Cells.Count; i++)
        {
            gvGridView.HeaderRow.Cells[i].Text = Table.Columns[i - 1].ColumnName;
        }

        for (int i = 0; i < gvGridView.Rows.Count; i++)
        {
            //Apply text style to each Row
            gvGridView.Rows[i].Attributes.Add("class", "textmode");
        }
        gvGridView.RenderControl(hw);

        //style to format numbers to string
        string style = @"<style> .textmode { mso-number-format:\@; } </style>";
        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();

This code seems fine. It produced an xls file. And it can be opened correctly. However I tried to reverse engineer this process. In short, converting back this particular xls file into a datatable. Code below:

Snippet2

int idx = filen.IndexOf(".");
        string tf = filen.Remove(idx, 4);

        OleDbConnection MyConnection = null;    
        DataSet DtSet = null;    
        OleDbDataAdapter MyCommand = null; 
        //Connection for MS Excel 2003 .xls format
        MyConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; Data Source='" + path + "';Extended Properties=Excel 8.0;");     
        MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [table$]", MyConnection);      
        DtSet = new System.Data.DataSet();       
        MyCommand.Fill(DtSet);        
        dt = DtSet.Tables[0];        
        MyConnection.Close();            
        if (dt.Rows.Count > 0)        
        {            
            theGridView.DataSource = dt;           
            theGridView.DataBind();       
        }        
        if(System.IO.File.Exists(path))       
        {            
            System.IO.File.Delete(path);      
        }

This code produced error. It says:

External file format not supported

I noticed this error is similar when you use the wrong OLEDB provider. But in my case there is supposed to be no problem as I am using Microsoft.Jet.OLEDB.4.0 for .xls (2003) format. But somehow it happened still.

This code somehow works for any other .xls file that I tested. It doesn’t work only to the files that produced from conversion using the #Snippet1. However supposed I produced a file using #Snippet1 and then I copy all the cells into a new .xls file. The #Snippet2 will work against this new file. In short, only file directly produced from #Snippet1 that won’t work.

I figured I did something wrong. So I hope that somebody might be able to help me here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:52:49+00:00Added an answer on May 28, 2026 at 8:52 pm

    Your Excel output snippet OS not generating an Excel file. It is generating an html table and setting the content type to tell the computer to open it with Excel.

    You will need to use a library to generate a real excel file or use the excel data interop (there are plenty of articles in both) in order to consume it directly.

    Alternatively you could generate a .csv file instead and still keep the Excel content-type so excel opens it but you lose formatting. Then you import can check the extension to read it either as excel or flat text.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to convert this c code into MIPS and run it in SPIM. int
I have an XML file and I'm trying convert it into a (table( HTML
I am trying to convert all DateTime values in a DataTable to strings. Here
I'm trying to convert old QuickTime framework code to the 64-bit Cocoa-based QTKit on
I'm trying to convert some code that worked great in VB, but I can't
I am trying to convert an ASP.NET website into a web application project. The
I am trying to convert an int into three bytes representing that int (big
I am trying to convert a DataTable to an IEnumerable. Where T is a
I'm using lambda expression and trying convert into uint while adding into the Hashset.
I am trying to convert datatable to List. Could you please help me with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.