export to excel

Posted on 2010-11-30
Last Modified: 2012-05-10
I have seen some references on the net, but none were specific enough to solve my problem (I think b/c those solutions were all for DataGrid). I have a GridView, which I export to an Excel sheet via a button in my ASP.NET form. Problem, is, for whatever reason, (randomly it sure appears) after a random amount of records, values in columns that should be numeric, get that green arrow and "are formatted as text". Is there some way I can force all of my numeric columns to ALWAYS be numeric in the exported Excel file?

Question by:IT-VAS
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 10

Expert Comment

ID: 34237502
this usually happens if there is at least one record with a non numeric or empty value
LVL 14

Assisted Solution

robasta earned 250 total points
ID: 34237509
1. This may depend with your implementation, but here is what I did:

protected void Btn_ExportClick(object sender, EventArgs e)
       string style = @"<style> .text { mso-number-format:\@; } </script> "; 
    //the line above is where the magic is at!, 
    //that mso-number-format specifies the format (see link for more formats)

        Response.AddHeader("content-disposition", "attachment;
        Response.ContentType = "application/excel";

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

        // Style is added dynamically

Open in new window

2. The following blog has sample format. Choose yours...
LVL 14

Expert Comment

ID: 34237512
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.


Author Comment

ID: 34237525
but it is the same column some will numric and the others get it as text
and it is the numric value when i read from the database and it's don't empty
LVL 21

Assisted Solution

by:Alpesh Patel
Alpesh Patel earned 125 total points
ID: 34237555

Pls let me know your export columns has some encrypted column.

If not then any column which has some special characters so the formatting of columns will mash.

To solve just remove that column from you output and try again. It solves your problem.

I had faced same problem.

Author Comment

ID: 34237585
hi robasta!
how can i replace the code below in visual basic?
string style = @"<style> .text { mso-number-format:\@; } </script> ";

LVL 14

Accepted Solution

robasta earned 250 total points
ID: 34237803
Dim style As String = "<style> .text { mso-number-format:\@; } </script> "

Assisted Solution

Danielcmorris earned 125 total points
ID: 34275597
you  might want to take a look at this:  OfficeOpenXml


It gives you a lot more control over your excel files.


Author Comment

ID: 34280640
thanks everyone for your answer,

hi PatelAlpesh!
 you are right ,it happens because  the sentence begins with minus sign så in excel it will be change to text format, so i need to formatting the data before exporting to excel but i don't know how i do that , has anyone any idea, here is my code to exporting to Excel:

Response.AddHeader("content-disposition", "attachment;filename=timecollection.xls")
Response.ContentType = "application/"
Response.ContentEncoding = Encoding.GetEncoding("iso-8859-1")
 Me.EnableViewState = False
 Dim objStrWriter As New System.IO.StringWriter
 Dim objHtmlTextWriter As New System.Web.UI.HtmlTextWriter(objStrWriter)
If gvperson.Visible = True Then
End If
Response.Write(Regex.Replace(objStrWriter.ToString(), "(<a[^>]*>)|(</a>)", " ", RegexOptions.IgnoreCase))
LVL 29

Expert Comment

by:Kumaraswamy R
ID: 34459420
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question