Solved

Custom headers

Posted on 2016-09-28
9
37 Views
Last Modified: 2016-09-29
Hi,

I was wondering if it is possible to display custom headers. I need to create an Excel report that display "user-friendly" headers.
What I use is something like this

Select username as "User Name", firstname as "First Name".....

It works fine to display all fields.  What if user can select the headers they want?  Is there a way to accomplish this goal?  thanks
0
Comment
Question by:mcrmg
  • 5
  • 4
9 Comments
 
LVL 57
ID: 41820690
<<It works fine to display all fields.  What if user can select the headers they want?  Is there a way to accomplish this goal?  thanks>>

 Not sure I understand the question, but there are two methods to do this:

1. Build the SQL on the fly, aliasing the field names as you showed.

2. Use a querydef, and set the Caption property on each of the fields.

  You can see this in the query designer by right clicking on a field to get it's properties.

<<What if user can select the headers they want? >>

 You'd use a form to collect that input and use for #1 or #2 above.

Jim.
0
 

Author Comment

by:mcrmg
ID: 41820837
At this point, users can pick their fields that they want. I combine all the fields into a comma delimited string and store them in a table. They can actually get the Excel report. The problem, again, is the headers.

I think I will need more directions.   In this case, which approach do you think is better?

If I have a string like this  userid, username, firstname, lastname, address

How can I alias the fields with different names?  thanks
0
 
LVL 57
ID: 41821505
<<   In this case, which approach do you think is better? >>

  Their both more or less equivenlant.  Depends on what your doing in code already as to which would work better.

<< I combine all the fields into a comma delimited string and store them in a table. >>

  You would probably change that part.   When you combine the strings to form the SELECT, that's the point where you would add the alias.

 strSQL = "SELECT " & <users first field choice> & " As " & <what user wants as a header> & ", " & <users 2nd field choice> & " As " & <what user wants as a header> & " FROM "

 and you'd end up with an SQL statement that looks just like what you posted in your question.

 If you post the code that is doing this and creates the spreadsheet, I can be more specific.

Jim.
0
 

Author Comment

by:mcrmg
ID: 41821554
I am not sure if this is enough, user can select the fields they want from a listbox in asp.net page. Then I loop through the fields by adding "," in between, then save that string to a field in sql table. So, when generating Excel report, it will be like this

strSQL =  "SELECT " & db(field) & " FROM table"

    // Save Report
    protected void SaveReportbtn_Click(object sender, EventArgs e)
    {
        string TimeslotItems = "";
        ReportNameMsg.Text = "";

        if (ReportName.Text == "")
        {
            ReportNameMsg.ForeColor = System.Drawing.Color.Red;
            ReportNameMsg.Text = "Please enter a report name";
        }
        else
        {
            foreach (ListItem item in mainlist.Items)
            {
                TimeslotItems += item.ToString() + ","; // /n to print each item on new line or you omit /n to print text on same line
            }
            //testBox.Text = TimeslotItems;

            CreateReport(TimeslotItems);
        }
    }

Open in new window

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 41821574
Not sure why I thought this was a Access/VBA question....no wonder you didn't understand what I was saying!  My apologies.

But you'd need to modify line 16 in the above adding in the aliases and then possibly modify CreateReport(), although I'm assuming your just passing the select list and it's building an SQL statement with that, so it will most likely work with no changes other than to line 16 of what you posted.

Jim.
0
 

Author Comment

by:mcrmg
ID: 41821590
No worries....     :)

Do you think it would work?  In the dropdown list, there will be a value and a text (where user will see on the page).  In order to get both fields of table and the "user friendly" fields, using a "*" in between them.  For example,
                    <asp:listbox ID="FollowUpReason" runat="server" Width="150px">
                    <asp:ListItem Value="">Select</asp:ListItem>
                    <asp:ListItem Value="username*User Name">User Name</asp:ListItem>
                    </asp:listbox>

I will have to parse this once pass to the function.  Is there a better way? thanks
0
 
LVL 57
ID: 41821615
<<Is there a better way? thanks>>

 Don't believe so.

<<username*User Name>>

  Your only job then would be to replace the * with " as "

Jim.
0
 

Author Closing Comment

by:mcrmg
ID: 41821617
thank you very much
0
 

Author Comment

by:mcrmg
ID: 41821631
oops...I left out another issue, Please let me know if I need to open a new question.

What if I need to do some data type conversion? such as

convert(varchar(50),cast(a.abc as money),1) as 'ABC',

--OR--

case a.abc                        
        when '1' then 'Yes'
        when '2' then 'No'  
        else ''
        end as 'ABC',


thanks
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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

863 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

21 Experts available now in Live!

Get 1:1 Help Now