Solved

Custom headers

Posted on 2016-09-28
9
33 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

708 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

19 Experts available now in Live!

Get 1:1 Help Now