Solved

Custom headers

Posted on 2016-09-28
9
41 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
[X]
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
  • 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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
 
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

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

749 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