Solved

export to excel

Posted on 2010-11-30
11
855 Views
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?

0
Comment
Question by:IT-VAS
11 Comments
 
LVL 10

Expert Comment

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

Assisted Solution

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

Code:
 
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.ClearContent();
        Response.AddHeader("content-disposition", "attachment;
        filename=MyExcelFile.xls");
        Response.ContentType = "application/excel";

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

        gvUsers.RenderControl(htw);
        // Style is added dynamically
        Response.Write(style); 
        Response.Write(sw.ToString());
        Response.End();
    }

Open in new window



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

Expert Comment

by:robasta
ID: 34237512
0
 

Author Comment

by:IT-VAS
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
0
 
LVL 21

Assisted Solution

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

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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

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


thanks,
0
 
LVL 14

Accepted Solution

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

Assisted Solution

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

DocumentFormat.OpenXml.dll
DocumentFormat.OpenXml.Extensions.dll

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


0
 

Author Comment

by:IT-VAS
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/vnd.ms-excel"
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
   gvperson.RenderControl(objHtmlTextWriter)
End If
Response.Write(Regex.Replace(objStrWriter.ToString(), "(<a[^>]*>)|(</a>)", " ", RegexOptions.IgnoreCase))
Response.End()
0
 
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to picture file 3 66
What .NET website keeps me current? 9 52
Re-position the objects 7 95
ASP.net VB.net Object not set to an instance of an object 4 33
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

929 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now